[Python-checkins] cpython (3.2): #16333: document a way to get rid of trailing whitespace when indent is used.

ezio.melotti python-checkins at python.org
Wed Nov 28 23:43:15 CET 2012


http://hg.python.org/cpython/rev/2a5b183ac3cd
changeset:   80628:2a5b183ac3cd
branch:      3.2
parent:      80624:c008f070f88a
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Thu Nov 29 00:35:29 2012 +0200
summary:
  #16333: document a way to get rid of trailing whitespace when indent is used.

files:
  Doc/library/json.rst |  15 ++++++++++++++-
  Lib/json/__init__.py |  12 ++++++++----
  Lib/json/encoder.py  |   5 ++++-
  3 files changed, 26 insertions(+), 6 deletions(-)


diff --git a/Doc/library/json.rst b/Doc/library/json.rst
--- a/Doc/library/json.rst
+++ b/Doc/library/json.rst
@@ -42,7 +42,8 @@
 Pretty printing::
 
     >>> import json
-    >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
+    >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
+    ...                  indent=4, separators=(',', ': ')))
     {
         "4": 5,
         "6": 7
@@ -155,6 +156,12 @@
    .. versionchanged:: 3.2
       Allow strings for *indent* in addition to integers.
 
+   .. note::
+
+      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.  ``(',',
    ':')`` is the most compact JSON representation.
@@ -393,6 +400,12 @@
    .. versionchanged:: 3.2
       Allow strings for *indent* in addition to integers.
 
+   .. note::
+
+      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 an ``(item_separator, key_separator)``
    tuple.  The default is ``(', ', ': ')``.  To get the most compact JSON
    representation, you should specify ``(',', ':')`` to eliminate whitespace.
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
--- a/Lib/json/__init__.py
+++ b/Lib/json/__init__.py
@@ -39,8 +39,8 @@
 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 @@
     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 @@
     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
--- a/Lib/json/encoder.py
+++ b/Lib/json/encoder.py
@@ -125,7 +125,10 @@
         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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list