Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d654dedb
Commit
d654dedb
authored
Nov 29, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#16333: document a way to get rid of trailing whitespace when indent is used.
parent
74120996
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
6 deletions
+26
-6
Doc/library/json.rst
Doc/library/json.rst
+14
-1
Lib/json/__init__.py
Lib/json/__init__.py
+8
-4
Lib/json/encoder.py
Lib/json/encoder.py
+4
-1
No files found.
Doc/library/json.rst
View file @
d654dedb
...
@@ -42,7 +42,8 @@ Compact encoding::
...
@@ -42,7 +42,8 @@ Compact encoding::
Pretty printing::
Pretty printing::
>>> import json
>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
... indent=4, separators=(',', ': ')))
{
{
"4": 5,
"4": 5,
"6": 7
"6": 7
...
@@ -155,6 +156,12 @@ Basic Usage
...
@@ -155,6 +156,12 @@ Basic Usage
.. versionchanged:: 3.2
.. versionchanged:: 3.2
Allow strings for *indent* in addition to integers.
Allow strings for *indent* in addition to integers.
.. note::
Since the default item separator is ``', '``, the output might include
trailing whitespace when *indent* is specified. You can use
``separators=(',', ': ')`` to avoid this.
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
':')`` is the most compact JSON representation.
':')`` is the most compact JSON representation.
...
@@ -393,6 +400,12 @@ Encoders and Decoders
...
@@ -393,6 +400,12 @@ Encoders and Decoders
.. versionchanged:: 3.2
.. versionchanged:: 3.2
Allow strings for *indent* in addition to integers.
Allow strings for *indent* in addition to integers.
.. note::
Since the default item separator is ``', '``, the output might include
trailing whitespace when *indent* is specified. You can use
``separators=(',', ': ')`` to avoid this.
If specified, *separators* should be an ``(item_separator, key_separator)``
If specified, *separators* should be an ``(item_separator, key_separator)``
tuple. The default is ``(', ', ': ')``. To get the most compact JSON
tuple. The default is ``(', ', ': ')``. To get the most compact JSON
representation, you should specify ``(',', ':')`` to eliminate whitespace.
representation, you should specify ``(',', ':')`` to eliminate whitespace.
...
...
Lib/json/__init__.py
View file @
d654dedb
...
@@ -39,8 +39,8 @@ Compact encoding::
...
@@ -39,8 +39,8 @@ Compact encoding::
Pretty printing::
Pretty printing::
>>> import json
>>> import json
>>>
s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
>>>
print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
>>> print('\n'.join([l.rstrip() for l in s.splitlines()]
))
... indent=4, separators=(',', ': ')
))
{
{
"4": 5,
"4": 5,
"6": 7
"6": 7
...
@@ -146,7 +146,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
...
@@ -146,7 +146,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
If ``indent`` is a non-negative integer, then JSON array elements and
If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
level of 0 will only insert newlines. ``None`` is the most compact
representation.
representation. Since the default item separator is ``', '``, the
output might include trailing whitespace when ``indent`` is specified.
You can use ``separators=(',', ': ')`` to avoid this.
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
then it will be used instead of the default ``(', ', ': ')`` separators.
...
@@ -207,7 +209,9 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
...
@@ -207,7 +209,9 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
If ``indent`` is a non-negative integer, then JSON array elements and
If ``indent`` is a non-negative integer, then JSON array elements and
object members will be pretty-printed with that indent level. An indent
object members will be pretty-printed with that indent level. An indent
level of 0 will only insert newlines. ``None`` is the most compact
level of 0 will only insert newlines. ``None`` is the most compact
representation.
representation. Since the default item separator is ``', '``, the
output might include trailing whitespace when ``indent`` is specified.
You can use ``separators=(',', ': ')`` to avoid this.
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
If ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
then it will be used instead of the default ``(', ', ': ')`` separators.
...
...
Lib/json/encoder.py
View file @
d654dedb
...
@@ -125,7 +125,10 @@ class JSONEncoder(object):
...
@@ -125,7 +125,10 @@ class JSONEncoder(object):
If indent is a non-negative integer, then JSON array
If indent is a non-negative integer, then JSON array
elements and object members will be pretty-printed with that
elements and object members will be pretty-printed with that
indent level. An indent level of 0 will only insert newlines.
indent level. An indent level of 0 will only insert newlines.
None is the most compact representation.
None is the most compact representation. Since the default
item separator is '
,
', the output might include trailing
whitespace when indent is specified. You can use
separators=('
,
', '
:
') to avoid this.
If specified, separators should be a (item_separator, key_separator)
If specified, separators should be a (item_separator, key_separator)
tuple. The default is ('
,
', '
:
'). To get the most compact JSON
tuple. The default is ('
,
', '
:
'). To get the most compact JSON
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment