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:
YAMAMOTO Takashi 2014-08-15 16:14:26 +09:00
parent 220ccb816c
commit 710dd17a6e
1 changed files with 10 additions and 0 deletions

View File

@ -38,11 +38,13 @@ import inspect
import itertools
import sys
is_simplejson = False
if sys.version_info < (2, 7):
# On Python <= 2.6, json module is not C boosted, so try to use
# simplejson module if available
try:
import simplejson as json
is_simplejson = True
except ImportError:
import json
else:
@ -165,9 +167,17 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
def dumps(value, default=to_primitive, **kwargs):
if is_simplejson:
kwargs['namedtuple_as_object'] = False
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):
return json.loads(strutils.safe_decode(s, encoding), **kwargs)