diff options
author | Raymond Hettinger <python@rcn.com> | 2010-10-31 08:00:16 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-10-31 08:00:16 (GMT) |
commit | b643ef8f8e92728c2a4afaaa860592e384c7178c (patch) | |
tree | 213adcc2519d529203480cf3ec7809943f3e35dd /Lib | |
parent | 6ff2a7d12166c3e6c3418fa215ba27aa31981304 (diff) | |
download | cpython-b643ef8f8e92728c2a4afaaa860592e384c7178c.zip cpython-b643ef8f8e92728c2a4afaaa860592e384c7178c.tar.gz cpython-b643ef8f8e92728c2a4afaaa860592e384c7178c.tar.bz2 |
Issue #5729: json.dumps to support using '\t' as an indent string
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/json/encoder.py | 11 | ||||
-rw-r--r-- | Lib/json/tests/test_indent.py | 40 |
2 files changed, 29 insertions, 22 deletions
diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 1335985..7475f5a 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -259,6 +259,9 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, 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 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, 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 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, 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 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, 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 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, 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] diff --git a/Lib/json/tests/test_indent.py b/Lib/json/tests/test_indent.py index 6055162..ddf88cd 100644 --- a/Lib/json/tests/test_indent.py +++ b/Lib/json/tests/test_indent.py @@ -10,32 +10,36 @@ class TestIndent(TestCase): 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) |