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