Skip to content

Commit 6e090dd

Browse files
Docs: mark up json.load() using parameter list
1 parent fd94c6a commit 6e090dd

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
@@ -255,64 +255,89 @@ Basic Usage
255255
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
256256
keys.
257257

258-
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
258+
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
259+
parse_int=None, parse_constant=None, \
260+
object_pairs_hook=None, **kw)
259261
260-
Deserialize *fp* (a ``.read()``-supporting :term:`text file` or
261-
:term:`binary file` containing a JSON document) to a Python object using
262-
this :ref:`conversion table <json-to-py-table>`.
262+
Deserialize *fp* to a Python object
263+
using the :ref:`JSON-to-Python conversion table <json-to-py-table>`.
263264

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

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

276324
.. versionchanged:: 3.1
277-
Added support for *object_pairs_hook*.
278325

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

284-
*parse_int* is an optional function that will be called with the string of
285-
every JSON int to be decoded. By default, this is equivalent to
286-
``int(num_str)``. This can be used to use another datatype or parser for
287-
JSON integers (e.g. :class:`float`).
329+
.. versionchanged:: 3.6
330+
331+
* All optional parameters are now :ref:`keyword-only <keyword-only_parameter>`.
332+
* *fp* can now be a :term:`binary file`.
333+
The input encoding should be UTF-8, UTF-16 or UTF-32.
288334

289335
.. versionchanged:: 3.11
290336
The default *parse_int* of :func:`int` now limits the maximum length of
291337
the integer string via the interpreter's :ref:`integer string
292338
conversion length limitation <int_max_str_digits>` to help avoid denial
293339
of service attacks.
294340

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

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

0 commit comments

Comments
 (0)