Skip to content

Commit 3626e5e

Browse files
Issue #4945: Improved the documenting of boolean arguments in the json module.
Based on patch by Gabriel Genellina.
1 parent bc44893 commit 3626e5e

2 files changed

Lines changed: 35 additions & 31 deletions

File tree

Doc/library/json.rst

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ Extending :class:`JSONEncoder`::
9999

100100
.. highlight:: none
101101

102-
Using json.tool from the shell to validate and pretty-print::
102+
Using :mod:`json.tool` from the shell to validate and pretty-print::
103103

104-
$ echo '{"json":"obj"}' | python -mjson.tool
104+
$ echo '{"json":"obj"}' | python -m json.tool
105105
{
106106
"json": "obj"
107107
}
@@ -130,28 +130,29 @@ Basic Usage
130130
:term:`file-like object`) using this :ref:`conversion table
131131
<py-to-json-table>`.
132132

133-
If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
133+
If *skipkeys* is true (default: ``False``), then dict keys that are not
134134
of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`,
135135
:class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
136136
:exc:`TypeError`.
137137

138-
If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
138+
If *ensure_ascii* is true (the default), all non-ASCII characters in the
139139
output are escaped with ``\uXXXX`` sequences, and the result is a
140140
:class:`str` instance consisting of ASCII characters only. If
141-
*ensure_ascii* is ``False``, some chunks written to *fp* may be
141+
*ensure_ascii* is false, some chunks written to *fp* may be
142142
:class:`unicode` instances. This usually happens because the input contains
143143
unicode strings or the *encoding* parameter is used. Unless ``fp.write()``
144144
explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`)
145145
this is likely to cause an error.
146146

147-
If *check_circular* is ``False`` (default: ``True``), then the circular
147+
If *check_circular* is false (default: ``True``), then the circular
148148
reference check for container types will be skipped and a circular reference
149149
will result in an :exc:`OverflowError` (or worse).
150150

151-
If *allow_nan* is ``False`` (default: ``True``), then it will be a
151+
If *allow_nan* is false (default: ``True``), then it will be a
152152
:exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
153-
``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
154-
using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
153+
``inf``, ``-inf``) in strict compliance of the JSON specification.
154+
If *allow_nan* is true, their JavaScript equivalents (``NaN``,
155+
``Infinity``, ``-Infinity``) will be used.
155156

156157
If *indent* is a non-negative integer, then JSON array elements and object
157158
members will be pretty-printed with that indent level. An indent level of 0,
@@ -164,16 +165,18 @@ Basic Usage
164165
trailing whitespace when *indent* is specified. You can use
165166
``separators=(',', ': ')`` to avoid this.
166167

167-
If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
168-
will be used instead of the default ``(', ', ': ')`` separators. ``(',',
169-
':')`` is the most compact JSON representation.
168+
If specified, *separators* should be an ``(item_separator, key_separator)``
169+
tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON
170+
representation, you should specify ``(',', ':')`` to eliminate whitespace.
170171

171172
*encoding* is the character encoding for str instances, default is UTF-8.
172173

173-
*default(obj)* is a function that should return a serializable version of
174-
*obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
174+
If specified, *default* should be a function that gets called for objects that
175+
can't otherwise be serialized. It should return a JSON encodable version of
176+
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
177+
is raised.
175178

176-
If *sort_keys* is ``True`` (default: ``False``), then the output of
179+
If *sort_keys* is true (default: ``False``), then the output of
177180
dictionaries will be sorted by key.
178181

179182
To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
@@ -192,7 +195,7 @@ Basic Usage
192195
default=None, sort_keys=False, **kw)
193196
194197
Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
195-
table <py-to-json-table>`. If *ensure_ascii* is ``False``, the result may
198+
table <py-to-json-table>`. If *ensure_ascii* is false, the result may
196199
contain non-ASCII characters and the return value may be a :class:`unicode`
197200
instance.
198201

@@ -345,7 +348,7 @@ Encoders and Decoders
345348
``'false'``. This can be used to raise an exception if invalid JSON numbers
346349
are encountered.
347350

348-
If *strict* is ``False`` (``True`` is the default), then control characters
351+
If *strict* is false (``True`` is the default), then control characters
349352
will be allowed inside strings. Control characters in this context are
350353
those with character codes in the 0-31 range, including ``'\t'`` (tab),
351354
``'\n'``, ``'\r'`` and ``'\0'``.
@@ -399,29 +402,29 @@ Encoders and Decoders
399402
for ``o`` if possible, otherwise it should call the superclass implementation
400403
(to raise :exc:`TypeError`).
401404

402-
If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
405+
If *skipkeys* is false (the default), then it is a :exc:`TypeError` to
403406
attempt encoding of keys that are not str, int, long, float or None. If
404-
*skipkeys* is ``True``, such items are simply skipped.
407+
*skipkeys* is true, such items are simply skipped.
405408

406-
If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
409+
If *ensure_ascii* is true (the default), all non-ASCII characters in the
407410
output are escaped with ``\uXXXX`` sequences, and the results are
408411
:class:`str` instances consisting of ASCII characters only. If
409-
*ensure_ascii* is ``False``, a result may be a :class:`unicode`
412+
*ensure_ascii* is false, a result may be a :class:`unicode`
410413
instance. This usually happens if the input contains unicode strings or the
411414
*encoding* parameter is used.
412415

413-
If *check_circular* is ``True`` (the default), then lists, dicts, and custom
416+
If *check_circular* is true (the default), then lists, dicts, and custom
414417
encoded objects will be checked for circular references during encoding to
415418
prevent an infinite recursion (which would cause an :exc:`OverflowError`).
416419
Otherwise, no such check takes place.
417420

418-
If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
421+
If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and
419422
``-Infinity`` will be encoded as such. This behavior is not JSON
420423
specification compliant, but is consistent with most JavaScript based
421424
encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
422425
such floats.
423426

424-
If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
427+
If *sort_keys* is true (default: ``False``), then the output of dictionaries
425428
will be sorted by key; this is useful for regression tests to ensure that
426429
JSON serializations can be compared on a day-to-day basis.
427430

@@ -437,12 +440,13 @@ Encoders and Decoders
437440
``separators=(',', ': ')`` to avoid this.
438441

439442
If specified, *separators* should be an ``(item_separator, key_separator)``
440-
tuple. The default is ``(', ', ': ')``. To get the most compact JSON
443+
tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON
441444
representation, you should specify ``(',', ':')`` to eliminate whitespace.
442445

443-
If specified, *default* is a function that gets called for objects that can't
444-
otherwise be serialized. It should return a JSON encodable version of the
445-
object or raise a :exc:`TypeError`.
446+
If specified, *default* should be a function that gets called for objects that
447+
can't otherwise be serialized. It should return a JSON encodable version of
448+
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
449+
is raised.
446450

447451
If *encoding* is not ``None``, then all input strings will be transformed
448452
into unicode using that encoding prior to JSON-encoding. The default is

Lib/json/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
132132
If ``ensure_ascii`` is true (the default), all non-ASCII characters in the
133133
output are escaped with ``\uXXXX`` sequences, and the result is a ``str``
134134
instance consisting of ASCII characters only. If ``ensure_ascii`` is
135-
``False``, some chunks written to ``fp`` may be ``unicode`` instances.
135+
false, some chunks written to ``fp`` may be ``unicode`` instances.
136136
This usually happens because the input contains unicode strings or the
137137
``encoding`` parameter is used. Unless ``fp.write()`` explicitly
138138
understands ``unicode`` (as in ``codecs.getwriter``) this is likely to
@@ -163,7 +163,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
163163
``default(obj)`` is a function that should return a serializable version
164164
of obj or raise TypeError. The default simply raises TypeError.
165165
166-
If *sort_keys* is ``True`` (default: ``False``), then the output of
166+
If *sort_keys* is true (default: ``False``), then the output of
167167
dictionaries will be sorted by key.
168168
169169
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
@@ -228,7 +228,7 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
228228
``default(obj)`` is a function that should return a serializable version
229229
of obj or raise TypeError. The default simply raises TypeError.
230230
231-
If *sort_keys* is ``True`` (default: ``False``), then the output of
231+
If *sort_keys* is true (default: ``False``), then the output of
232232
dictionaries will be sorted by key.
233233
234234
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the

0 commit comments

Comments
 (0)