cpython: e63ac05ccfa8 (original) (raw)

Mercurial > cpython

changeset 80631:e63ac05ccfa8

#16333: use (",", ": ") as default separator when indent is specified to avoid trailing whitespace. Patch by Serhiy Storchaka. [#16333]

Ezio Melotti ezio.melotti@gmail.com
date Thu, 29 Nov 2012 00:42:56 +0200
parents 8b30a764b58d
children e7919cf9b5e5
files Doc/library/json.rst Lib/json/__init__.py Lib/json/encoder.py Lib/test/json_tests/test_indent.py
diffstat 4 files changed, 31 insertions(+), 14 deletions(-)[+] [-] Doc/library/json.rst 18 Lib/json/__init__.py 14 Lib/json/encoder.py 9 Lib/test/json_tests/test_indent.py 4

line wrap: on

line diff

--- a/Doc/library/json.rst +++ b/Doc/library/json.rst @@ -155,9 +155,13 @@ Basic Usage .. versionchanged:: 3.2 Allow strings for indent in addition to integers.

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 @@ Encoders and Decoders Allow strings for indent in addition to integers. If specified, separators should be an (item_separator, key_separator)

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

--- a/Lib/json/init.py +++ b/Lib/json/init.py @@ -148,9 +148,10 @@ def dump(obj, fp, skipkeys=False, ensure level of 0 will only insert newlines. None is the most compact representation.

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 @@ def dumps(obj, skipkeys=False, ensure_as level of 0 will only insert newlines. None is the most compact representation.

default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.

--- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -127,9 +127,10 @@ class JSONEncoder(object): indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

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 @@ class JSONEncoder(object): self.indent = indent if separators is not None: self.item_separator, self.key_separator = separators

--- a/Lib/test/json_tests/test_indent.py +++ b/Lib/test/json_tests/test_indent.py @@ -32,6 +32,8 @@ class TestIndent: d1 = self.dumps(h) d2 = self.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) d3 = self.dumps(h, indent='\t', sort_keys=True, separators=(',', ': '))

h1 = self.loads(d1) h2 = self.loads(d2) @@ -42,6 +44,8 @@ class TestIndent: self.assertEqual(h3, h) self.assertEqual(d2, expect.expandtabs(2)) self.assertEqual(d3, expect)

def test_indent0(self): h = {3: 1}