Skip to content

bpo-29863: Add json.COMPACT constant #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -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``.
Expand Down Expand Up @@ -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
---------------------

Expand Down Expand Up @@ -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``.
Expand Down
5 changes: 4 additions & 1 deletion Lib/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down Expand Up @@ -99,6 +99,7 @@
__all__ = [
'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
'COMPACT',
]

__author__ = 'Bob Ippolito <bob@redivi.com>'
Expand All @@ -107,6 +108,8 @@
from .encoder import JSONEncoder
import codecs

COMPACT = (',', ':')

_default_encoder = JSONEncoder(
skipkeys=False,
ensure_ascii=True,
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_json/test_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down