Skip to content

Commit d531548

Browse files
committed
Merge #10019: Fix regression relative to 2.6: add newlines if indent=0
Patch by Amaury Forgeot d'Arc, updated by Sando Tosi.
2 parents 62f8bcb + 3dd02d6 commit d531548

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

Doc/library/json.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ Basic Usage
136136

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

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

Lib/json/encoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def floatstr(o, allow_nan=self.allow_nan,
233233

234234

235235
if (_one_shot and c_make_encoder is not None
236-
and not self.indent):
236+
and self.indent is None):
237237
_iterencode = c_make_encoder(
238238
markers, self.default, _encoder, self.indent,
239239
self.key_separator, self.item_separator, self.sort_keys,

Lib/test/json_tests/test_indent.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import textwrap
5+
from io import StringIO
56

67
class TestIndent(TestCase):
78
def test_indent(self):
@@ -43,3 +44,18 @@ def test_indent(self):
4344
self.assertEqual(h3, h)
4445
self.assertEqual(d2, expect.expandtabs(2))
4546
self.assertEqual(d3, expect)
47+
48+
def test_indent0(self):
49+
h = {3: 1}
50+
def check(indent, expected):
51+
d1 = json.dumps(h, indent=indent)
52+
self.assertEqual(d1, expected)
53+
54+
sio = StringIO()
55+
json.dump(h, sio, indent=indent)
56+
self.assertEqual(sio.getvalue(), expected)
57+
58+
# indent=0 should emit newlines
59+
check(0, '{\n"3": 1\n}')
60+
# indent=None is more compact
61+
check(None, '{"3": 1}')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Core and Builtins
5353
Library
5454
-------
5555

56+
- Issue #10019: Fixed regression in json module where an indent of 0 stopped
57+
adding newlines and acted instead like 'None'.
58+
5659
- Issue #5162: Treat services like frozen executables to allow child spawning
5760
from multiprocessing.forking on Windows.
5861

0 commit comments

Comments
 (0)