Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4460aa

Browse files
committed
#4785: document strict argument of JSONDecoder, plus add object_pairs_hook in the docstrings.
1 parent b67878a commit d4460aa

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

Doc/library/json.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ Basic Usage
149149

150150
To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
151151
:meth:`default` method to serialize additional types), specify it with the
152-
*cls* kwarg.
152+
*cls* kwarg; otherwise :class:`JSONEncoder` is used.
153153

154154

155155
.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw)
@@ -195,8 +195,8 @@ Basic Usage
195195
are encountered.
196196

197197
To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
198-
kwarg. Additional keyword arguments will be passed to the constructor of the
199-
class.
198+
kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
199+
will be passed to the constructor of the class.
200200

201201

202202
.. function:: loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
@@ -275,6 +275,11 @@ Encoders and decoders
275275
``'false'``. This can be used to raise an exception if invalid JSON numbers
276276
are encountered.
277277

278+
If *strict* is ``False`` (``True`` is the default), then control characters
279+
will be allowed inside strings. Control characters in this context are
280+
those with character codes in the 0-31 range, including ``'\t'`` (tab),
281+
``'\n'``, ``'\r'`` and ``'\0'``.
282+
278283

279284
.. method:: decode(s)
280285

Lib/json/__init__.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
155155
156156
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
157157
``.default()`` method to serialize additional types), specify it with
158-
the ``cls`` kwarg.
158+
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
159159
160160
"""
161161
# cached encoder
@@ -213,7 +213,7 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
213213
214214
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
215215
``.default()`` method to serialize additional types), specify it with
216-
the ``cls`` kwarg.
216+
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
217217
218218
"""
219219
# cached encoder
@@ -244,8 +244,16 @@ def load(fp, cls=None, object_hook=None, parse_float=None,
244244
``object_hook`` will be used instead of the ``dict``. This feature
245245
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
246246
247+
``object_pairs_hook`` is an optional function that will be called with the
248+
result of any object literal decoded with an ordered list of pairs. The
249+
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
250+
This feature can be used to implement custom decoders that rely on the
251+
order that the key and value pairs are decoded (for example,
252+
collections.OrderedDict will remember the order of insertion). If
253+
``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
254+
247255
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
248-
kwarg.
256+
kwarg; otherwise ``JSONDecoder`` is used.
249257
250258
"""
251259
return loads(fp.read(),
@@ -264,6 +272,14 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
264272
``object_hook`` will be used instead of the ``dict``. This feature
265273
can be used to implement custom decoders (e.g. JSON-RPC class hinting).
266274
275+
``object_pairs_hook`` is an optional function that will be called with the
276+
result of any object literal decoded with an ordered list of pairs. The
277+
return value of ``object_pairs_hook`` will be used instead of the ``dict``.
278+
This feature can be used to implement custom decoders that rely on the
279+
order that the key and value pairs are decoded (for example,
280+
collections.OrderedDict will remember the order of insertion). If
281+
``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
282+
267283
``parse_float``, if specified, will be called with the string
268284
of every JSON float to be decoded. By default this is equivalent to
269285
float(num_str). This can be used to use another datatype or parser
@@ -280,7 +296,7 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
280296
are encountered.
281297
282298
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
283-
kwarg.
299+
kwarg; otherwise ``JSONDecoder`` is used.
284300
285301
"""
286302
if (cls is None and object_hook is None and

Lib/json/decoder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,15 @@ def __init__(self, object_hook=None, parse_float=None,
294294
place of the given ``dict``. This can be used to provide custom
295295
deserializations (e.g. to support JSON-RPC class hinting).
296296
297+
``object_pairs_hook``, if specified will be called with the result of
298+
every JSON object decoded with an ordered list of pairs. The return
299+
value of ``object_pairs_hook`` will be used instead of the ``dict``.
300+
This feature can be used to implement custom decoders that rely on the
301+
order that the key and value pairs are decoded (for example,
302+
collections.OrderedDict will remember the order of insertion). If
303+
``object_hook`` is also defined, the ``object_pairs_hook`` takes
304+
priority.
305+
297306
``parse_float``, if specified, will be called with the string
298307
of every JSON float to be decoded. By default this is equivalent to
299308
float(num_str). This can be used to use another datatype or parser
@@ -309,6 +318,11 @@ def __init__(self, object_hook=None, parse_float=None,
309318
This can be used to raise an exception if invalid JSON numbers
310319
are encountered.
311320
321+
If ``strict`` is false (true is the default), then control
322+
characters will be allowed inside strings. Control characters in
323+
this context are those with character codes in the 0-31 range,
324+
including ``'\\t'`` (tab), ``'\\n'``, ``'\\r'`` and ``'\\0'``.
325+
312326
"""
313327
self.object_hook = object_hook
314328
self.parse_float = parse_float or float

0 commit comments

Comments
 (0)