FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-146440: Add an example for the json array_hook decoder parameter by fedonman · Pull Request #154522 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 7 additions & 1 deletion Doc/library/json.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ Customizing JSON object decoding::
>>> import decimal
>>> json.loads('1.1', parse_float=decimal.Decimal)
Decimal('1.1')
>>> json.loads('{"name": "point", "coords": [[1, 2], [3, 4]]}',
... array_hook=tuple) # doctest: +SKIP
{'name': 'point', 'coords': ((1, 2), (3, 4))}
>>> json.loads('{"name": "point", "coords": [1, 2]}',
... object_pairs_hook=frozendict, array_hook=tuple) # doctest: +SKIP
frozendict({'name': 'point', 'coords': (1, 2)})

Extending :class:`JSONEncoder`::

Expand Down Expand Up @@ -303,7 +309,7 @@ Basic Usage

:param array_hook:
If set, a function that is called with the result of
any JSON array literal decoded with as a Python list.
any JSON array literal decoded as a Python list.
The return value of this function will be used
instead of the :class:`list`.
This feature can be used to implement custom decoders.
Expand Down
2 changes: 1 addition & 1 deletion Lib/json/decoder.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def __init__(self, *, object_hook=None, parse_float=None,
be used along ``object_pairs_hook`` to customize the resulting data
structure - for example, by setting that to ``frozendict`` and
``array_hook`` to ``tuple``, one can get a deep immutable data
structute from any JSON data.
structure from any JSON data.

``parse_float``, if specified, will be called with the string
of every JSON float to be decoded. By default this is equivalent to
Expand Down
Loading

Back | FazBrowse Home | New Git URL