Merge "Fix serialization of binary strings in Python3"
This commit is contained in:
commit
1f86a66a36
@ -52,12 +52,12 @@ _nasty_type_tests = [inspect.ismodule, inspect.isclass, inspect.ismethod,
|
||||
inspect.iscode, inspect.isbuiltin, inspect.isroutine,
|
||||
inspect.isabstract]
|
||||
|
||||
_simple_types = (six.string_types + six.integer_types
|
||||
_simple_types = ((six.text_type,) + six.integer_types
|
||||
+ (type(None), bool, float))
|
||||
|
||||
|
||||
def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
level=0, max_depth=3):
|
||||
level=0, max_depth=3, encoding='utf-8'):
|
||||
"""Convert a complex object into primitives.
|
||||
|
||||
Handy for JSON serialization. We can optionally handle instances,
|
||||
@ -92,6 +92,11 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
if isinstance(value, _simple_types):
|
||||
return value
|
||||
|
||||
if isinstance(value, six.binary_type):
|
||||
if six.PY3:
|
||||
value = value.decode(encoding=encoding)
|
||||
return value
|
||||
|
||||
# It's not clear why xmlrpclib created their own DateTime type, but
|
||||
# for our purposes, make it a datetime type which is explicitly
|
||||
# handled
|
||||
@ -141,7 +146,8 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
||||
convert_instances=convert_instances,
|
||||
convert_datetime=convert_datetime,
|
||||
level=level,
|
||||
max_depth=max_depth)
|
||||
max_depth=max_depth,
|
||||
encoding=encoding)
|
||||
if isinstance(value, dict):
|
||||
return {recursive(k): recursive(v)
|
||||
for k, v in six.iteritems(value)}
|
||||
|
@ -113,6 +113,9 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
||||
super(ToPrimitiveTestCase, self).setUp()
|
||||
self.trans_fixture = self.useFixture(fixture.Translation())
|
||||
|
||||
def test_bytes(self):
|
||||
self.assertEqual(jsonutils.to_primitive(b'abc'), 'abc')
|
||||
|
||||
def test_list(self):
|
||||
self.assertEqual([1, 2, 3], jsonutils.to_primitive([1, 2, 3]))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user