diff --git a/Doc/library/json.rst b/Doc/library/json.rst index 8103c614aaf450..e4ccc8095ba02f 100644 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -43,7 +43,7 @@ Encoding basic Python object hierarchies:: Compact encoding:: >>> import json - >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':')) + >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=json.COMPACT) '[1,2,3,{"4":5,"6":7}]' Pretty printing:: @@ -170,7 +170,7 @@ Basic Usage If specified, *separators* should be an ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON representation, - you should specify ``(',', ':')`` to eliminate whitespace. + you should specify :attr:`json.COMPACT` to eliminate whitespace. .. versionchanged:: 3.4 Use ``(',', ': ')`` as default if *indent* is not ``None``. @@ -283,6 +283,17 @@ Basic Usage input encoding should be UTF-8, UTF-16 or UTF-32. +Constants +^^^^^^^^^ + +.. data:: COMPACT + + A constant that can be used as the *separators* argument + to emit a compact serialization. + + .. versionadded:: 3.7 + + Encoders and Decoders --------------------- @@ -448,7 +459,7 @@ Encoders and Decoders If specified, *separators* should be an ``(item_separator, key_separator)`` tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and ``(',', ': ')`` otherwise. To get the most compact JSON representation, - you should specify ``(',', ':')`` to eliminate whitespace. + you should specify :attr:`json.COMPACT` to eliminate whitespace. .. versionchanged:: 3.4 Use ``(',', ': ')`` as default if *indent* is not ``None``. diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index b8d5e6cff8cb21..0b7037619dfa2d 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -30,7 +30,7 @@ >>> import json >>> from collections import OrderedDict >>> mydict = OrderedDict([('4', 5), ('6', 7)]) - >>> json.dumps([1,2,3,mydict], separators=(',', ':')) + >>> json.dumps([1,2,3,mydict], separators=json.COMPACT) '[1,2,3,{"4":5,"6":7}]' Pretty printing:: @@ -99,6 +99,7 @@ __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', + 'COMPACT', ] __author__ = 'Bob Ippolito ' @@ -107,6 +108,8 @@ from .encoder import JSONEncoder import codecs +COMPACT = (',', ':') + _default_encoder = JSONEncoder( skipkeys=False, ensure_ascii=True, diff --git a/Lib/test/test_json/test_dump.py b/Lib/test/test_json/test_dump.py index fd0d86b392cee9..cdf0de18e612e8 100644 --- a/Lib/test/test_json/test_dump.py +++ b/Lib/test/test_json/test_dump.py @@ -47,6 +47,15 @@ def __lt__(self, o): d[1337] = "true.dat" self.assertEqual(self.dumps(d, sort_keys=True), '{"1337": "true.dat"}') + def test_compact_dump(self): + sio = StringIO() + self.json.dump({'name': 'some name', 'value': 'some value'}, sio, separators=self.json.COMPACT) + self.assertEqual(sio.getvalue(), '{"name":"some name","value":"some value"}') + + def test_compact_encode(self): + encoder = self.json.JSONEncoder(separators=self.json.COMPACT) + encoded = encoder.encode({'name': 'some name', 'value': 'some value'}) + self.assertEqual(encoded, '{"name":"some name","value":"some value"}') class TestPyDump(TestDump, PyTest): pass