[Python-checkins] r86022 - in python/branches/py3k: Doc/library/json.rst Lib/json/encoder.py Lib/json/tests/test_indent.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Sun Oct 31 09:00:17 CET 2010


Author: raymond.hettinger
Date: Sun Oct 31 09:00:16 2010
New Revision: 86022

Log:
Issue #5729: json.dumps to support using '\t' as an indent string

Modified:
   python/branches/py3k/Doc/library/json.rst
   python/branches/py3k/Lib/json/encoder.py
   python/branches/py3k/Lib/json/tests/test_indent.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Doc/library/json.rst
==============================================================================
--- python/branches/py3k/Doc/library/json.rst	(original)
+++ python/branches/py3k/Doc/library/json.rst	Sun Oct 31 09:00:16 2010
@@ -135,10 +135,12 @@
    ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
    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.
+   If *indent* is a non-negative integer or string, then JSON array elements and
+   object members will be pretty-printed with that indent level.  An indent level
+   of 0 or ``""`` will only insert newlines.  ``None`` (the default) selects the
+   most compact representation. Using an integer indent indents that many spaces
+   per level.  If *indent* is a string (such at '\t'), that string is used to indent
+   each level.
 
    If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
    will be used instead of the default ``(', ', ': ')`` separators.  ``(',',

Modified: python/branches/py3k/Lib/json/encoder.py
==============================================================================
--- python/branches/py3k/Lib/json/encoder.py	(original)
+++ python/branches/py3k/Lib/json/encoder.py	Sun Oct 31 09:00:16 2010
@@ -259,6 +259,9 @@
         tuple=tuple,
     ):
 
+    if _indent is not None and not isinstance(_indent, str):
+        _indent = ' ' * _indent
+
     def _iterencode_list(lst, _current_indent_level):
         if not lst:
             yield '[]'
@@ -271,7 +274,7 @@
         buf = '['
         if _indent is not None:
             _current_indent_level += 1
-            newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
+            newline_indent = '\n' + _indent * _current_indent_level
             separator = _item_separator + newline_indent
             buf += newline_indent
         else:
@@ -307,7 +310,7 @@
                     yield chunk
         if newline_indent is not None:
             _current_indent_level -= 1
-            yield '\n' + (' ' * (_indent * _current_indent_level))
+            yield '\n' + _indent * _current_indent_level
         yield ']'
         if markers is not None:
             del markers[markerid]
@@ -324,7 +327,7 @@
         yield '{'
         if _indent is not None:
             _current_indent_level += 1
-            newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
+            newline_indent = '\n' + _indent * _current_indent_level
             item_separator = _item_separator + newline_indent
             yield newline_indent
         else:
@@ -383,7 +386,7 @@
                     yield chunk
         if newline_indent is not None:
             _current_indent_level -= 1
-            yield '\n' + (' ' * (_indent * _current_indent_level))
+            yield '\n' + _indent * _current_indent_level
         yield '}'
         if markers is not None:
             del markers[markerid]

Modified: python/branches/py3k/Lib/json/tests/test_indent.py
==============================================================================
--- python/branches/py3k/Lib/json/tests/test_indent.py	(original)
+++ python/branches/py3k/Lib/json/tests/test_indent.py	Sun Oct 31 09:00:16 2010
@@ -10,32 +10,36 @@
 
         expect = textwrap.dedent("""\
         [
-          [
-            "blorpie"
-          ],
-          [
-            "whoops"
-          ],
-          [],
-          "d-shtaeou",
-          "d-nthiouh",
-          "i-vhbjkhnth",
-          {
-            "nifty": 87
-          },
-          {
-            "field": "yes",
-            "morefield": false
-          }
+        \t[
+        \t\t"blorpie"
+        \t],
+        \t[
+        \t\t"whoops"
+        \t],
+        \t[],
+        \t"d-shtaeou",
+        \t"d-nthiouh",
+        \t"i-vhbjkhnth",
+        \t{
+        \t\t"nifty": 87
+        \t},
+        \t{
+        \t\t"field": "yes",
+        \t\t"morefield": false
+        \t}
         ]""")
 
 
         d1 = json.dumps(h)
         d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
+        d3 = json.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
 
         h1 = json.loads(d1)
         h2 = json.loads(d2)
+        h3 = json.loads(d3)
 
         self.assertEquals(h1, h)
         self.assertEquals(h2, h)
-        self.assertEquals(d2, expect)
+        self.assertEquals(h3, h)
+        self.assertEquals(d2, expect.expandtabs(2))
+        self.assertEquals(d3, expect)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Oct 31 09:00:16 2010
@@ -59,6 +59,9 @@
 Library
 -------
 
+- Issue #5729:  json.dumps() now supports using a string such as '\t'
+  for pretty-printing multilevel objects.
+
 - Issue #10253: FileIO leaks a file descriptor when trying to open a file
   for append that isn't seekable.  Patch by Brian Brazil.
 


More information about the Python-checkins mailing list