Skip to content

Commit d654ded

Browse files
committed
#16333: document a way to get rid of trailing whitespace when indent is used.
1 parent 7412099 commit d654ded

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

Doc/library/json.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ Compact encoding::
4242
Pretty printing::
4343

4444
>>> import json
45-
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
45+
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
46+
... indent=4, separators=(',', ': ')))
4647
{
4748
"4": 5,
4849
"6": 7
@@ -155,6 +156,12 @@ Basic Usage
155156
.. versionchanged:: 3.2
156157
Allow strings for *indent* in addition to integers.
157158

159+
.. note::
160+
161+
Since the default item separator is ``', '``, the output might include
162+
trailing whitespace when *indent* is specified. You can use
163+
``separators=(',', ': ')`` to avoid this.
164+
158165
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
159166
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
160167
':')`` is the most compact JSON representation.
@@ -393,6 +400,12 @@ Encoders and Decoders
393400
.. versionchanged:: 3.2
394401
Allow strings for *indent* in addition to integers.
395402

403+
.. note::
404+
405+
Since the default item separator is ``', '``, the output might include
406+
trailing whitespace when *indent* is specified. You can use
407+
``separators=(',', ': ')`` to avoid this.
408+
396409
If specified, *separators* should be an ``(item_separator, key_separator)``
397410
tuple. The default is ``(', ', ': ')``. To get the most compact JSON
398411
representation, you should specify ``(',', ':')`` to eliminate whitespace.

Lib/json/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
Pretty printing::
4040
4141
>>> import json
42-
>>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
43-
>>> print('\n'.join([l.rstrip() for l in s.splitlines()]))
42+
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
43+
... indent=4, separators=(',', ': ')))
4444
{
4545
"4": 5,
4646
"6": 7
@@ -146,7 +146,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
146146
If ``indent`` is a non-negative integer, then JSON array elements and
147147
object members will be pretty-printed with that indent level. An indent
148148
level of 0 will only insert newlines. ``None`` is the most compact
149-
representation.
149+
representation. Since the default item separator is ``', '``, the
150+
output might include trailing whitespace when ``indent`` is specified.
151+
You can use ``separators=(',', ': ')`` to avoid this.
150152
151153
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
152154
then it will be used instead of the default ``(', ', ': ')`` separators.
@@ -207,7 +209,9 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
207209
If ``indent`` is a non-negative integer, then JSON array elements and
208210
object members will be pretty-printed with that indent level. An indent
209211
level of 0 will only insert newlines. ``None`` is the most compact
210-
representation.
212+
representation. Since the default item separator is ``', '``, the
213+
output might include trailing whitespace when ``indent`` is specified.
214+
You can use ``separators=(',', ': ')`` to avoid this.
211215
212216
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
213217
then it will be used instead of the default ``(', ', ': ')`` separators.

Lib/json/encoder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
125125
If indent is a non-negative integer, then JSON array
126126
elements and object members will be pretty-printed with that
127127
indent level. An indent level of 0 will only insert newlines.
128-
None is the most compact representation.
128+
None is the most compact representation. Since the default
129+
item separator is ', ', the output might include trailing
130+
whitespace when indent is specified. You can use
131+
separators=(',', ': ') to avoid this.
129132
130133
If specified, separators should be a (item_separator, key_separator)
131134
tuple. The default is (', ', ': '). To get the most compact JSON

0 commit comments

Comments
 (0)