diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2012-11-28 22:42:56 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2012-11-28 22:42:56 (GMT) |
commit | 100314427a558be01bb42f8bdb4bab800cd114d2 (patch) | |
tree | 5a520cf84a80823bfcec7c58aafe2457effed1cd /Lib/json | |
parent | d966c2156ad0bba85258262e5535912361cbd7b7 (diff) | |
download | cpython-100314427a558be01bb42f8bdb4bab800cd114d2.zip cpython-100314427a558be01bb42f8bdb4bab800cd114d2.tar.gz cpython-100314427a558be01bb42f8bdb4bab800cd114d2.tar.bz2 |
#16333: use (",", ": ") as default separator when indent is specified to avoid trailing whitespace. Patch by Serhiy Storchaka.
Diffstat (limited to 'Lib/json')
-rw-r--r-- | Lib/json/__init__.py | 14 | ||||
-rw-r--r-- | Lib/json/encoder.py | 9 |
2 files changed, 14 insertions, 9 deletions
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index 86a7a3e..7314cb4 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -148,9 +148,10 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, 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 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, 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 --git a/Lib/json/encoder.py b/Lib/json/encoder.py index ba57c2c..93b5ea7 100644 --- 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, 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 @@ class JSONEncoder(object): 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 |