summaryrefslogtreecommitdiffstats
path: root/Lib/json
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2012-11-28 22:35:29 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2012-11-28 22:35:29 (GMT)
commitd654dedbbb1116bfed989aafd2ee7a004828546b (patch)
tree14213ed055151133efc364b0c94294c3ac4d4fd3 /Lib/json
parent74120996f54b53c26b0682406647a2f8bdf53e8b (diff)
downloadcpython-d654dedbbb1116bfed989aafd2ee7a004828546b.zip
cpython-d654dedbbb1116bfed989aafd2ee7a004828546b.tar.gz
cpython-d654dedbbb1116bfed989aafd2ee7a004828546b.tar.bz2
#16333: document a way to get rid of trailing whitespace when indent is used.
Diffstat (limited to 'Lib/json')
-rw-r--r--Lib/json/__init__.py12
-rw-r--r--Lib/json/encoder.py5
2 files changed, 12 insertions, 5 deletions
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
index 86a7a3e..44f49c4 100644
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -39,8 +39,8 @@ Compact encoding::
Pretty printing::
>>> import json
- >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
- >>> print('\n'.join([l.rstrip() for l in s.splitlines()]))
+ >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
+ ... indent=4, separators=(',', ': ')))
{
"4": 5,
"6": 7
@@ -146,7 +146,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
- representation.
+ representation. Since the default item separator is ``', '``, the
+ output might include trailing whitespace when ``indent`` is specified.
+ You can use ``separators=(',', ': ')`` to avoid this.
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
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,
If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
- representation.
+ representation. Since the default item separator is ``', '``, the
+ output might include trailing whitespace when ``indent`` is specified.
+ You can use ``separators=(',', ': ')`` to avoid this.
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py
index 75b7f49..e1ed21f 100644
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -125,7 +125,10 @@ class JSONEncoder(object):
If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
indent level. An indent level of 0 will only insert newlines.
- None is the most compact representation.
+ None is the most compact representation. Since the default
+ item separator is ', ', the output might include trailing
+ whitespace when indent is specified. You can use
+ separators=(',', ': ') to avoid this.
If specified, separators should be a (item_separator, key_separator)
tuple. The default is (', ', ': '). To get the most compact JSON