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