summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYAMAMOTO Takashi <yamamoto@valinux.co.jp>2014-08-14 02:18:19 +0900
committerYAMAMOTO Takashi <yamamoto@valinux.co.jp>2014-08-14 02:50:59 +0900
commitc5b869462f9b1d1a56dd98cd66c7b5f4238334ae (patch)
treeaff8f7bd3d686117b6fc77ee6ff9613f0d8aecbb
parentaf8fafcf34762898e9e19199690a1d636f5fe748 (diff)
downloadoslo-serialization-c5b869462f9b1d1a56dd98cd66c7b5f4238334ae.tar.gz
Specify namedtuple_as_object=False when using simplejson
This makes namedtuple serialization consistent with non simplejson case. Closes-Bug: #1356173 Change-Id: I9dd879914c7cb226c71e856914536318b416e488
-rw-r--r--oslo/serialization/jsonutils.py6
-rw-r--r--tests/test_jsonutils.py19
2 files changed, 23 insertions, 2 deletions
diff --git a/oslo/serialization/jsonutils.py b/oslo/serialization/jsonutils.py
index a462143..be2b097 100644
--- a/oslo/serialization/jsonutils.py
+++ b/oslo/serialization/jsonutils.py
@@ -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,10 +167,14 @@ 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)
diff --git a/tests/test_jsonutils.py b/tests/test_jsonutils.py
index 1eeb30f..42fb311 100644
--- a/tests/test_jsonutils.py
+++ b/tests/test_jsonutils.py
@@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+import collections
import datetime
import json
@@ -33,8 +34,9 @@ class JSONUtilsTestMixin(object):
def setUp(self):
super(JSONUtilsTestMixin, self).setUp()
- self.json_patcher = mock.patch.object(
- jsonutils, 'json', self.json_impl)
+ self.json_patcher = mock.patch.multiple(
+ jsonutils, json=self.json_impl,
+ is_simplejson=self.json_impl is simplejson)
self.json_impl_mock = self.json_patcher.start()
def tearDown(self):
@@ -44,6 +46,10 @@ class JSONUtilsTestMixin(object):
def test_dumps(self):
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):
expected = '{"a": "b"}'
json_dict = {'a': 'b'}
@@ -53,6 +59,15 @@ class JSONUtilsTestMixin(object):
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):
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))