Use ujson instead of jsonutils

ujson is much faster than json.
For more details, please check [1] & [2].

[1] https://blog.dataweave.com/json-vs-simplejson-vs-ujson-2887b2c128b2
[2] https://blog.ionelmc.ro/2015/11/22/memory-use-and-speed-of-json-parsers/

Change-Id: I2756fb6cdde713be46a3dac09eeccf13d431a5c5
This commit is contained in:
Vu Cong Tuan 2017-11-01 13:23:49 +07:00
parent 935f21736a
commit 775a18c646
6 changed files with 21 additions and 21 deletions

View File

@ -20,7 +20,7 @@ import os
from oslo_config import cfg
from oslo_log import log
from oslo_serialization import jsonutils
import ujson
import webob.dec
import webob.exc
@ -100,7 +100,7 @@ class MeteosKeystoneContext(base_wsgi.Middleware):
if req.headers.get('X_SERVICE_CATALOG') is not None:
try:
catalog_header = req.headers.get('X_SERVICE_CATALOG')
service_catalog = jsonutils.loads(catalog_header)
service_catalog = ujson.loads(catalog_header)
except ValueError:
raise webob.exc.HTTPInternalServerError(
_('Invalid service catalog json.'))

View File

@ -19,9 +19,9 @@ import math
import time
from oslo_log import log
from oslo_serialization import jsonutils
from oslo_utils import strutils
import six
import ujson
import webob
import webob.exc
@ -276,7 +276,7 @@ class JSONDeserializer(TextDeserializer):
def _from_json(self, datastring):
try:
return jsonutils.loads(datastring)
return ujson.loads(datastring)
except ValueError:
msg = _("cannot understand JSON")
raise exception.MalformedRequestBody(reason=msg)
@ -299,7 +299,7 @@ class JSONDictSerializer(DictSerializer):
"""Default JSON request body serialization."""
def default(self, data):
return six.b(jsonutils.dumps(data))
return six.b(ujson.dumps(data))
def serializers(**serializers):
@ -486,7 +486,7 @@ def action_peek_json(body):
"""Determine action to invoke."""
try:
decoded = jsonutils.loads(body)
decoded = ujson.loads(body)
except ValueError:
msg = _("cannot understand JSON")
raise exception.MalformedRequestBody(reason=msg)

View File

@ -18,7 +18,7 @@ Client side of the learning RPC API.
from oslo_config import cfg
import oslo_messaging as messaging
from oslo_serialization import jsonutils
import ujson
from meteos import rpc
@ -67,7 +67,7 @@ class LearningAPI(object):
return client.cast(ctxt, method, **kwargs)
def create_template(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.call(context, self.make_msg('create_template',
request_spec=request_spec_p))
@ -76,7 +76,7 @@ class LearningAPI(object):
id=id))
def create_experiment(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('create_experiment',
request_spec=request_spec_p))
@ -85,7 +85,7 @@ class LearningAPI(object):
id=id))
def create_dataset(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('create_dataset',
request_spec=request_spec_p))
@ -96,7 +96,7 @@ class LearningAPI(object):
id=id))
def create_model(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('create_model',
request_spec=request_spec_p))
@ -108,17 +108,17 @@ class LearningAPI(object):
recreate=recreate))
def load_model(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('load_model',
request_spec=request_spec_p))
def unload_model(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('unload_model',
request_spec=request_spec_p))
def create_model_evaluation(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('create_model_evaluation',
request_spec=request_spec_p))
@ -129,12 +129,12 @@ class LearningAPI(object):
id=id))
def create_learning(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.cast(context, self.make_msg('create_learning',
request_spec=request_spec_p))
def create_online_learning(self, context, request_spec):
request_spec_p = jsonutils.to_primitive(request_spec)
request_spec_p = ujson.to_primitive(request_spec)
return self.call(context, self.make_msg('create_online_learning',
request_spec=request_spec_p))

View File

@ -27,7 +27,7 @@ __all__ = [
from oslo_config import cfg
import oslo_messaging as messaging
from oslo_serialization import jsonutils
import ujson
import meteos.context
import meteos.exception
@ -83,7 +83,7 @@ class JsonPayloadSerializer(messaging.NoOpSerializer):
@staticmethod
def serialize_entity(context, entity):
return jsonutils.to_primitive(entity, convert_instances=True)
return ujson.to_primitive(entity, convert_instances=True)
class RequestContextSerializer(messaging.Serializer):

View File

@ -16,7 +16,7 @@ import collections
import functools
import oslo_messaging as messaging
from oslo_serialization import jsonutils
import ujson
from meteos import rpc
@ -53,7 +53,7 @@ class FakeNotifier(object):
# NOTE(sileht): simulate the kombu serializer
# this permit to raising an exception if something have not
# been serialized correctly
jsonutils.to_primitive(payload)
ujson.to_primitive(payload)
msg = dict(publisher_id=self.publisher_id,
priority=priority,
event_type=event_type,

View File

@ -20,7 +20,6 @@ oslo.messaging>=5.2.0 # Apache-2.0
oslo.middleware>=3.0.0 # Apache-2.0
oslo.policy>=1.9.0 # Apache-2.0
oslo.rootwrap>=5.0.0 # Apache-2.0
oslo.serialization>=1.10.0 # Apache-2.0
oslo.service>=1.10.0 # Apache-2.0
oslo.utils>=3.16.0 # Apache-2.0
oslo.concurrency>=3.8.0 # Apache-2.0
@ -42,4 +41,5 @@ stevedore>=1.16.0 # Apache-2.0
python-cinderclient>=2.0.1 # Apache-2.0
python-novaclient>=7.1.0 # Apache-2.0
python-saharaclient>=0.14.0 # Apache-2.0
ujson>=1.35 # BSD
WebOb>=1.2.3 # MIT