]> granicus.if.org Git - python/commitdiff
#10019: Fix regression relative to 2.6: add newlines if indent=0
authorR David Murray <rdmurray@bitdance.com>
Wed, 13 Apr 2011 01:00:26 +0000 (21:00 -0400)
committerR David Murray <rdmurray@bitdance.com>
Wed, 13 Apr 2011 01:00:26 +0000 (21:00 -0400)
Patch by Amaury Forgeot d'Arc, updated by Sando Tosi.

Doc/library/json.rst
Lib/json/encoder.py
Lib/json/tests/test_indent.py
Misc/NEWS

index be54eec524e4a658fba1ca2f2d4023ca0eec97e7..6c84f94241a403566d85311df9ee512f0e7adcaa 100644 (file)
@@ -139,9 +139,9 @@ Basic Usage
    using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
 
    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`` (the default) selects the most compact
-   representation.
+   members will be pretty-printed with that indent level.  An indent level of 0,
+   or negative, will only insert newlines.  ``None`` (the default) selects the
+   most compact representation.
 
    If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
    will be used instead of the default ``(', ', ': ')`` separators.  ``(',',
index d8692c4881fd5254145ee7b93b953aa09f56816f..906c46216aa99d77062f114e25940bd9952afb85 100644 (file)
@@ -251,7 +251,7 @@ class JSONEncoder(object):
 
 
         if (_one_shot and c_make_encoder is not None
-                and not self.indent and not self.sort_keys):
+                and self.indent is None and not self.sort_keys):
             _iterencode = c_make_encoder(
                 markers, self.default, _encoder, self.indent,
                 self.key_separator, self.item_separator, self.sort_keys,
index cd608d94a57cdb436400f665914bff76fa5d383a..64b9b9c604cdc88832729d0139edc9c3ea20cb0b 100644 (file)
@@ -2,6 +2,7 @@ from unittest import TestCase
 
 import json
 import textwrap
+from StringIO import StringIO
 
 class TestIndent(TestCase):
     def test_indent(self):
@@ -39,3 +40,18 @@ class TestIndent(TestCase):
         self.assertEqual(h1, h)
         self.assertEqual(h2, h)
         self.assertEqual(d2, expect)
+
+    def test_indent0(self):
+        h = {3: 1}
+        def check(indent, expected):
+            d1 = json.dumps(h, indent=indent)
+            self.assertEqual(d1, expected)
+
+            sio = StringIO()
+            json.dump(h, sio, indent=indent)
+            self.assertEqual(sio.getvalue(), expected)
+
+        # indent=0 should emit newlines
+        check(0, '{\n"3": 1\n}')
+        # indent=None is more compact
+        check(None, '{"3": 1}')
index 63f4b485b46ed9b918807fa4c83c734c5bb057c1..3e83823e4bde386ca3f05bd2d03f3ff4cff79994 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -51,6 +51,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10019: Fixed regression in json module where an indent of 0 stopped
+  adding newlines and acted instead like 'None'.
+
 - Issue #5162: Treat services like frozen executables to allow child spawning
   from multiprocessing.forking on Windows.