summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-02-16 18:29:01 +0000
committerGerrit Code Review <review@openstack.org>2015-02-16 18:29:01 +0000
commit530fea24cc2aaaf01f5c079b972813635c53772d (patch)
tree26821a5fc42a1014d5cc05c0b274076113372992
parent4bfc83e0b0e73d7b2c7fe81616169274c1e5b06a (diff)
parent22cea10c38d4b76569426a90da9c2876877b1e29 (diff)
downloadoslo-serialization-530fea24cc2aaaf01f5c079b972813635c53772d.tar.gz
Merge "jsonutils: add set() tests and simplify recursive code"1.3.0
-rw-r--r--oslo_serialization/jsonutils.py11
-rw-r--r--tests/test_jsonutils.py7
2 files changed, 11 insertions, 7 deletions
diff --git a/oslo_serialization/jsonutils.py b/oslo_serialization/jsonutils.py
index c4724c0..8dba684 100644
--- a/oslo_serialization/jsonutils.py
+++ b/oslo_serialization/jsonutils.py
@@ -140,8 +140,6 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
max_depth=max_depth)
if isinstance(value, dict):
return dict((k, recursive(v)) for k, v in six.iteritems(value))
- elif isinstance(value, (list, tuple)):
- return [recursive(lv) for lv in value]
# It's not clear why xmlrpclib created their own DateTime type, but
# for our purposes, make it a datetime type which is explicitly
@@ -154,17 +152,16 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
elif hasattr(value, 'iteritems'):
return recursive(dict(value.iteritems()), level=level + 1)
elif hasattr(value, '__iter__'):
- return recursive(list(value))
+ return list(map(recursive, value))
elif convert_instances and hasattr(value, '__dict__'):
# Likely an instance of something. Watch for cycles.
# Ignore class member vars.
return recursive(value.__dict__, level=level + 1)
elif netaddr and isinstance(value, netaddr.IPAddress):
return six.text_type(value)
- else:
- if any(test(value) for test in _nasty_type_tests):
- return six.text_type(value)
- return value
+ elif any(test(value) for test in _nasty_type_tests):
+ return six.text_type(value)
+ return value
except TypeError:
# Class objects are tricky since they may define something like
# __iter__ defined but it isn't callable as list().
diff --git a/tests/test_jsonutils.py b/tests/test_jsonutils.py
index 34f0616..e41b66e 100644
--- a/tests/test_jsonutils.py
+++ b/tests/test_jsonutils.py
@@ -74,6 +74,10 @@ class JSONUtilsTestMixin(object):
jsonutils.dumps(
uuid.UUID("87edfaf49bff11e482bdb7b4e88d3780")))
+ def test_dump_set(self):
+ # Only test with one entry because the order is random :]
+ self.assertEqual("[1]", jsonutils.dumps(set([1])))
+
def test_loads(self):
self.assertEqual({'a': 'b'}, jsonutils.loads('{"a": "b"}'))
@@ -123,6 +127,9 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
def test_list(self):
self.assertEqual(jsonutils.to_primitive([1, 2, 3]), [1, 2, 3])
+ def test_set(self):
+ self.assertEqual(jsonutils.to_primitive(set([1, 2, 3])), [1, 2, 3])
+
def test_empty_list(self):
self.assertEqual(jsonutils.to_primitive([]), [])