mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-09 03:44:23 +00:00
Utility to dump JSON to bytes.
This commit is contained in:
parent
4005d90024
commit
d182ba8283
@ -507,7 +507,6 @@ class JSONBytes(unittest.TestCase):
|
||||
self.assertEqual(json.loads(encoded), expected)
|
||||
self.assertEqual(jsonbytes.loads(encoded), expected)
|
||||
|
||||
|
||||
def test_encode_unicode(self):
|
||||
"""BytesJSONEncoder encodes Unicode string as usual."""
|
||||
expected = {
|
||||
@ -515,3 +514,10 @@ class JSONBytes(unittest.TestCase):
|
||||
}
|
||||
encoded = jsonbytes.dumps(expected)
|
||||
self.assertEqual(json.loads(encoded), expected)
|
||||
|
||||
def test_dumps_bytes(self):
|
||||
"""jsonbytes.dumps_bytes always returns bytes."""
|
||||
x = {u"def\N{SNOWMAN}\uFF00": 123}
|
||||
encoded = jsonbytes.dumps_bytes(x)
|
||||
self.assertIsInstance(encoded, bytes)
|
||||
self.assertEqual(json.loads(encoded, encoding="utf-8"), x)
|
||||
|
@ -9,7 +9,7 @@ from __future__ import absolute_import
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
|
||||
from future.utils import PY2
|
||||
from future.utils import PY2, PY3
|
||||
if PY2:
|
||||
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
|
||||
|
||||
@ -51,6 +51,14 @@ def dumps(obj, *args, **kwargs):
|
||||
return json.dumps(obj, cls=BytesJSONEncoder, *args, **kwargs)
|
||||
|
||||
|
||||
def dumps_bytes(obj, *args, **kwargs):
|
||||
"""Encode to JSON, then encode as bytes."""
|
||||
result = dumps(obj, *args, **kwargs)
|
||||
if PY3:
|
||||
result = result.encode("utf-8")
|
||||
return result
|
||||
|
||||
|
||||
# To make this module drop-in compatible with json module:
|
||||
loads = json.loads
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user