Use oslo_serialization instead of the json module directly

* Always use oslo jsonutils.
* Consistently import jsonutils as-is.
* Use dump_as_bytes instead of dumps.
https://wiki.openstack.org/wiki/Python3#Serialization:_base64.2C_JSON.2C_etc.

Change-Id: Ifb6c673da24276b0528e5975f63ac326d558936d
This commit is contained in:
Erik Olof Gunnar Andersson 2019-01-14 21:11:57 -08:00
parent 44670b5775
commit b4120a1352
6 changed files with 20 additions and 24 deletions

View File

@ -16,13 +16,13 @@
# under the License.
import copy
import json
import logging
import os
import socket
import ssl
from keystoneauth1 import adapter
from oslo_serialization import jsonutils
from oslo_utils import importutils
import six
import six.moves.urllib.parse as urlparse
@ -43,7 +43,7 @@ def _extract_error_json_text(body_json):
error_json = {}
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'],
@ -69,7 +69,7 @@ def _extract_error_json(body, resp):
return {}
else:
try:
body_json = json.loads(body)
body_json = jsonutils.loads(body)
return _extract_error_json_text(body_json)
except ValueError:
return {}
@ -228,7 +228,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)
@ -239,7 +239,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:
@ -373,7 +373,7 @@ class SessionClient(adapter.LegacyJsonAdapter):
kwargs['headers'].setdefault('Content-Type', 'application/json')
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

@ -16,7 +16,6 @@
# under the License.
import base64
import json
import os
from cryptography.hazmat.backends import default_backend
@ -25,6 +24,7 @@ from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography import x509
from cryptography.x509.oid import NameOID
from oslo_serialization import jsonutils
from magnumclient import exceptions as exc
from magnumclient.i18n import _
@ -64,7 +64,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
@ -100,7 +100,7 @@ def handle_labels(labels):
if 'mesos_slave_executor_env_file' in labels:
environment_variables_data = handle_json_from_file(
labels['mesos_slave_executor_env_file'])
labels['mesos_slave_executor_env_variables'] = json.dumps(
labels['mesos_slave_executor_env_variables'] = jsonutils.dumps(
environment_variables_data)
return labels
@ -146,7 +146,7 @@ def handle_json_from_file(json_arg):
try:
with open(json_arg, 'r') as f:
json_arg = f.read().strip()
json_arg = json.loads(json_arg)
json_arg = jsonutils.loads(json_arg)
except IOError as e:
err = _("Cannot get JSON from file '%(file)s'. "
"Error: %(err)s") % {'err': e, 'file': json_arg}

View File

@ -13,13 +13,12 @@
# under the License.
#
import json
import mock
from oslo_serialization import jsonutils
import sys
from keystoneauth1 import fixture
import requests
import six
AUTH_TOKEN = "foobar"
AUTH_URL = "http://0.0.0.0"
@ -239,9 +238,7 @@ class FakeResponse(requests.Response):
self.status_code = status_code
self.headers.update(headers)
self._content = json.dumps(data)
if not isinstance(self._content, six.binary_type):
self._content = self._content.encode()
self._content = jsonutils.dump_as_bytes(data)
class FakeModel(dict):

View File

@ -13,9 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import mock
from oslo_serialization import jsonutils
import six
import socket
@ -37,7 +36,7 @@ def _get_error_body(faultstring=None, debuginfo=None, err_type=NORMAL_ERROR):
'faultstring': faultstring,
'debuginfo': debuginfo
}
raw_error_body = json.dumps(error_body)
raw_error_body = jsonutils.dumps(error_body)
body = {'error_message': raw_error_body}
elif err_type == ERROR_DICT:
body = {'error': {'title': faultstring, 'message': debuginfo}}
@ -47,7 +46,7 @@ def _get_error_body(faultstring=None, debuginfo=None, err_type=NORMAL_ERROR):
elif err_type == ERROR_LIST_WITH_DESC:
main_body = {'title': faultstring, 'description': debuginfo}
body = {'errors': [main_body]}
raw_body = json.dumps(body)
raw_body = jsonutils.dumps(body)
return raw_body
@ -347,7 +346,7 @@ class HttpClientTest(utils.BaseTestCase):
resp, body = client.json_request('GET', '/v1/resources')
self.assertEqual(resp, fake_resp)
self.assertEqual(json.dumps(body), err)
self.assertEqual(jsonutils.dumps(body), err)
def test_raw_request(self):
fake_resp = utils.FakeResponse(

View File

@ -17,7 +17,7 @@
import collections
import mock
from oslo_serialization import jsonutils as json
from oslo_serialization import jsonutils
import six
import six.moves.builtins as __builtin__
import tempfile
@ -261,7 +261,7 @@ class HandleJsonFromFileTest(test_utils.BaseTestCase):
f.flush()
steps = utils.handle_json_from_file(f.name)
self.assertEqual(json.loads(contents), steps)
self.assertEqual(jsonutils.loads(contents), steps)
@mock.patch.object(__builtin__, 'open', autospec=True)
def test_handle_json_from_file_open_fail(self, mock_open):

View File

@ -15,8 +15,8 @@
import copy
import datetime
import json as jsonlib
import os
from oslo_serialization import jsonutils
import sys
import fixtures
@ -182,7 +182,7 @@ class FakeSessionResponse(object):
def json(self):
if self.content is not None:
return jsonlib.loads(self.content)
return jsonutils.loads(self.content)
else:
return {}