"""Implementation of JSONDecoder """ import re from json import scanner # JSONDecodeError is exported for backwards compatibility __all__ = ['JSONDecoder', 'JSONDecodeError'] JSONDecodeError = scanner.JSONDecodeError _CONSTANTS = { '-Infinity': float('-inf'), 'Infinity': float('inf'), 'NaN': float('nan'), } WHITESPACE = re.compile(r'[ \t\n\r]*', (re.VERBOSE | re.MULTILINE | re.DOTALL)) class JSONDecoder(object): """Simple JSON decoder Performs the following translations in decoding by default: +---------------+-------------------+ | JSON | Python | +===============+===================+ | object | dict | +---------------+-------------------+ | array | list | +---------------+-------------------+ | string | str | +---------------+-------------------+ | number (int) | int | +---------------+-------------------+ | number (real) | float | +---------------+-------------------+ | true | True | +---------------+-------------------+ | false | False | +---------------+-------------------+ | null | None | +---------------+-------------------+ It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their corresponding ``float`` values, which is outside the JSON spec. """ def __init__(self, *, parse_object=None, parse_ordered_object=None, parse_string=None, parse_key=None, parse_array=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, **kw): """``parse_object``, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the given ``dict``. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting). ``parse_ordered_object``, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value of ``object_pairs_hook`` will be used instead of the ``dict``. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict will remember the order of insertion). If ``parse_object`` is also defined, the ``parse_ordered_object`` takes priority. ``parse_key``, if specified, will be called with the string of every JSON object's key to be decoded. ``parse_string``, if specified, will be called with the string of every JSON object's value to be decoded. ``parse_Array``, if specified, will be called with the list of every JSON array decoded and its return will be used in place of the given ``list``. This feature can be used to provide custom decoders (for example, ``tuple``). ``parse_float``, if specified, will be called with the string of every JSON float to be decoded. By default this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal). ``parse_int``, if specified, will be called with the string of every JSON int to be decoded. By default this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float). ``parse_constant``, if specified, will be called with one of the following strings: -Infinity, Infinity, NaN. This can be used to raise an exception if invalid JSON numbers are encountered. If ``strict`` is false (true is the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0-31 range, including ``'\\t'`` (tab), ``'\\n'``, ``'\\r'`` and ``'\\0'``. """ # Backwards compatibility parse_object = kw.get('object_hook', parse_object) parse_ordered_object = kw.get('object_pairs_hook', parse_ordered_object) self.parse_ordered_object = parse_ordered_object self.parse_object = parse_object self.parse_key = parse_key self.parse_array = parse_array self.parse_string = parse_string self.parse_float = parse_float or float self.parse_int = parse_int or int self.parse_constant = parse_constant or _CONSTANTS.__getitem__ self.strict = strict self.memo = {} self.scan_once = scanner.make_scanner(self) def decode(self, s, _w=WHITESPACE.match): """Return the Python representation of ``s`` (a ``str`` instance containing a JSON document). """ obj, end = self.raw_decode(s, idx=_w(s, 0).end()) end = _w(s, end).end() if end != len(s): raise JSONDecodeError("Extra data", s, end) return obj def raw_decode(self, s, idx=0): """Decode a JSON document from ``s`` (a ``str`` beginning with a JSON document) and return a 2-tuple of the Python representation and the index in ``s`` where the document ended. This can be used to decode a JSON document from a string that may have extraneous data at the end. """ try: obj, end = self.scan_once(s, idx) except StopIteration as err: raise JSONDecodeError("Expecting value", s, err.value) from None return obj, end