Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(158511)

Side by Side Diff: Doc/library/json.rst

Issue 16333: Trailing whitespace in json dump when using indent
Patch Set: Created 7 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/json/encoder.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 :mod:`json` --- JSON encoder and decoder 1 :mod:`json` --- JSON encoder and decoder
2 ======================================== 2 ========================================
3 3
4 .. module:: json 4 .. module:: json
5 :synopsis: Encode and decode the JSON format. 5 :synopsis: Encode and decode the JSON format.
6 .. moduleauthor:: Bob Ippolito <bob@redivi.com> 6 .. moduleauthor:: Bob Ippolito <bob@redivi.com>
7 .. sectionauthor:: Bob Ippolito <bob@redivi.com> 7 .. sectionauthor:: Bob Ippolito <bob@redivi.com>
8 8
9 `JSON (JavaScript Object Notation) <http://json.org>`_, specified by 9 `JSON (JavaScript Object Notation) <http://json.org>`_, specified by
10 :rfc:`4627`, is a lightweight data interchange format based on a subset of 10 :rfc:`4627`, is a lightweight data interchange format based on a subset of
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 If *indent* is a non-negative integer or string, then JSON array elements and 148 If *indent* is a non-negative integer or string, then JSON array elements and
149 object members will be pretty-printed with that indent level. An indent leve l 149 object members will be pretty-printed with that indent level. An indent leve l
150 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) 150 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
151 selects the most compact representation. Using a positive integer indent 151 selects the most compact representation. Using a positive integer indent
152 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `), 152 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `),
153 that string is used to indent each level. 153 that string is used to indent each level.
154 154
155 .. versionchanged:: 3.2 155 .. versionchanged:: 3.2
156 Allow strings for *indent* in addition to integers. 156 Allow strings for *indent* in addition to integers.
157 157
158 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it 158 If specified, *separators* should be an ``(item_separator, key_separator)``
159 will be used instead of the default ``(', ', ': ')`` separators. ``(',', 159 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
160 ':')`` is the most compact JSON representation. 160 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
161 you should specify ``(',', ':')`` to eliminate whitespace.
162
163 .. versionchanged:: 3.4
164 Use ``(',', ': ')`` as default if *indent* is not ``None``.
161 165
162 *default(obj)* is a function that should return a serializable version of 166 *default(obj)* is a function that should return a serializable version of
163 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`. 167 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
164 168
165 If *sort_keys* is ``True`` (default: ``False``), then the output of 169 If *sort_keys* is ``True`` (default: ``False``), then the output of
166 dictionaries will be sorted by key. 170 dictionaries will be sorted by key.
167 171
168 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the 172 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
169 :meth:`default` method to serialize additional types), specify it with the 173 :meth:`default` method to serialize additional types), specify it with the
170 *cls* kwarg; otherwise :class:`JSONEncoder` is used. 174 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 object members will be pretty-printed with that indent level. An indent leve l 391 object members will be pretty-printed with that indent level. An indent leve l
388 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) 392 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
389 selects the most compact representation. Using a positive integer indent 393 selects the most compact representation. Using a positive integer indent
390 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `), 394 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `),
391 that string is used to indent each level. 395 that string is used to indent each level.
392 396
393 .. versionchanged:: 3.2 397 .. versionchanged:: 3.2
394 Allow strings for *indent* in addition to integers. 398 Allow strings for *indent* in addition to integers.
395 399
396 If specified, *separators* should be an ``(item_separator, key_separator)`` 400 If specified, *separators* should be an ``(item_separator, key_separator)``
397 tuple. The default is ``(', ', ': ')``. To get the most compact JSON 401 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
398 representation, you should specify ``(',', ':')`` to eliminate whitespace. 402 ``(',', ': ')`` otherwise. To get the most compact JSON representation,
403 you should specify ``(',', ':')`` to eliminate whitespace.
404
405 .. versionchanged:: 3.4
406 Use ``(',', ': ')`` as default if *indent* is not ``None``.
399 407
400 If specified, *default* is a function that gets called for objects that can't 408 If specified, *default* is a function that gets called for objects that can't
401 otherwise be serialized. It should return a JSON encodable version of the 409 otherwise be serialized. It should return a JSON encodable version of the
402 object or raise a :exc:`TypeError`. 410 object or raise a :exc:`TypeError`.
403 411
404 412
405 .. method:: default(o) 413 .. method:: default(o)
406 414
407 Implement this method in a subclass such that it returns a serializable 415 Implement this method in a subclass such that it returns a serializable
408 object for *o*, or calls the base implementation (to raise a 416 object for *o*, or calls the base implementation (to raise a
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 The RFC specifies that the names within a JSON object should be unique, but 538 The RFC specifies that the names within a JSON object should be unique, but
531 does not specify how repeated names in JSON objects should be handled. By 539 does not specify how repeated names in JSON objects should be handled. By
532 default, this module does not raise an exception; instead, it ignores all but 540 default, this module does not raise an exception; instead, it ignores all but
533 the last name-value pair for a given name:: 541 the last name-value pair for a given name::
534 542
535 >>> weird_json = '{"x": 1, "x": 2, "x": 3}' 543 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
536 >>> json.loads(weird_json) 544 >>> json.loads(weird_json)
537 {'x': 3} 545 {'x': 3}
538 546
539 The *object_pairs_hook* parameter can be used to alter this behavior. 547 The *object_pairs_hook* parameter can be used to alter this behavior.
OLDNEW
« no previous file with comments | « no previous file | Lib/json/encoder.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+