Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b643ef8

Browse files
committed
Issue #5729: json.dumps to support using '\t' as an indent string
1 parent 6ff2a7d commit b643ef8

4 files changed

Lines changed: 38 additions & 26 deletions

File tree

Doc/library/json.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ Basic Usage
135135
``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
136136
using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
137137

138-
If *indent* is a non-negative integer, then JSON array elements and object
139-
members will be pretty-printed with that indent level. An indent level of 0
140-
will only insert newlines. ``None`` (the default) selects the most compact
141-
representation.
138+
If *indent* is a non-negative integer or string, then JSON array elements and
139+
object members will be pretty-printed with that indent level. An indent level
140+
of 0 or ``""`` will only insert newlines. ``None`` (the default) selects the
141+
most compact representation. Using an integer indent indents that many spaces
142+
per level. If *indent* is a string (such at '\t'), that string is used to indent
143+
each level.
142144

143145
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
144146
will be used instead of the default ``(', ', ': ')`` separators. ``(',',

Lib/json/encoder.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
259259
tuple=tuple,
260260
):
261261

262+
if _indent is not None and not isinstance(_indent, str):
263+
_indent = ' ' * _indent
264+
262265
def _iterencode_list(lst, _current_indent_level):
263266
if not lst:
264267
yield '[]'
@@ -271,7 +274,7 @@ def _iterencode_list(lst, _current_indent_level):
271274
buf = '['
272275
if _indent is not None:
273276
_current_indent_level += 1
274-
newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
277+
newline_indent = '\n' + _indent * _current_indent_level
275278
separator = _item_separator + newline_indent
276279
buf += newline_indent
277280
else:
@@ -307,7 +310,7 @@ def _iterencode_list(lst, _current_indent_level):
307310
yield chunk
308311
if newline_indent is not None:
309312
_current_indent_level -= 1
310-
yield '\n' + (' ' * (_indent * _current_indent_level))
313+
yield '\n' + _indent * _current_indent_level
311314
yield ']'
312315
if markers is not None:
313316
del markers[markerid]
@@ -324,7 +327,7 @@ def _iterencode_dict(dct, _current_indent_level):
324327
yield '{'
325328
if _indent is not None:
326329
_current_indent_level += 1
327-
newline_indent = '\n' + (' ' * (_indent * _current_indent_level))
330+
newline_indent = '\n' + _indent * _current_indent_level
328331
item_separator = _item_separator + newline_indent
329332
yield newline_indent
330333
else:
@@ -383,7 +386,7 @@ def _iterencode_dict(dct, _current_indent_level):
383386
yield chunk
384387
if newline_indent is not None:
385388
_current_indent_level -= 1
386-
yield '\n' + (' ' * (_indent * _current_indent_level))
389+
yield '\n' + _indent * _current_indent_level
387390
yield '}'
388391
if markers is not None:
389392
del markers[markerid]

Lib/json/tests/test_indent.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,36 @@ def test_indent(self):
1010

1111
expect = textwrap.dedent("""\
1212
[
13-
[
14-
"blorpie"
15-
],
16-
[
17-
"whoops"
18-
],
19-
[],
20-
"d-shtaeou",
21-
"d-nthiouh",
22-
"i-vhbjkhnth",
23-
{
24-
"nifty": 87
25-
},
26-
{
27-
"field": "yes",
28-
"morefield": false
29-
}
13+
\t[
14+
\t\t"blorpie"
15+
\t],
16+
\t[
17+
\t\t"whoops"
18+
\t],
19+
\t[],
20+
\t"d-shtaeou",
21+
\t"d-nthiouh",
22+
\t"i-vhbjkhnth",
23+
\t{
24+
\t\t"nifty": 87
25+
\t},
26+
\t{
27+
\t\t"field": "yes",
28+
\t\t"morefield": false
29+
\t}
3030
]""")
3131

3232

3333
d1 = json.dumps(h)
3434
d2 = json.dumps(h, indent=2, sort_keys=True, separators=(',', ': '))
35+
d3 = json.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))
3536

3637
h1 = json.loads(d1)
3738
h2 = json.loads(d2)
39+
h3 = json.loads(d3)
3840

3941
self.assertEquals(h1, h)
4042
self.assertEquals(h2, h)
41-
self.assertEquals(d2, expect)
43+
self.assertEquals(h3, h)
44+
self.assertEquals(d2, expect.expandtabs(2))
45+
self.assertEquals(d3, expect)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Core and Builtins
5959
Library
6060
-------
6161

62+
- Issue #5729: json.dumps() now supports using a string such as '\t'
63+
for pretty-printing multilevel objects.
64+
6265
- Issue #10253: FileIO leaks a file descriptor when trying to open a file
6366
for append that isn't seekable. Patch by Brian Brazil.
6467

0 commit comments

Comments
 (0)