Update json module to jsonutils

oslo project provide jsonutils, and zunclient use it in many place[1],
this PS to update the remained json module to oslo jsonutils for
consistency.

[1]: https://github.com/openstack/python-zunclient/search?q=jsonutils&unscoped_q=jsonutils

Change-Id: I3e352c11824689ec9ef4338f151bd4cc562b1bf4
This commit is contained in:
cao.yuan 2019-02-25 19:00:15 +08:00
parent 9930b6f8d0
commit 55cdb633f4
9 changed files with 31 additions and 27 deletions

View File

@ -15,9 +15,9 @@
# under the License.
import copy
import json
import os
from oslo_log import log as logging
from oslo_serialization import jsonutils
import socket
import ssl
@ -43,10 +43,10 @@ def _extract_error_json(body):
"""Return error_message from the HTTP response body."""
error_json = {}
try:
body_json = json.loads(body)
body_json = jsonutils.loads(body)
if 'error_message' in body_json:
raw_msg = body_json['error_message']
error_json = json.loads(raw_msg)
error_json = jsonutils.loads(raw_msg)
elif 'error' in body_json:
error_body = body_json['error']
error_json = {'faultstring': error_body['title'],
@ -219,7 +219,7 @@ class HTTPClient(object):
kwargs['headers'].setdefault('Accept', 'application/json')
if 'body' in kwargs:
kwargs['body'] = json.dumps(kwargs['body'])
kwargs['body'] = jsonutils.dumps(kwargs['body'])
resp, body_iter = self._http_request(url, method, **kwargs)
content_type = resp.getheader('content-type', None)
@ -230,7 +230,7 @@ class HTTPClient(object):
if 'application/json' in content_type:
body = ''.join([chunk for chunk in body_iter])
try:
body = json.loads(body)
body = jsonutils.loads(body)
except ValueError:
LOG.error('Could not decode response body as JSON')
else:
@ -362,7 +362,7 @@ class SessionClient(adapter.LegacyJsonAdapter):
kwargs['headers'].setdefault('Accept', 'application/json')
if 'body' in kwargs:
kwargs['data'] = json.dumps(kwargs.pop('body'))
kwargs['data'] = jsonutils.dumps(kwargs.pop('body'))
resp = self._http_request(url, method, **kwargs)
body = resp.content

View File

@ -12,9 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import yaml
from oslo_serialization import jsonutils
if hasattr(yaml, 'CSafeDumper'):
yaml_dumper_base = yaml.CSafeDumper
@ -53,7 +54,7 @@ def parse(tmpl_str):
# strip any whitespace before the check
tmpl_str = tmpl_str.strip()
if tmpl_str.startswith('{'):
tpl = json.loads(tmpl_str)
tpl = jsonutils.loads(tmpl_str)
else:
try:
tpl = yaml.safe_load(tmpl_str)

View File

@ -16,10 +16,10 @@
import base64
import binascii
import json
import os
import re
from oslo_serialization import jsonutils
from oslo_utils import netutils
import six
from six.moves.urllib import parse
@ -78,7 +78,7 @@ def split_and_deserialize(string):
raise exc.CommandError(_('Attributes must be a list of '
'PATH=VALUE not "%s"') % string)
try:
value = json.loads(value)
value = jsonutils.loads(value)
except ValueError:
pass

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
from oslo_serialization import jsonutils
from tempest.lib.common.utils import data_utils
from tempest.lib import exceptions
@ -50,7 +50,7 @@ class TestCase(base.FunctionalTestBase):
output = self.openstack('appcontainer create {0}'
' --name {1} {2} {3}'
.format(opts, name, image, params))
container = json.loads(output)
container = jsonutils.loads(output)
if not output:
self.fail('Container has not been created!')
@ -71,7 +71,7 @@ class TestCase(base.FunctionalTestBase):
output = self.openstack('appcontainer run {0}'
' --name {1} {2} {3}'
.format(opts, name, image, params))
container = json.loads(output)
container = jsonutils.loads(output)
if not output:
self.fail('Container has not run!')
@ -105,7 +105,7 @@ class TestCase(base.FunctionalTestBase):
opts = self.get_opts(fields=fields)
output = self.openstack('appcontainer list {0} {1}'
.format(opts, params))
return json.loads(output)
return jsonutils.loads(output)
def container_show(self, identifier, fields=None, params=''):
"""Show specified container.
@ -118,7 +118,7 @@ class TestCase(base.FunctionalTestBase):
opts = self.get_opts(fields)
output = self.openstack('appcontainer show {0} {1} {2}'
.format(opts, identifier, params))
return json.loads(output)
return jsonutils.loads(output)
def container_rename(self, identifier, name):
"""Rename specified container.

View File

@ -13,11 +13,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
import six
from oslo_serialization import jsonutils
from zunclient import api_versions
from zunclient.common.apiclient import exceptions
@ -31,9 +30,9 @@ def _get_error_body(faultstring=None, debuginfo=None):
'faultstring': faultstring,
'debuginfo': debuginfo
}
raw_error_body = json.dumps(error_body)
raw_error_body = jsonutils.dumps(error_body)
body = {'error_message': raw_error_body}
raw_body = json.dumps(body)
raw_body = jsonutils.dumps(body)
return raw_body

View File

@ -12,9 +12,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import yaml
from oslo_serialization import jsonutils
from zunclient.common import cliutils as utils
from zunclient.common import template_utils
from zunclient.common import utils as zun_utils
@ -102,7 +103,7 @@ def do_capsule_describe(cs, args):
"""Show details of a capsule."""
capsule = cs.capsules.describe(args.capsule)
if args.format == 'json':
print(json.dumps(capsule._info, indent=4, sort_keys=True))
print(jsonutils.dumps(capsule._info, indent=4, sort_keys=True))
elif args.format == 'yaml':
print(yaml.safe_dump(capsule._info, default_flow_style=False))
elif args.format == 'table':

View File

@ -15,12 +15,13 @@
import argparse
from contextlib import closing
import io
import json
import os
import tarfile
import time
import yaml
from oslo_serialization import jsonutils
from zunclient.common import cliutils as utils
from zunclient.common import utils as zun_utils
from zunclient.common.websocketclient import exceptions
@ -345,7 +346,7 @@ def do_show(cs, args):
opts = zun_utils.remove_null_parms(**opts)
container = cs.containers.get(**opts)
if args.format == 'json':
print(json.dumps(container._info, indent=4, sort_keys=True))
print(jsonutils.dumps(container._info, indent=4, sort_keys=True))
elif args.format == 'yaml':
print(yaml.safe_dump(container._info, default_flow_style=False))
elif args.format == 'table':

View File

@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import yaml
from oslo_serialization import jsonutils
from zunclient.common import cliutils as utils
from zunclient.common import utils as zun_utils
@ -63,7 +64,7 @@ def do_host_show(cs, args):
"""Show details of a host."""
host = cs.hosts.get(args.host)
if args.format == 'json':
print(json.dumps(host._info, indent=4, sort_keys=True))
print(jsonutils.dumps(host._info, indent=4, sort_keys=True))
elif args.format == 'yaml':
print(yaml.safe_dump(host._info, default_flow_style=False))
elif args.format == 'table':

View File

@ -10,9 +10,10 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import yaml
from oslo_serialization import jsonutils
from zunclient.common import cliutils as utils
from zunclient.common import utils as zun_utils
from zunclient import exceptions as exc
@ -138,7 +139,7 @@ def do_registry_show(cs, args):
opts = zun_utils.remove_null_parms(**opts)
registry = cs.registries.get(**opts)
if args.format == 'json':
print(json.dumps(registry._info, indent=4, sort_keys=True))
print(jsonutils.dumps(registry._info, indent=4, sort_keys=True))
elif args.format == 'yaml':
print(yaml.safe_dump(registry._info, default_flow_style=False))
elif args.format == 'table':