Fix JSON serialization issues with Python3

Partially implements: blueprint python-3

Change-Id: I0f4ec3470529dfcda6fa9056d0785ec93c50d5dd
This commit is contained in:
Hiroaki Kobayashi 2018-01-12 14:56:20 +09:00
parent d475d81e83
commit f7098011a2
11 changed files with 26 additions and 29 deletions

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
from blazar.api.v1 import app as v1_app from blazar.api.v1 import app as v1_app
from blazar.api.v2 import app as v2_app from blazar.api.v2 import app as v2_app
@ -31,7 +31,7 @@ class VersionSelectorApplication(object):
def _append_versions_from_app(self, versions, app, environ): def _append_versions_from_app(self, versions, app, environ):
tmp_versions = app(environ, self.internal_start_response) tmp_versions = app(environ, self.internal_start_response)
if self._status.startswith("300"): if self._status.startswith("300"):
tmp_versions = json.loads("".join(tmp_versions)) tmp_versions = jsonutils.loads(tmp_versions.pop())
versions['versions'].extend(tmp_versions['versions']) versions['versions'].extend(tmp_versions['versions'])
def internal_start_response(self, status, response_headers, exc_info=None): def internal_start_response(self, status, response_headers, exc_info=None):
@ -49,7 +49,7 @@ class VersionSelectorApplication(object):
if len(versions['versions']): if len(versions['versions']):
start_response("300 Multiple Choices", start_response("300 Multiple Choices",
[("Content-Type", "application/json")]) [("Content-Type", "application/json")])
return [json.dumps(versions)] return [jsonutils.dump_as_bytes(versions)]
else: else:
start_response("204 No Content", []) start_response("204 No Content", [])
return [] return []

View File

@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
import six import six
from blazar import context from blazar import context
@ -23,7 +22,7 @@ from blazar import exceptions
def ctx_from_headers(headers): def ctx_from_headers(headers):
try: try:
service_catalog = json.loads(headers['X-Service-Catalog']) service_catalog = jsonutils.loads(headers['X-Service-Catalog'])
except KeyError: except KeyError:
raise exceptions.ServiceCatalogNotFound() raise exceptions.ServiceCatalogNotFound()
except TypeError: except TypeError:

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
import pecan import pecan
from blazar.api.v2 import controllers from blazar.api.v2 import controllers
@ -37,7 +37,7 @@ class RootController(object):
versions = {"versions": []} versions = {"versions": []}
self._append_versions_from_controller(versions['versions'], self._append_versions_from_controller(versions['versions'],
self.v2, 'v2') self.v2, 'v2')
return json.dumps(versions) return jsonutils.dump_as_bytes(versions)
@pecan.expose(content_type='application/json') @pecan.expose(content_type='application/json')
def versions(self): def versions(self):

View File

@ -160,7 +160,7 @@ def render(result=None, response_type=None, status=None, **kwargs):
_("Content type '%s' isn't supported") % response_type) _("Content type '%s' isn't supported") % response_type)
return return
body = serializer.dumps(result) body = serializer.dump_as_bytes(result)
response_type = str(response_type) response_type = str(response_type)

View File

@ -14,9 +14,9 @@
# limitations under the License. # limitations under the License.
import datetime import datetime
import json
import uuid import uuid
from oslo_serialization import jsonutils
import six import six
from wsme import types as wtypes from wsme import types as wtypes
from wsme import utils as wutils from wsme import utils as wutils
@ -81,7 +81,7 @@ class CPUInfo(wtypes.UserType):
# another. We need to keep this method as generic as # another. We need to keep this method as generic as
# possible, ie. we accept JSONified dict. # possible, ie. we accept JSONified dict.
try: try:
cpu_info = json.loads(value) cpu_info = jsonutils.loads(value)
except TypeError: except TypeError:
raise exceptions.InvalidInput(cls=CPUInfo.name, value=value) raise exceptions.InvalidInput(cls=CPUInfo.name, value=value)
if not isinstance(cpu_info, dict): if not isinstance(cpu_info, dict):

View File

@ -13,9 +13,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
from oslo_log import log as logging from oslo_log import log as logging
from oslo_serialization import jsonutils
import webob import webob
from blazar.db import exceptions as db_exceptions from blazar.db import exceptions as db_exceptions
@ -79,7 +78,7 @@ class ParsableErrorMiddleware(object):
if not state: if not state:
return app_iter return app_iter
try: try:
res_dct = json.loads(app_iter[0]) res_dct = jsonutils.loads(app_iter[0])
except ValueError: except ValueError:
return app_iter return app_iter
else: else:
@ -128,7 +127,7 @@ class ParsableErrorMiddleware(object):
state['status_code'] = cls.code state['status_code'] = cls.code
# NOTE(sbauza): Client expects a JSON encoded dict # NOTE(sbauza): Client expects a JSON encoded dict
body = [json.dumps( body = [jsonutils.dump_as_bytes(
{'error_code': state['status_code'], {'error_code': state['status_code'],
'error_message': faultstring, 'error_message': faultstring,
'error_name': state['status_code']} 'error_name': state['status_code']}

View File

@ -24,7 +24,7 @@ class JsonEncoded(sa.TypeDecorator):
def process_bind_param(self, value, dialect): def process_bind_param(self, value, dialect):
if value is not None: if value is not None:
value = jsonutils.dumps(value) value = jsonutils.dump_as_bytes(value)
return value return value
def process_result_value(self, value, dialect): def process_result_value(self, value, dialect):

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
from blazar.api import context as api_context from blazar.api import context as api_context
from blazar import context from blazar import context
@ -34,7 +34,7 @@ class ContextTestCase(tests.TestCase):
def test_ctx_from_headers(self): def test_ctx_from_headers(self):
self.context = self.patch(context, 'BlazarContext') self.context = self.patch(context, 'BlazarContext')
catalog = json.dumps({'nova': 'catalog'}) catalog = jsonutils.dump_as_bytes({'nova': 'catalog'})
self.fake_headers[u'X-Service-Catalog'] = catalog self.fake_headers[u'X-Service-Catalog'] = catalog
api_context.ctx_from_headers(self.fake_headers) api_context.ctx_from_headers(self.fake_headers)
self.context.assert_called_once_with(user_id=u'1', self.context.assert_called_once_with(user_id=u'1',

View File

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
from blazar.tests import api from blazar.tests import api
@ -21,7 +21,7 @@ from blazar.tests import api
class TestRoot(api.APITest): class TestRoot(api.APITest):
def setUp(self): def setUp(self):
super(TestRoot, self).setUp() super(TestRoot, self).setUp()
self.versions = json.dumps( self.versions = jsonutils.dump_as_bytes(
{"versions": {"versions":
[{"status": "CURRENT", [{"status": "CURRENT",
"id": "v2.0", "id": "v2.0",

View File

@ -13,7 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json from oslo_serialization import jsonutils
from blazar.api import app as api from blazar.api import app as api
from blazar.api.v1 import app as v1_app from blazar.api.v1 import app as v1_app
@ -35,7 +35,7 @@ class FakeWSGIApp(object):
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
start_response(self.status_code, []) start_response(self.status_code, [])
return [json.dumps(self.versions)] return [jsonutils.dump_as_bytes(self.versions)]
class TestVersionDiscovery(tests.TestCase): class TestVersionDiscovery(tests.TestCase):
@ -62,7 +62,7 @@ class TestVersionDiscovery(tests.TestCase):
environ = {'PATH_INFO': self.path} environ = {'PATH_INFO': self.path}
versions_raw = version_selector(environ, self.start_response) versions_raw = version_selector(environ, self.start_response)
versions = json.loads("".join(versions_raw)) versions = jsonutils.loads(versions_raw.pop())
self.assertEqual(2, len(versions['versions'])) self.assertEqual(2, len(versions['versions']))
self.assertEqual("v{0}".format(self.v1_make_app().id_version), self.assertEqual("v{0}".format(self.v1_make_app().id_version),
@ -79,7 +79,7 @@ class TestVersionDiscovery(tests.TestCase):
environ = {'PATH_INFO': self.path} environ = {'PATH_INFO': self.path}
versions_raw = version_selector(environ, self.start_response) versions_raw = version_selector(environ, self.start_response)
versions = json.loads("".join(versions_raw)) versions = jsonutils.loads(versions_raw.pop())
self.assertEqual(1, len(versions['versions'])) self.assertEqual(1, len(versions['versions']))
self.assertEqual("v{0}".format(self.v1_make_app().id_version), self.assertEqual("v{0}".format(self.v1_make_app().id_version),
@ -117,7 +117,7 @@ class TestVersionSelectorApplication(tests.TestCase):
environ = {'PATH_INFO': "/v1"} environ = {'PATH_INFO': "/v1"}
versions_raw = version_selector(environ, self.start_response) versions_raw = version_selector(environ, self.start_response)
versions = json.loads("".join(versions_raw)) versions = jsonutils.loads(versions_raw.pop())
self.assertEqual(self.v1_make_app().versions, versions) self.assertEqual(self.v1_make_app().versions, versions)
def test_get_v2_app(self): def test_get_v2_app(self):
@ -125,5 +125,5 @@ class TestVersionSelectorApplication(tests.TestCase):
environ = {'PATH_INFO': "/v2"} environ = {'PATH_INFO': "/v2"}
versions_raw = version_selector(environ, self.start_response) versions_raw = version_selector(environ, self.start_response)
versions = json.loads("".join(versions_raw)) versions = jsonutils.loads(versions_raw.pop())
self.assertEqual(self.v2_make_app().versions, versions) self.assertEqual(self.v2_make_app().versions, versions)

View File

@ -13,8 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from oslo_serialization import jsonutils
import six import six
from blazar.manager import exceptions as manager_ex from blazar.manager import exceptions as manager_ex
@ -30,7 +29,7 @@ def convert_requirements(requirements):
# Convert text to json # Convert text to json
if isinstance(requirements, six.string_types): if isinstance(requirements, six.string_types):
try: try:
requirements = json.loads(requirements) requirements = jsonutils.loads(requirements)
except ValueError: except ValueError:
raise manager_ex.MalformedRequirements(rqrms=requirements) raise manager_ex.MalformedRequirements(rqrms=requirements)