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

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/tool.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 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 148
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.
158
159 .. note::
160
161 Use ``separators=(',', ': ')`` instead the default *separators* to supress
162 an extra space preciding the newline.
157 163
158 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it 164 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
159 will be used instead of the default ``(', ', ': ')`` separators. ``(',', 165 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
160 ':')`` is the most compact JSON representation. 166 ':')`` is the most compact JSON representation.
161 167
162 *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
163 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`. 169 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
164 170
165 If *sort_keys* is ``True`` (default: ``False``), then the output of 171 If *sort_keys* is ``True`` (default: ``False``), then the output of
166 dictionaries will be sorted by key. 172 dictionaries will be sorted by key.
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 391
386 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
387 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
388 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default) 394 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
389 selects the most compact representation. Using a positive integer indent 395 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"` `), 396 indents that many spaces per level. If *indent* is a string (such as ``"\t"` `),
391 that string is used to indent each level. 397 that string is used to indent each level.
392 398
393 .. versionchanged:: 3.2 399 .. versionchanged:: 3.2
394 Allow strings for *indent* in addition to integers. 400 Allow strings for *indent* in addition to integers.
401
402 .. note::
403
404 Use ``separators=(',', ': ')`` instead the default *separators* to supress
405 an extra space preciding the newline.
395 406
396 If specified, *separators* should be an ``(item_separator, key_separator)`` 407 If specified, *separators* should be an ``(item_separator, key_separator)``
397 tuple. The default is ``(', ', ': ')``. To get the most compact JSON 408 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
398 representation, you should specify ``(',', ':')`` to eliminate whitespace. 409 representation, you should specify ``(',', ':')`` to eliminate whitespace.
399 410
400 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
401 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
402 object or raise a :exc:`TypeError`. 413 object or raise a :exc:`TypeError`.
403 414
404 415
(...skipping 125 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 541 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 542 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 543 default, this module does not raise an exception; instead, it ignores all but
533 the last name-value pair for a given name:: 544 the last name-value pair for a given name::
534 545
535 >>> weird_json = '{"x": 1, "x": 2, "x": 3}' 546 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
536 >>> json.loads(weird_json) 547 >>> json.loads(weird_json)
537 {'x': 3} 548 {'x': 3}
538 549
539 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.
OLDNEW
« no previous file with comments | « no previous file | Lib/json/tool.py » ('j') | no next file with comments »

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