Skip to content

Commit 731fc40

Browse files
miss-islingtonerlend-aaslandhugovk
authored
[3.12] Docs: mark up json.dump() using parameter list (GH-128482) (#128487)
(cherry picked from commit a0088b4) Co-authored-by: Erlend E. Aasland <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent f2cc565 commit 731fc40

File tree

1 file changed

+70
-48
lines changed

1 file changed

+70
-48
lines changed

Doc/library/json.rst

Lines changed: 70 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -140,69 +140,91 @@ Basic Usage
140140
sort_keys=False, **kw)
141141
142142
Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
143-
:term:`file-like object`) using this :ref:`conversion table
143+
:term:`file-like object`) using this :ref:`Python-to-JSON conversion table
144144
<py-to-json-table>`.
145145

146-
If *skipkeys* is true (default: ``False``), then dict keys that are not
147-
of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
148-
``None``) will be skipped instead of raising a :exc:`TypeError`.
149-
150-
The :mod:`json` module always produces :class:`str` objects, not
151-
:class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
152-
input.
153-
154-
If *ensure_ascii* is true (the default), the output is guaranteed to
155-
have all incoming non-ASCII characters escaped. If *ensure_ascii* is
156-
false, these characters will be output as-is.
146+
To use a custom :class:`JSONEncoder` subclass (for example, one that overrides the
147+
:meth:`~JSONEncoder.default` method to serialize additional types), specify it with the
148+
*cls* keyword argument; otherwise :class:`JSONEncoder` is used.
157149

158-
If *check_circular* is false (default: ``True``), then the circular
159-
reference check for container types will be skipped and a circular reference
160-
will result in a :exc:`RecursionError` (or worse).
150+
.. note::
161151

162-
If *allow_nan* is false (default: ``True``), then it will be a
163-
:exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
164-
``inf``, ``-inf``) in strict compliance of the JSON specification.
165-
If *allow_nan* is true, their JavaScript equivalents (``NaN``,
166-
``Infinity``, ``-Infinity``) will be used.
152+
Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
153+
so trying to serialize multiple objects with repeated calls to
154+
:func:`dump` using the same *fp* will result in an invalid JSON file.
167155

168-
If *indent* is a non-negative integer or string, then JSON array elements and
169-
object members will be pretty-printed with that indent level. An indent level
170-
of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
171-
selects the most compact representation. Using a positive integer indent
172-
indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
173-
that string is used to indent each level.
156+
:param object obj:
157+
The Python object to be serialized.
158+
159+
:param fp:
160+
The file-like object *obj* will be serialized to.
161+
The :mod:`!json` module always produces :class:`str` objects,
162+
not :class:`bytes` objects,
163+
therefore ``fp.write()`` must support :class:`str` input.
164+
:type fp: :term:`file-like object`
165+
166+
:param bool skipkeys:
167+
If ``True``, keys that are not of a basic type
168+
(:class:`str`, :class:`int`, :class:`float`, :class:`bool`, ``None``)
169+
will be skipped instead of raising a :exc:`TypeError`.
170+
Default ``False``.
171+
172+
:param bool ensure_ascii:
173+
If ``True`` (the default), the output is guaranteed to
174+
have all incoming non-ASCII characters escaped.
175+
If ``False``, these characters will be outputted as-is.
176+
177+
:param bool check_circular:
178+
If ``False``, the circular reference check for container types is skipped
179+
and a circular reference will result in a :exc:`RecursionError` (or worse).
180+
Default ``True``.
181+
182+
:param bool allow_nan:
183+
If ``False``, serialization of out-of-range :class:`float` values
184+
(``nan``, ``inf``, ``-inf``) will result in a :exc:`ValueError`,
185+
in strict compliance with the JSON specification.
186+
If ``True`` (the default), their JavaScript equivalents
187+
(``NaN``, ``Infinity``, ``-Infinity``) are used.
188+
189+
:param indent:
190+
If a positive integer or string, JSON array elements and
191+
object members will be pretty-printed with that indent level.
192+
A positive integer indents that many spaces per level;
193+
a string (such as ``"\t"``) is used to indent each level.
194+
If zero, negative, or ``""`` (the empty string),
195+
only newlines are inserted.
196+
If ``None`` (the default), the most compact representation is used.
197+
:type indent: int | str | None
198+
199+
:param separators:
200+
A two-tuple: ``(item_separator, key_separator)``.
201+
If ``None`` (the default), *separators* defaults to
202+
``(', ', ': ')`` if *indent* is ``None``,
203+
and ``(',', ': ')`` otherwise.
204+
For the most compact JSON,
205+
specify ``(',', ':')`` to eliminate whitespace.
206+
:type separators: tuple | None
207+
208+
:param default:
209+
A function that is called for objects that can't otherwise be serialized.
210+
It should return a JSON encodable version of the object
211+
or raise a :exc:`TypeError`.
212+
If ``None`` (the default), :exc:`!TypeError` is raised.
213+
:type default: :term:`callable` | None
214+
215+
:param sort_keys:
216+
If ``True``, dictionaries will be outputted sorted by key.
217+
Default ``False``.
174218

175219
.. versionchanged:: 3.2
176220
Allow strings for *indent* in addition to integers.
177221

178-
If specified, *separators* should be an ``(item_separator, key_separator)``
179-
tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
180-
``(',', ': ')`` otherwise. To get the most compact JSON representation,
181-
you should specify ``(',', ':')`` to eliminate whitespace.
182-
183222
.. versionchanged:: 3.4
184223
Use ``(',', ': ')`` as default if *indent* is not ``None``.
185224

186-
If specified, *default* should be a function that gets called for objects that
187-
can't otherwise be serialized. It should return a JSON encodable version of
188-
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
189-
is raised.
190-
191-
If *sort_keys* is true (default: ``False``), then the output of
192-
dictionaries will be sorted by key.
193-
194-
To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
195-
:meth:`~JSONEncoder.default` method to serialize additional types), specify it with the
196-
*cls* kwarg; otherwise :class:`JSONEncoder` is used.
197-
198225
.. versionchanged:: 3.6
199226
All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
200227

201-
.. note::
202-
203-
Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
204-
so trying to serialize multiple objects with repeated calls to
205-
:func:`dump` using the same *fp* will result in an invalid JSON file.
206228

207229
.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
208230
check_circular=True, allow_nan=True, cls=None, \

0 commit comments

Comments
 (0)