diff -r aac6b313ef5f Doc/library/json.rst --- a/Doc/library/json.rst Sat Nov 24 20:42:59 2012 +0100 +++ b/Doc/library/json.rst Sun Nov 25 18:59:31 2012 +0200 @@ -109,8 +109,8 @@ JSON is a subset of `YAML `_ 1.2. The JSON produced by this module's default settings (in particular, the default *separators* - value) is also a subset of YAML 1.0 and 1.1. This module can thus also be - used as a YAML serializer. + value without indentation) is also a subset of YAML 1.0 and 1.1. This + module can thus also be used as a YAML serializer. Basic Usage @@ -155,9 +155,13 @@ .. versionchanged:: 3.2 Allow strings for *indent* in addition to integers. - If *separators* is an ``(item_separator, dict_separator)`` tuple, then it - will be used instead of the default ``(', ', ': ')`` separators. ``(',', - ':')`` is the most compact JSON representation. + If specified, *separators* should be an ``(item_separator, key_separator)`` + tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and + ``(',', ': ')`` otherwise. To get the most compact JSON representation, + you should specify ``(',', ':')`` to eliminate whitespace. + + .. versionchanged:: 3.4 + Use ``(',', ': ')`` as default if *indent* is not ``None``. *default(obj)* is a function that should return a serializable version of *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`. @@ -394,8 +398,12 @@ Allow strings for *indent* in addition to integers. If specified, *separators* should be an ``(item_separator, key_separator)`` - tuple. The default is ``(', ', ': ')``. To get the most compact JSON - representation, you should specify ``(',', ':')`` to eliminate whitespace. + tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and + ``(',', ': ')`` otherwise. To get the most compact JSON representation, + you should specify ``(',', ':')`` to eliminate whitespace. + + .. versionchanged:: 3.4 + Use ``(',', ': ')`` as default if *indent* is not ``None``. If specified, *default* is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the diff -r aac6b313ef5f Lib/json/__init__.py --- a/Lib/json/__init__.py Sat Nov 24 20:42:59 2012 +0100 +++ b/Lib/json/__init__.py Sun Nov 25 18:59:31 2012 +0200 @@ -148,9 +148,10 @@ level of 0 will only insert newlines. ``None`` is the most compact representation. - If ``separators`` is an ``(item_separator, dict_separator)`` tuple - then it will be used instead of the default ``(', ', ': ')`` separators. - ``(',', ':')`` is the most compact JSON representation. + If specified, ``separators`` should be an ``(item_separator, key_separator)`` + tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and + ``(',', ': ')`` otherwise. To get the most compact JSON representation, + you should specify ``(',', ':')`` to eliminate whitespace. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. @@ -209,9 +210,10 @@ level of 0 will only insert newlines. ``None`` is the most compact representation. - If ``separators`` is an ``(item_separator, dict_separator)`` tuple - then it will be used instead of the default ``(', ', ': ')`` separators. - ``(',', ':')`` is the most compact JSON representation. + If specified, ``separators`` should be an ``(item_separator, key_separator)`` + tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and + ``(',', ': ')`` otherwise. To get the most compact JSON representation, + you should specify ``(',', ':')`` to eliminate whitespace. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. diff -r aac6b313ef5f Lib/json/encoder.py --- a/Lib/json/encoder.py Sat Nov 24 20:42:59 2012 +0100 +++ b/Lib/json/encoder.py Sun Nov 25 18:59:31 2012 +0200 @@ -127,9 +127,10 @@ indent level. An indent level of 0 will only insert newlines. None is the most compact representation. - If specified, separators should be a (item_separator, key_separator) - tuple. The default is (', ', ': '). To get the most compact JSON - representation you should specify (',', ':') to eliminate whitespace. + If specified, separators should be an (item_separator, key_separator) + tuple. The default is (', ', ': ') if *indent* is ``None`` and + (',', ': ') otherwise. To get the most compact JSON representation, + you should specify (',', ':') to eliminate whitespace. If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable @@ -145,6 +146,8 @@ self.indent = indent if separators is not None: self.item_separator, self.key_separator = separators + elif indent is not None: + self.item_separator = ',' if default is not None: self.default = default diff -r aac6b313ef5f Lib/test/json_tests/test_indent.py --- a/Lib/test/json_tests/test_indent.py Sat Nov 24 20:42:59 2012 +0100 +++ b/Lib/test/json_tests/test_indent.py Sun Nov 25 18:59:31 2012 +0200 @@ -32,6 +32,8 @@ d1 = self.dumps(h) d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': ')) + d4 = self.dumps(h, indent=2, sort_keys=True) + d5 = self.dumps(h, indent='\t', sort_keys=True) h1 = self.loads(d1) h2 = self.loads(d2) @@ -42,6 +44,8 @@ self.assertEqual(h3, h) self.assertEqual(d2, expect.expandtabs(2)) self.assertEqual(d3, expect) + self.assertEqual(d4, d2) + self.assertEqual(d5, d3) def test_indent0(self): h = {3: 1}