Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance containing a JSON document) to a Python object. ``object_hook`` is an optional function that will be called with the result of any object literal decode (a ``dict``). The return value of ``object_hook`` will be used i
(s, *, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None,
array_hook=None, **kw)
| 310 | |
| 311 | |
| 312 | def loads(s, *, cls=None, object_hook=None, parse_float=None, |
| 313 | parse_int=None, parse_constant=None, object_pairs_hook=None, |
| 314 | array_hook=None, **kw): |
| 315 | """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance |
| 316 | containing a JSON document) to a Python object. |
| 317 | |
| 318 | ``object_hook`` is an optional function that will be called with the |
| 319 | result of any object literal decode (a ``dict``). The return value of |
| 320 | ``object_hook`` will be used instead of the ``dict``. This feature |
| 321 | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
| 322 | |
| 323 | ``object_pairs_hook`` is an optional function that will be called with |
| 324 | the result of any object literal decoded with an ordered list of pairs. |
| 325 | The return value of ``object_pairs_hook`` will be used instead of the |
| 326 | ``dict``. This feature can be used to implement custom decoders. If |
| 327 | ``object_hook`` is also defined, the ``object_pairs_hook`` takes |
| 328 | priority. |
| 329 | |
| 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 | |
| 337 | ``parse_float``, if specified, will be called with the string |
| 338 | of every JSON float to be decoded. By default this is equivalent to |
| 339 | float(num_str). This can be used to use another datatype or parser |
| 340 | for JSON floats (e.g. decimal.Decimal). |
| 341 | |
| 342 | ``parse_int``, if specified, will be called with the string |
| 343 | of every JSON int to be decoded. By default this is equivalent to |
| 344 | int(num_str). This can be used to use another datatype or parser |
| 345 | for JSON integers (e.g. float). |
| 346 | |
| 347 | ``parse_constant``, if specified, will be called with one of the |
| 348 | following strings: -Infinity, Infinity, NaN. |
| 349 | This can be used to raise an exception if invalid JSON numbers |
| 350 | are encountered. |
| 351 | |
| 352 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
| 353 | kwarg; otherwise ``JSONDecoder`` is used. |
| 354 | """ |
| 355 | if isinstance(s, str): |
| 356 | if s.startswith('\ufeff'): |
| 357 | raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)", |
| 358 | s, 0) |
| 359 | else: |
| 360 | if not isinstance(s, (bytes, bytearray)): |
| 361 | raise TypeError(f'the JSON object must be str, bytes or bytearray, ' |
| 362 | f'not {s.__class__.__name__}') |
| 363 | s = s.decode(detect_encoding(s), 'surrogatepass') |
| 364 | |
| 365 | if (cls is None and object_hook is None and |
| 366 | parse_int is None and parse_float is None and |
| 367 | parse_constant is None and object_pairs_hook is None |
| 368 | and array_hook is None and not kw): |
| 369 | return _default_decoder.decode(s) |
no test coverage detected
searching dependent graphs…