Specify namedtuple_as_object=False when using simplejson
This makes namedtuple serialization consistent with non simplejson case. Closes-Bug: #1356173 Change-Id: I9dd879914c7cb226c71e856914536318b416e488
This commit is contained in:
parent
af8fafcf34
commit
c5b869462f
@ -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,10 +167,14 @@ 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):
|
def dump(obj, fp, *args, **kwargs):
|
||||||
|
if is_simplejson:
|
||||||
|
kwargs['namedtuple_as_object'] = False
|
||||||
return json.dump(obj, fp, *args, **kwargs)
|
return json.dump(obj, fp, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,6 +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 collections
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@ -33,8 +34,9 @@ class JSONUtilsTestMixin(object):
|
|||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(JSONUtilsTestMixin, self).setUp()
|
super(JSONUtilsTestMixin, self).setUp()
|
||||||
self.json_patcher = mock.patch.object(
|
self.json_patcher = mock.patch.multiple(
|
||||||
jsonutils, 'json', self.json_impl)
|
jsonutils, json=self.json_impl,
|
||||||
|
is_simplejson=self.json_impl is simplejson)
|
||||||
self.json_impl_mock = self.json_patcher.start()
|
self.json_impl_mock = self.json_patcher.start()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
@ -44,6 +46,10 @@ class JSONUtilsTestMixin(object):
|
|||||||
def test_dumps(self):
|
def test_dumps(self):
|
||||||
self.assertEqual('{"a": "b"}', jsonutils.dumps({'a': 'b'}))
|
self.assertEqual('{"a": "b"}', jsonutils.dumps({'a': 'b'}))
|
||||||
|
|
||||||
|
def test_dumps_namedtuple(self):
|
||||||
|
n = collections.namedtuple("foo", "bar baz")(1, 2)
|
||||||
|
self.assertEqual('[1, 2]', jsonutils.dumps(n))
|
||||||
|
|
||||||
def test_dump(self):
|
def test_dump(self):
|
||||||
expected = '{"a": "b"}'
|
expected = '{"a": "b"}'
|
||||||
json_dict = {'a': 'b'}
|
json_dict = {'a': 'b'}
|
||||||
@ -53,6 +59,15 @@ class JSONUtilsTestMixin(object):
|
|||||||
|
|
||||||
self.assertEqual(expected, fp.getvalue())
|
self.assertEqual(expected, fp.getvalue())
|
||||||
|
|
||||||
|
def test_dump_namedtuple(self):
|
||||||
|
expected = '[1, 2]'
|
||||||
|
json_dict = collections.namedtuple("foo", "bar baz")(1, 2)
|
||||||
|
|
||||||
|
fp = six.StringIO()
|
||||||
|
jsonutils.dump(json_dict, fp)
|
||||||
|
|
||||||
|
self.assertEqual(expected, fp.getvalue())
|
||||||
|
|
||||||
def test_loads(self):
|
def test_loads(self):
|
||||||
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))
|
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user