Skip to content

Commit 82b061d

Browse files
[3.12] Docs: mark up json.load() using parameter list (GH-128488) (#128597)
(cherry picked from commit a21e31e) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent dae5b16 commit 82b061d

File tree

1 file changed

+69
-44
lines changed

1 file changed

+69
-44
lines changed

Doc/library/json.rst

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -247,64 +247,89 @@ Basic Usage
247247
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
248248
keys.
249249

250-
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
250+
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
251+
parse_int=None, parse_constant=None, \
252+
object_pairs_hook=None, **kw)
251253
252-
Deserialize *fp* (a ``.read()``-supporting :term:`text file` or
253-
:term:`binary file` containing a JSON document) to a Python object using
254-
this :ref:`conversion table <json-to-py-table>`.
254+
Deserialize *fp* to a Python object
255+
using the :ref:`JSON-to-Python conversion table <json-to-py-table>`.
255256

256-
*object_hook* is an optional function that will be called with the result of
257-
any object literal decoded (a :class:`dict`). The return value of
258-
*object_hook* will be used instead of the :class:`dict`. This feature can
259-
be used to implement custom decoders (e.g. `JSON-RPC
260-
<https://www.jsonrpc.org>`_ class hinting).
257+
:param fp:
258+
A ``.read()``-supporting :term:`text file` or :term:`binary file`
259+
containing the JSON document to be deserialized.
260+
:type fp: :term:`file-like object`
261261

262-
*object_pairs_hook* is an optional function that will be called with the
263-
result of any object literal decoded with an ordered list of pairs. The
264-
return value of *object_pairs_hook* will be used instead of the
265-
:class:`dict`. This feature can be used to implement custom decoders. If
266-
*object_hook* is also defined, the *object_pairs_hook* takes priority.
262+
:param cls:
263+
If set, a custom JSON decoder.
264+
Additional keyword arguments to :func:`!load`
265+
will be passed to the constructor of *cls*.
266+
If ``None`` (the default), :class:`!JSONDecoder` is used.
267+
:type cls: a :class:`JSONDecoder` subclass
268+
269+
:param object_hook:
270+
If set, a function that is called with the result of
271+
any object literal decoded (a :class:`dict`).
272+
The return value of this function will be used
273+
instead of the :class:`dict`.
274+
This feature can be used to implement custom decoders,
275+
for example `JSON-RPC <https://www.jsonrpc.org>`_ class hinting.
276+
Default ``None``.
277+
:type object_hook: :term:`callable` | None
278+
279+
:param object_pairs_hook:
280+
If set, a function that is called with the result of
281+
any object literal decoded with an ordered list of pairs.
282+
The return value of this function will be used
283+
instead of the :class:`dict`.
284+
This feature can be used to implement custom decoders.
285+
If *object_hook* is also set, *object_pairs_hook* takes priority.
286+
Default ``None``.
287+
:type object_pairs_hook: :term:`callable` | None
288+
289+
:param parse_float:
290+
If set, a function that is called with
291+
the string of every JSON float to be decoded.
292+
If ``None`` (the default), it is equivalent to ``float(num_str)``.
293+
This can be used to parse JSON floats into custom datatypes,
294+
for example :class:`decimal.Decimal`.
295+
:type parse_float: :term:`callable` | None
296+
297+
:param parse_int:
298+
If set, a function that is called with
299+
the string of every JSON int to be decoded.
300+
If ``None`` (the default), it is equivalent to ``int(num_str)``.
301+
This can be used to parse JSON integers into custom datatypes,
302+
for example :class:`float`.
303+
:type parse_int: :term:`callable` | None
304+
305+
:param parse_constant:
306+
If set, a function that is called with one of the following strings:
307+
``'-Infinity'``, ``'Infinity'``, or ``'NaN'``.
308+
This can be used to raise an exception
309+
if invalid JSON numbers are encountered.
310+
Default ``None``.
311+
:type parse_constant: :term:`callable` | None
312+
313+
:raises JSONDecodeError:
314+
When the data being deserialized is not a valid JSON document.
267315

268316
.. versionchanged:: 3.1
269-
Added support for *object_pairs_hook*.
270317

271-
*parse_float* is an optional function that will be called with the string of
272-
every JSON float to be decoded. By default, this is equivalent to
273-
``float(num_str)``. This can be used to use another datatype or parser for
274-
JSON floats (e.g. :class:`decimal.Decimal`).
318+
* Added the optional *object_pairs_hook* parameter.
319+
* *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
275320

276-
*parse_int* is an optional function that will be called with the string of
277-
every JSON int to be decoded. By default, this is equivalent to
278-
``int(num_str)``. This can be used to use another datatype or parser for
279-
JSON integers (e.g. :class:`float`).
321+
.. versionchanged:: 3.6
322+
323+
* All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
324+
* *fp* can now be a :term:`binary file`.
325+
The input encoding should be UTF-8, UTF-16 or UTF-32.
280326

281327
.. versionchanged:: 3.11
282328
The default *parse_int* of :func:`int` now limits the maximum length of
283329
the integer string via the interpreter's :ref:`integer string
284330
conversion length limitation <int_max_str_digits>` to help avoid denial
285331
of service attacks.
286332

287-
*parse_constant* is an optional function that will be called with one of the
288-
following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This can be
289-
used to raise an exception if invalid JSON numbers are encountered.
290-
291-
.. versionchanged:: 3.1
292-
*parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
293-
294-
To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
295-
kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
296-
will be passed to the constructor of the class.
297-
298-
If the data being deserialized is not a valid JSON document, a
299-
:exc:`JSONDecodeError` will be raised.
300-
301-
.. versionchanged:: 3.6
302-
All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
303-
304-
.. versionchanged:: 3.6
305-
*fp* can now be a :term:`binary file`. The input encoding should be
306-
UTF-8, UTF-16 or UTF-32.
307-
308333
.. function:: loads(s, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
309334

310335
Deserialize *s* (a :class:`str`, :class:`bytes` or :class:`bytearray`

0 commit comments

Comments
 (0)