Sync jsonutils from oslo-incubator
Included commits: commit ad248f66 Specify namedtuple_as_object=False when using simplejson commit ef37e032 Added missing jsonutils.dump() function My motivation of sync is the following neutron change. https://review.openstack.org/#/c/112178/ Closes-Bug: #1356173 Change-Id: I0086e5ffa27fa5035112d4868aeb14cbdfa8f4de
This commit is contained in:
parent
220ccb816c
commit
710dd17a6e
|
@ -38,11 +38,13 @@ import inspect
|
||||||
import itertools
|
import itertools
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
is_simplejson = False
|
||||||
if sys.version_info < (2, 7):
|
if sys.version_info < (2, 7):
|
||||||
# On Python <= 2.6, json module is not C boosted, so try to use
|
# On Python <= 2.6, json module is not C boosted, so try to use
|
||||||
# simplejson module if available
|
# simplejson module if available
|
||||||
try:
|
try:
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
is_simplejson = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import json
|
import json
|
||||||
else:
|
else:
|
||||||
|
@ -165,9 +167,17 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||||
|
|
||||||
|
|
||||||
def dumps(value, default=to_primitive, **kwargs):
|
def dumps(value, default=to_primitive, **kwargs):
|
||||||
|
if is_simplejson:
|
||||||
|
kwargs['namedtuple_as_object'] = False
|
||||||
return json.dumps(value, default=default, **kwargs)
|
return json.dumps(value, default=default, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def dump(obj, fp, *args, **kwargs):
|
||||||
|
if is_simplejson:
|
||||||
|
kwargs['namedtuple_as_object'] = False
|
||||||
|
return json.dump(obj, fp, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def loads(s, encoding='utf-8', **kwargs):
|
def loads(s, encoding='utf-8', **kwargs):
|
||||||
return json.loads(strutils.safe_decode(s, encoding), **kwargs)
|
return json.loads(strutils.safe_decode(s, encoding), **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue