summaryrefslogtreecommitdiff
path: root/oslo_serialization
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2015-09-23 14:20:33 +0200
committerVictor Stinner <vstinner@redhat.com>2015-09-23 15:23:43 +0200
commit92f2111b26d21a30e3dda62a942aa7f28ae41537 (patch)
tree7cb264460e9a44dde9ff2b3dfc80d739cc9513e0 /oslo_serialization
parentf49cc0289b2592f7e6ae336aacd05f71e28caaae (diff)
downloadoslo-serialization-92f2111b26d21a30e3dda62a942aa7f28ae41537.tar.gz
Add jsonutils.dump_as_bytes() function for py3
The jsonutils.dumps() function returns bytes on Python 2 and Unicode oon Python 3. In some cases, we always want bytes. For example, a HTTP body must be bytes. This function avoids an condition call to .encode() depending on the type or on the Python version. For example: body = jsonutils.dumps(data) if isinstance(body, six.text_type): body = body.encode('utf-8') can be replaced with: body = jsonutils.dump_as_bytes(data) Change-Id: Ib9d8f1309982762b54d8a91b1f24f64d0ae6723a
Diffstat (limited to 'oslo_serialization')
-rw-r--r--oslo_serialization/jsonutils.py22
-rw-r--r--oslo_serialization/tests/test_jsonutils.py3
2 files changed, 25 insertions, 0 deletions
diff --git a/oslo_serialization/jsonutils.py b/oslo_serialization/jsonutils.py
index 537c5d0..2677574 100644
--- a/oslo_serialization/jsonutils.py
+++ b/oslo_serialization/jsonutils.py
@@ -179,12 +179,34 @@ def dumps(obj, default=to_primitive, **kwargs):
:param kwargs: extra named parameters, please see documentation \
of `json.dumps <https://docs.python.org/2/library/json.html#basic-usage>`_
:returns: json formatted string
+
+ Use dump_as_bytes() to ensure that the result type is ``bytes`` on Python 2
+ and Python 3.
"""
if is_simplejson:
kwargs['namedtuple_as_object'] = False
return json.dumps(obj, default=default, **kwargs)
+def dump_as_bytes(obj, default=to_primitive, encoding='utf-8', **kwargs):
+ """Serialize ``obj`` to a JSON formatted ``bytes``.
+
+ :param obj: object to be serialized
+ :param default: function that returns a serializable version of an object
+ :param encoding: encoding used to encode the serialized JSON output
+ :param kwargs: extra named parameters, please see documentation \
+ of `json.dumps <https://docs.python.org/2/library/json.html#basic-usage>`_
+ :returns: json formatted string
+
+ .. versionadded:: 2.0
+ """
+ serialized = dumps(obj, default=default, **kwargs)
+ if isinstance(serialized, six.text_type):
+ # On Python 3, json.dumps() returns Unicode
+ serialized = serialized.encode(encoding)
+ return serialized
+
+
def dump(obj, fp, *args, **kwargs):
"""Serialize ``obj`` as a JSON formatted stream to ``fp``
diff --git a/oslo_serialization/tests/test_jsonutils.py b/oslo_serialization/tests/test_jsonutils.py
index 9709cb6..e2c618b 100644
--- a/oslo_serialization/tests/test_jsonutils.py
+++ b/oslo_serialization/tests/test_jsonutils.py
@@ -46,6 +46,9 @@ class JSONUtilsTestMixin(object):
def test_dumps(self):
self.assertEqual('{"a": "b"}', jsonutils.dumps({'a': 'b'}))
+ def test_dump_as_bytes(self):
+ self.assertEqual(b'{"a": "b"}', jsonutils.dump_as_bytes({'a': 'b'}))
+
def test_dumps_namedtuple(self):
n = collections.namedtuple("foo", "bar baz")(1, 2)
self.assertEqual('[1, 2]', jsonutils.dumps(n))