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

Delta Between Two Patch Sets: Doc/library/json.rst

Issue 16333: Trailing whitespace in json dump when using indent
Left Patch Set: Created 6 years, 11 months ago
Right Patch Set: Created 6 years, 11 months 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/json/tool.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 24 matching lines...) Expand all
35 35
36 Compact encoding:: 36 Compact encoding::
37 37
38 >>> import json 38 >>> import json
39 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':')) 39 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))
40 '[1,2,3,{"4":5,"6":7}]' 40 '[1,2,3,{"4":5,"6":7}]'
41 41
42 Pretty printing:: 42 Pretty printing::
43 43
44 >>> import json 44 >>> import json
45 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)) 45 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True,
46 ... indent=4, separators=(',', ': ')))
46 { 47 {
47 "4": 5, 48 "4": 5,
48 "6": 7 49 "6": 7
49 } 50 }
50 51
51 Decoding JSON:: 52 Decoding JSON::
52 53
53 >>> import json 54 >>> import json
54 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') 55 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
55 ['foo', {'bar': ['baz', None, 1.0, 2]}] 56 ['foo', {'bar': ['baz', None, 1.0, 2]}]
(...skipping 92 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 149 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 150 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) 151 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
151 selects the most compact representation. Using a positive integer indent 152 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"` `), 153 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `),
153 that string is used to indent each level. 154 that string is used to indent each level.
154 155
155 .. versionchanged:: 3.2 156 .. versionchanged:: 3.2
156 Allow strings for *indent* in addition to integers. 157 Allow strings for *indent* in addition to integers.
157 158
158 If specified, *separators* should be an ``(item_separator, key_separator)`` 159 .. note::
159 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and 160
160 ``(',', ': ')`` otherwise. To get the most compact JSON representation, 161 Use ``separators=(',', ': ')`` instead the default *separators* to supress
161 you should specify ``(',', ':')`` to eliminate whitespace. 162 an extra space preciding the newline.
162 163
163 .. versionchanged:: 3.4 164 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
164 Use ``(',', ': ')`` as default if *indent* is not ``None``. 165 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
166 ':')`` is the most compact JSON representation.
165 167
166 *default(obj)* is a function that should return a serializable version of 168 *default(obj)* is a function that should return a serializable version of
167 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`. 169 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
168 170
169 If *sort_keys* is ``True`` (default: ``False``), then the output of 171 If *sort_keys* is ``True`` (default: ``False``), then the output of
170 dictionaries will be sorted by key. 172 dictionaries will be sorted by key.
171 173
172 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the 174 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
173 :meth:`default` method to serialize additional types), specify it with the 175 :meth:`default` method to serialize additional types), specify it with the
174 *cls* kwarg; otherwise :class:`JSONEncoder` is used. 176 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 If *indent* is a non-negative integer or string, then JSON array elements and 392 If *indent* is a non-negative integer or string, then JSON array elements and
391 object members will be pretty-printed with that indent level. An indent leve l 393 object members will be pretty-printed with that indent level. An indent leve l
392 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) 394 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
393 selects the most compact representation. Using a positive integer indent 395 selects the most compact representation. Using a positive integer indent
394 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `), 396 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `),
395 that string is used to indent each level. 397 that string is used to indent each level.
396 398
397 .. versionchanged:: 3.2 399 .. versionchanged:: 3.2
398 Allow strings for *indent* in addition to integers. 400 Allow strings for *indent* in addition to integers.
399 401
402 .. note::
403
404 Use ``separators=(',', ': ')`` instead the default *separators* to supress
405 an extra space preciding the newline.
406
400 If specified, *separators* should be an ``(item_separator, key_separator)`` 407 If specified, *separators* should be an ``(item_separator, key_separator)``
401 tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and 408 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
402 ``(',', ': ')`` otherwise. To get the most compact JSON representation, 409 representation, you should specify ``(',', ':')`` to eliminate whitespace.
403 you should specify ``(',', ':')`` to eliminate whitespace.
404
405 .. versionchanged:: 3.4
406 Use ``(',', ': ')`` as default if *indent* is not ``None``.
407 410
408 If specified, *default* is a function that gets called for objects that can't 411 If specified, *default* is a function that gets called for objects that can't
409 otherwise be serialized. It should return a JSON encodable version of the 412 otherwise be serialized. It should return a JSON encodable version of the
410 object or raise a :exc:`TypeError`. 413 object or raise a :exc:`TypeError`.
411 414
412 415
413 .. method:: default(o) 416 .. method:: default(o)
414 417
415 Implement this method in a subclass such that it returns a serializable 418 Implement this method in a subclass such that it returns a serializable
416 object for *o*, or calls the base implementation (to raise a 419 object for *o*, or calls the base implementation (to raise a
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 The RFC specifies that the names within a JSON object should be unique, but 541 The RFC specifies that the names within a JSON object should be unique, but
539 does not specify how repeated names in JSON objects should be handled. By 542 does not specify how repeated names in JSON objects should be handled. By
540 default, this module does not raise an exception; instead, it ignores all but 543 default, this module does not raise an exception; instead, it ignores all but
541 the last name-value pair for a given name:: 544 the last name-value pair for a given name::
542 545
543 >>> weird_json = '{"x": 1, "x": 2, "x": 3}' 546 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
544 >>> json.loads(weird_json) 547 >>> json.loads(weird_json)
545 {'x': 3} 548 {'x': 3}
546 549
547 The *object_pairs_hook* parameter can be used to alter this behavior. 550 The *object_pairs_hook* parameter can be used to alter this behavior.
LEFTRIGHT
« no previous file | Lib/json/tool.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

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