summaryrefslogtreecommitdiff
path: root/Lib/json
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2012-11-29 00:42:56 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2012-11-29 00:42:56 +0200
commited9ad244bc3e53955f789443e222e87415b8955e (patch)
tree698b662041ff1c3c4e82d76eb7ebf4606b1c4e68 /Lib/json
parentd452e3146d35459ab162638bf80bf218f5ea6299 (diff)
downloadcpython-ed9ad244bc3e53955f789443e222e87415b8955e.tar.gz
#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__.py14
-rw-r--r--Lib/json/encoder.py9
2 files changed, 14 insertions, 9 deletions
diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py
index 86a7a3e50a..7314cb4096 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 ba57c2c25e..93b5ea7d5e 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