Use jsonutils instead of stdlib json

jsonutils provides multiple benefits in comparison to pure stdlib json
(like using simplejson on Python 2.6).

Similar patch was already merged before [1], but since it lacked hacking
rule to enforce jsonutils usage, new occurrences of stdlib json module
usage were introduced.

This patch switches all the code to using jsonutils and adds a hacking
rule to enforce the rule.

The hacking rule requires that jsonutils module does not mimic as 'json'
thru using import renames, so the code was updated not to rename the
module when doing import.

The hacking rule was shamelessly copied from the corresponding nova
review [2].

[1]: https://review.openstack.org/#/c/99760/
[2]: https://review.openstack.org/111296/

Change-Id: Ie7a5bb76445e15cde9fbf9ff3d2101a014637b37
This commit is contained in:
Ihar Hrachyshka 2014-08-08 00:04:44 +02:00
parent 459ddd074c
commit 8b8376c635
3 changed files with 10 additions and 9 deletions

View File

@ -9,6 +9,7 @@ Neutron Specific Commandments
--------------------------
- [N320] Validate that LOG messages, except debug ones, have translations
- [N321] Validate that jsonutils module is used instead of json
Creating Unit Tests
-------------------

View File

@ -34,7 +34,7 @@ from neutron import context
from neutron.db.loadbalancer import loadbalancer_db as lb_db
from neutron.extensions import loadbalancer
from neutron.openstack.common import excutils
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.openstack.common import log as logging
from neutron.plugins.common import constants
from neutron.services.loadbalancer.drivers import abstract_driver
@ -728,7 +728,7 @@ class vDirectRESTClient:
if binary:
body = data
else:
body = json.dumps(data)
body = jsonutils.dumps(data)
debug_data = 'binary' if binary else body
debug_data = debug_data if debug_data else 'EMPTY'
@ -758,7 +758,7 @@ class vDirectRESTClient:
respstr = response.read()
respdata = respstr
try:
respdata = json.loads(respstr)
respdata = jsonutils.loads(respstr)
except ValueError:
# response was not JSON, ignore the exception
pass

View File

@ -24,7 +24,7 @@ from six.moves import queue as Queue
from neutron import context
from neutron.extensions import loadbalancer
from neutron import manager
from neutron.openstack.common import jsonutils as json
from neutron.openstack.common import jsonutils
from neutron.plugins.common import constants
from neutron.services.loadbalancer.drivers.radware import driver
from neutron.services.loadbalancer.drivers.radware import exceptions as r_exc
@ -66,9 +66,9 @@ def rest_call_function_mock(action, resource, data, headers, binary=False):
def _get_handler(resource):
if resource == GET_200[2]:
if rest_call_function_mock.TEMPLATES_MISSING:
data = json.loads('[]')
data = jsonutils.loads('[]')
else:
data = json.loads(
data = jsonutils.loads(
'[{"name":"openstack_l2_l3"},{"name":"openstack_l4"}]'
)
return 200, '', '', data
@ -76,7 +76,7 @@ def _get_handler(resource):
if resource in GET_200:
return 200, '', '', ''
else:
data = json.loads('{"complete":"True", "success": "True"}')
data = jsonutils.loads('{"complete":"True", "success": "True"}')
return 202, '', '', data
@ -86,10 +86,10 @@ def _delete_handler(resource):
def _post_handler(resource, binary):
if re.search(r'/api/workflow/.+/action/.+', resource):
data = json.loads('{"uri":"some_uri"}')
data = jsonutils.loads('{"uri":"some_uri"}')
return 202, '', '', data
elif re.search(r'/api/service\?name=.+', resource):
data = json.loads('{"links":{"actions":{"provision":"someuri"}}}')
data = jsonutils.loads('{"links":{"actions":{"provision":"someuri"}}}')
return 201, '', '', data
elif binary:
return 201, '', '', ''