Skip to content

Commit fdfc1e2

Browse files
fedonmanmiss-islington
authored andcommitted
gh-146440: Add an example for the json array_hook decoder parameter (GH-154522)
(cherry picked from commit 6d6b1ab) Co-authored-by: Vyron Vasileiadis <hi@fedonman.com>
1 parent 68ac574 commit fdfc1e2

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

‎Doc/library/json.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ Customizing JSON object decoding::
9999
>>> import decimal
100100
>>> json.loads('1.1', parse_float=decimal.Decimal)
101101
Decimal('1.1')
102+
>>> json.loads('{"name": "point", "coords": [[1, 2], [3, 4]]}',
103+
... array_hook=tuple) # doctest: +SKIP
104+
{'name': 'point', 'coords': ((1, 2), (3, 4))}
105+
>>> json.loads('{"name": "point", "coords": [1, 2]}',
106+
... object_pairs_hook=frozendict, array_hook=tuple) # doctest: +SKIP
107+
frozendict({'name': 'point', 'coords': (1, 2)})
102108

103109
Extending :class:`JSONEncoder`::
104110

@@ -304,7 +310,7 @@ Basic Usage
304310

305311
:param array_hook:
306312
If set, a function that is called with the result of
307-
any JSON array literal decoded with as a Python list.
313+
any JSON array literal decoded as a Python list.
308314
The return value of this function will be used
309315
instead of the :class:`list`.
310316
This feature can be used to implement custom decoders.

‎Lib/json/decoder.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def __init__(self, *, object_hook=None, parse_float=None,
314314
be used along ``object_pairs_hook`` to customize the resulting data
315315
structure - for example, by setting that to ``frozendict`` and
316316
``array_hook`` to ``tuple``, one can get a deep immutable data
317-
structute from any JSON data.
317+
structure from any JSON data.
318318
319319
``parse_float``, if specified, will be called with the string
320320
of every JSON float to be decoded. By default this is equivalent to

0 commit comments

Comments
 (0)