Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON document) to a Python object. *encoding* determines the encoding used to interpret any :class:`str` objects decoded by this instance (``'utf-8'`` by default). It has no effect when decoding :class:`unicode` obje
(s, encoding=None, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None,
use_decimal=False, **kw)
| 396 | |
| 397 | |
| 398 | def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, |
| 399 | parse_int=None, parse_constant=None, object_pairs_hook=None, |
| 400 | use_decimal=False, **kw): |
| 401 | """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON |
| 402 | document) to a Python object. |
| 403 | |
| 404 | *encoding* determines the encoding used to interpret any |
| 405 | :class:`str` objects decoded by this instance (``'utf-8'`` by |
| 406 | default). It has no effect when decoding :class:`unicode` objects. |
| 407 | |
| 408 | Note that currently only encodings that are a superset of ASCII work, |
| 409 | strings of other encodings should be passed in as :class:`unicode`. |
| 410 | |
| 411 | *object_hook*, if specified, will be called with the result of every |
| 412 | JSON object decoded and its return value will be used in place of the |
| 413 | given :class:`dict`. This can be used to provide custom |
| 414 | deserializations (e.g. to support JSON-RPC class hinting). |
| 415 | |
| 416 | *object_pairs_hook* is an optional function that will be called with |
| 417 | the result of any object literal decode with an ordered list of pairs. |
| 418 | The return value of *object_pairs_hook* will be used instead of the |
| 419 | :class:`dict`. This feature can be used to implement custom decoders |
| 420 | that rely on the order that the key and value pairs are decoded (for |
| 421 | example, :func:`collections.OrderedDict` will remember the order of |
| 422 | insertion). If *object_hook* is also defined, the *object_pairs_hook* |
| 423 | takes priority. |
| 424 | |
| 425 | *parse_float*, if specified, will be called with the string of every |
| 426 | JSON float to be decoded. By default, this is equivalent to |
| 427 | ``float(num_str)``. This can be used to use another datatype or parser |
| 428 | for JSON floats (e.g. :class:`decimal.Decimal`). |
| 429 | |
| 430 | *parse_int*, if specified, will be called with the string of every |
| 431 | JSON int to be decoded. By default, this is equivalent to |
| 432 | ``int(num_str)``. This can be used to use another datatype or parser |
| 433 | for JSON integers (e.g. :class:`float`). |
| 434 | |
| 435 | *parse_constant*, if specified, will be called with one of the |
| 436 | following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This |
| 437 | can be used to raise an exception if invalid JSON numbers are |
| 438 | encountered. |
| 439 | |
| 440 | If *use_decimal* is true (default: ``False``) then it implies |
| 441 | parse_float=decimal.Decimal for parity with ``dump``. |
| 442 | |
| 443 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
| 444 | kwarg. |
| 445 | |
| 446 | """ |
| 447 | if (cls is None and encoding is None and object_hook is None and |
| 448 | parse_int is None and parse_float is None and |
| 449 | parse_constant is None and object_pairs_hook is None |
| 450 | and not use_decimal and not kw): |
| 451 | return _default_decoder.decode(s) |
| 452 | if cls is None: |
| 453 | cls = JSONDecoder |
| 454 | if object_hook is not None: |
| 455 | kw['object_hook'] = object_hook |