Beispiel #1
0
def patch_json_decoder():
    """
    Apply patches to json.decoder module.

    Used in Python 2.6.
    """
    import json.decoder
    from json.scanner import pattern, Scanner
    constants = json.decoder._CONSTANTS
    constants['nan'] = constants['NaN']
    constants['inf'] = constants['Infinity']
    constants['-inf'] = constants['-Infinity']
    JSONConstant = json.decoder.JSONConstant
    pattern('(-?inf|nan|-?Infinity|NaN|true|false|null)')(JSONConstant)
    json.decoder.JSONScanner = Scanner(json.decoder.ANYTHING)
Beispiel #2
0
def patch_json_decoder():
    """
    Apply patches to json.decoder module.

    Used in Python 2.6.
    """
    import json.decoder
    from json.scanner import pattern, Scanner
    constants = json.decoder._CONSTANTS
    constants['nan'] = constants['NaN']
    constants['inf'] = constants['Infinity']
    constants['-inf'] = constants['-Infinity']
    JSONConstant = json.decoder.JSONConstant
    pattern('(-?inf|nan|-?Infinity|NaN|true|false|null)')(JSONConstant)
    json.decoder.JSONScanner = Scanner(json.decoder.ANYTHING)
Beispiel #3
0
    'NaN': NaN,
    'true': True,
    'false': False,
    'null': None,
}


def JSONConstant(match, context, c=_CONSTANTS):
    s = match.group(0)
    fn = getattr(context, 'parse_constant', None)
    if fn is None:
        rval = c[s]
    else:
        rval = fn(s)
    return rval, None
pattern('(-?Infinity|NaN|true|false|null)')(JSONConstant)


def JSONNumber(match, context):
    match = JSONNumber.regex.match(match.string, *match.span())
    integer, frac, exp = match.groups()
    if frac or exp:
        fn = getattr(context, 'parse_float', None) or float
        res = fn(integer + (frac or '') + (exp or ''))
    else:
        fn = getattr(context, 'parse_int', None) or int
        res = fn(integer)
    return res, None
pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber)

Beispiel #4
0
    'NaN': NaN,
    'true': True,
    'false': False,
    'null': None,
}


def JSONConstant(match, context, c=_CONSTANTS):
    s = match.group(0)
    fn = getattr(context, 'parse_constant', None)
    if fn is None:
        rval = c[s]
    else:
        rval = fn(s)
    return rval, None
pattern('(-?Infinity|NaN|true|false|null)')(JSONConstant)


def JSONNumber(match, context):
    match = JSONNumber.regex.match(match.string, *match.span())
    integer, frac, exp = match.groups()
    if frac or exp:
        fn = getattr(context, 'parse_float', None) or float
        res = fn(integer + (frac or '') + (exp or ''))
    else:
        fn = getattr(context, 'parse_int', None) or int
        res = fn(integer)
    return res, None
pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber)

Beispiel #5
0
        end = _w(s, end + 1).end()
        try:
            value, end = iterscan(s, idx=end, context=context).next()
        except StopIteration:
            raise ValueError(decoder.errmsg("Expecting object", s, end))
        pairs[key] = value
        end = _w(s, end).end()
        nextchar = s[end:end + 1]
        end += 1
        if nextchar == '}':
            break
        if nextchar != ',':
            raise ValueError(decoder.errmsg("Expecting , delimiter", s, end - 1))
        end = _w(s, end).end()
        nextchar = s[end:end + 1]
        end += 1
        if nextchar != '"':
            raise ValueError(decoder.errmsg("Expecting property name", s, end - 1))
    object_hook = getattr(context, 'object_hook', None)
    if object_hook is not None:
        pairs = object_hook(pairs)
    return pairs, end
scanner.pattern(r'{')(JSONObject);

ANYTHING = list(decoder.ANYTHING);
ANYTHING[0] = JSONObject;
JSONScanner = scanner.Scanner(ANYTHING);

class AbstractJSONDecoderOrderedDict(decoder.JSONDecoder):
    _scanner = JSONScanner;