Skip to content

Commit 1003144

Browse files
committed
#16333: use (",", ": ") as default separator when indent is specified to avoid trailing whitespace. Patch by Serhiy Storchaka.
1 parent d966c21 commit 1003144

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

Doc/library/json.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,13 @@ Basic Usage
155155
.. versionchanged:: 3.2
156156
Allow strings for *indent* in addition to integers.
157157

158-
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
159-
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
160-
':')`` is the most compact JSON representation.
158+
If specified, *separators* should be an ``(item_separator, key_separator)``
159+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
160+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
161+
you should specify ``(',', ':')`` to eliminate whitespace.
162+
163+
.. versionchanged:: 3.4
164+
Use ``(',', ': ')`` as default if *indent* is not ``None``.
161165

162166
*default(obj)* is a function that should return a serializable version of
163167
*obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
@@ -394,8 +398,12 @@ Encoders and Decoders
394398
Allow strings for *indent* in addition to integers.
395399

396400
If specified, *separators* should be an ``(item_separator, key_separator)``
397-
tuple. The default is ``(', ', ': ')``. To get the most compact JSON
398-
representation, you should specify ``(',', ':')`` to eliminate whitespace.
401+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
402+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
403+
you should specify ``(',', ':')`` to eliminate whitespace.
404+
405+
.. versionchanged:: 3.4
406+
Use ``(',', ': ')`` as default if *indent* is not ``None``.
399407

400408
If specified, *default* is a function that gets called for objects that can't
401409
otherwise be serialized. It should return a JSON encodable version of the

Lib/json/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
148148
level of 0 will only insert newlines. ``None`` is the most compact
149149
representation.
150150
151-
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
152-
then it will be used instead of the default ``(', ', ': ')`` separators.
153-
``(',', ':')`` is the most compact JSON representation.
151+
If specified, ``separators`` should be an ``(item_separator, key_separator)``
152+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
153+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
154+
you should specify ``(',', ':')`` to eliminate whitespace.
154155
155156
``default(obj)`` is a function that should return a serializable version
156157
of obj or raise TypeError. The default simply raises TypeError.
@@ -209,9 +210,10 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
209210
level of 0 will only insert newlines. ``None`` is the most compact
210211
representation.
211212
212-
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
213-
then it will be used instead of the default ``(', ', ': ')`` separators.
214-
``(',', ':')`` is the most compact JSON representation.
213+
If specified, ``separators`` should be an ``(item_separator, key_separator)``
214+
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
215+
``(',', ': ')`` otherwise. To get the most compact JSON representation,
216+
you should specify ``(',', ':')`` to eliminate whitespace.
215217
216218
``default(obj)`` is a function that should return a serializable version
217219
of obj or raise TypeError. The default simply raises TypeError.

Lib/json/encoder.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
127127
indent level. An indent level of 0 will only insert newlines.
128128
None is the most compact representation.
129129
130-
If specified, separators should be a (item_separator, key_separator)
131-
tuple. The default is (', ', ': '). To get the most compact JSON
132-
representation you should specify (',', ':') to eliminate whitespace.
130+
If specified, separators should be an (item_separator, key_separator)
131+
tuple. The default is (', ', ': ') if *indent* is ``None`` and
132+
(',', ': ') otherwise. To get the most compact JSON representation,
133+
you should specify (',', ':') to eliminate whitespace.
133134
134135
If specified, default is a function that gets called for objects
135136
that can't otherwise be serialized. It should return a JSON encodable
@@ -145,6 +146,8 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
145146
self.indent = indent
146147
if separators is not None:
147148
self.item_separator, self.key_separator = separators
149+
elif indent is not None:
150+
self.item_separator = ','
148151
if default is not None:
149152
self.default = default
150153

Lib/test/json_tests/test_indent.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ def test_indent(self):
3232
d1 = self.dumps(h)
3333
d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
3434
d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
35+
d4 = self.dumps(h, indent=2, sort_keys=True)
36+
d5 = self.dumps(h, indent='\t', sort_keys=True)
3537

3638
h1 = self.loads(d1)
3739
h2 = self.loads(d2)
@@ -42,6 +44,8 @@ def test_indent(self):
4244
self.assertEqual(h3, h)
4345
self.assertEqual(d2, expect.expandtabs(2))
4446
self.assertEqual(d3, expect)
47+
self.assertEqual(d4, d2)
48+
self.assertEqual(d5, d3)
4549

4650
def test_indent0(self):
4751
h = {3: 1}

0 commit comments

Comments
 (0)