@@ -241,7 +241,7 @@ def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
241241 ** kw ).encode (obj )
242242
243243
244- _default_decoder = JSONDecoder (object_hook = None , object_pairs_hook = None )
244+ _default_decoder = JSONDecoder ()
245245
246246
247247def detect_encoding (b ):
@@ -275,7 +275,8 @@ def detect_encoding(b):
275275
276276
277277def load (fp , * , cls = None , object_hook = None , parse_float = None ,
278- parse_int = None , parse_constant = None , object_pairs_hook = None , ** kw ):
278+ parse_int = None , parse_constant = None , object_pairs_hook = None ,
279+ array_hook = None , ** kw ):
279280 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
280281 a JSON document) to a Python object.
281282
@@ -291,17 +292,26 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None,
291292 ``object_hook`` is also defined, the ``object_pairs_hook`` takes
292293 priority.
293294
295+ ``array_hook`` is an optional function that will be called with the result
296+ of any literal array decode (a ``list``). The return value of this function will
297+ be used instead of the ``list``. This feature can be used along
298+ ``object_pairs_hook`` to customize the resulting data structure - for example,
299+ by setting that to ``frozendict`` and ``array_hook`` to ``tuple``, one can get
300+ a deep immutable data structute from any JSON data.
301+
294302 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
295303 kwarg; otherwise ``JSONDecoder`` is used.
296304 """
297305 return loads (fp .read (),
298306 cls = cls , object_hook = object_hook ,
299307 parse_float = parse_float , parse_int = parse_int ,
300- parse_constant = parse_constant , object_pairs_hook = object_pairs_hook , ** kw )
308+ parse_constant = parse_constant , object_pairs_hook = object_pairs_hook ,
309+ array_hook = None , ** kw )
301310
302311
303312def loads (s , * , cls = None , object_hook = None , parse_float = None ,
304- parse_int = None , parse_constant = None , object_pairs_hook = None , ** kw ):
313+ parse_int = None , parse_constant = None , object_pairs_hook = None ,
314+ array_hook = None , ** kw ):
305315 """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance
306316 containing a JSON document) to a Python object.
307317
@@ -317,6 +327,13 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
317327 ``object_hook`` is also defined, the ``object_pairs_hook`` takes
318328 priority.
319329
330+ ``array_hook`` is an optional function that will be called with the result
331+ of any literal array decode (a ``list``). The return value of this function will
332+ be used instead of the ``list``. This feature can be used along
333+ ``object_pairs_hook`` to customize the resulting data structure - for example,
334+ by setting that to ``frozendict`` and ``array_hook`` to ``tuple``, one can get
335+ a deep immutable data structute from any JSON data.
336+
320337 ``parse_float``, if specified, will be called with the string
321338 of every JSON float to be decoded. By default this is equivalent to
322339 float(num_str). This can be used to use another datatype or parser
@@ -347,14 +364,17 @@ def loads(s, *, cls=None, object_hook=None, parse_float=None,
347364
348365 if (cls is None and object_hook is None and
349366 parse_int is None and parse_float is None and
350- parse_constant is None and object_pairs_hook is None and not kw ):
367+ parse_constant is None and object_pairs_hook is None
368+ and array_hook is None and not kw ):
351369 return _default_decoder .decode (s )
352370 if cls is None :
353371 cls = JSONDecoder
354372 if object_hook is not None :
355373 kw ['object_hook' ] = object_hook
356374 if object_pairs_hook is not None :
357375 kw ['object_pairs_hook' ] = object_pairs_hook
376+ if array_hook is not None :
377+ kw ['array_hook' ] = array_hook
358378 if parse_float is not None :
359379 kw ['parse_float' ] = parse_float
360380 if parse_int is not None :
0 commit comments