]> granicus.if.org Git - python/commitdiff
#16333: use (",", ": ") as default separator when indent is specified to avoid traili...
authorEzio Melotti <ezio.melotti@gmail.com>
Wed, 28 Nov 2012 22:42:56 +0000 (00:42 +0200)
committerEzio Melotti <ezio.melotti@gmail.com>
Wed, 28 Nov 2012 22:42:56 +0000 (00:42 +0200)
Doc/library/json.rst
Lib/json/__init__.py
Lib/json/encoder.py
Lib/test/json_tests/test_indent.py

index f9547cb3587f99bb0262712cf6a9d0158565d4b0..25a33587ae56b60f17e439f2d7a546f230183896 100644 (file)
@@ -155,9 +155,13 @@ Basic Usage
    .. versionchanged:: 3.2
       Allow strings for *indent* in addition to integers.
 
-   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.
+   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.
+
+   .. versionchanged:: 3.4
+      Use ``(',', ': ')`` as default if *indent* is not ``None``.
 
    *default(obj)* is a function that should return a serializable version of
    *obj* or raise :exc:`TypeError`.  The default simply raises :exc:`TypeError`.
@@ -394,8 +398,12 @@ Encoders and Decoders
       Allow strings for *indent* in addition to integers.
 
    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.
+   tuple.  The default is ``(', ', ': ')`` if *indent* is ``None`` and
+   ``(',', ': ')`` otherwise.  To get the most compact JSON representation,
+   you should specify ``(',', ':')`` to eliminate whitespace.
+
+   .. versionchanged:: 3.4
+      Use ``(',', ': ')`` as default if *indent* is not ``None``.
 
    If specified, *default* is a function that gets called for objects that can't
    otherwise be serialized.  It should return a JSON encodable version of the
index 86a7a3e50a6fca936ae76385510b3713dcc987d2..7314cb4096a6cca2acdb0a2866f2cb8bdbb026a5 100644 (file)
@@ -148,9 +148,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
     level of 0 will only insert newlines. ``None`` is the most compact
     representation.
 
-    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.
+    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.
 
     ``default(obj)`` is a function that should return a serializable version
     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,
     level of 0 will only insert newlines. ``None`` is the most compact
     representation.
 
-    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.
+    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.
 
     ``default(obj)`` is a function that should return a serializable version
     of obj or raise TypeError. The default simply raises TypeError.
index ba57c2c25eeed92d4b328d8823bb5a4bc3d556f0..93b5ea7d5e046baef9c81571d644812028dcfe5f 100644 (file)
@@ -127,9 +127,10 @@ class JSONEncoder(object):
         indent level.  An indent level of 0 will only insert newlines.
         None is the most compact representation.
 
-        If specified, separators should be a (item_separator, key_separator)
-        tuple.  The default is (', ', ': ').  To get the most compact JSON
-        representation you should specify (',', ':') to eliminate whitespace.
+        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.
 
         If specified, default is a function that gets called for objects
         that can't otherwise be serialized.  It should return a JSON encodable
@@ -145,6 +146,8 @@ class JSONEncoder(object):
         self.indent = indent
         if separators is not None:
             self.item_separator, self.key_separator = separators
+        elif indent is not None:
+            self.item_separator = ','
         if default is not None:
             self.default = default
 
index 4c706463396d315978c28d19932ec0f979535312..4eb4f892d0040d792ac95295733970e6440eb070 100644 (file)
@@ -32,6 +32,8 @@ class TestIndent:
         d1 = self.dumps(h)
         d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
         d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
+        d4 = self.dumps(h, indent=2, sort_keys=True)
+        d5 = self.dumps(h, indent='\t', sort_keys=True)
 
         h1 = self.loads(d1)
         h2 = self.loads(d2)
@@ -42,6 +44,8 @@ class TestIndent:
         self.assertEqual(h3, h)
         self.assertEqual(d2, expect.expandtabs(2))
         self.assertEqual(d3, expect)
+        self.assertEqual(d4, d2)
+        self.assertEqual(d5, d3)
 
     def test_indent0(self):
         h = {3: 1}