``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. ``(',',
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}')