From: R David Murray Date: Wed, 13 Apr 2011 01:09:18 +0000 (-0400) Subject: Merge #10019: Fix regression relative to 2.6: add newlines if indent=0 X-Git-Tag: v3.2.1b1~140^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5315482e92d1f3c47460999a790df684bf8b09e;p=python Merge #10019: Fix regression relative to 2.6: add newlines if indent=0 Patch by Amaury Forgeot d'Arc, updated by Sando Tosi. --- d5315482e92d1f3c47460999a790df684bf8b09e diff --cc Doc/library/json.rst index 6bb7fe3a64,2bf242fff4..9064409f98 --- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@@ -134,12 -134,10 +134,12 @@@ Basic Usag ``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, - or negative, 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. ++ of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) ++ selects the most compact representation. Using a positive 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. ``(',', diff --cc Lib/test/json_tests/test_indent.py index d8030aaade,d45aa851bd..692a494cfe --- a/Lib/test/json_tests/test_indent.py +++ b/Lib/test/json_tests/test_indent.py @@@ -40,6 -39,19 +41,21 @@@ class TestIndent(TestCase) self.assertEqual(h1, h) self.assertEqual(h2, h) - self.assertEqual(d2, expect) + self.assertEqual(h3, h) + self.assertEqual(d2, expect.expandtabs(2)) + self.assertEqual(d3, 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}')