Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../json
File: decoder.py
"""Implementation of JSONDecoder
[0] Fix | Delete
"""
[1] Fix | Delete
import re
[2] Fix | Delete
[3] Fix | Delete
from json import scanner
[4] Fix | Delete
try:
[5] Fix | Delete
from _json import scanstring as c_scanstring
[6] Fix | Delete
except ImportError:
[7] Fix | Delete
c_scanstring = None
[8] Fix | Delete
[9] Fix | Delete
__all__ = ['JSONDecoder', 'JSONDecodeError']
[10] Fix | Delete
[11] Fix | Delete
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
[12] Fix | Delete
[13] Fix | Delete
NaN = float('nan')
[14] Fix | Delete
PosInf = float('inf')
[15] Fix | Delete
NegInf = float('-inf')
[16] Fix | Delete
[17] Fix | Delete
[18] Fix | Delete
class JSONDecodeError(ValueError):
[19] Fix | Delete
"""Subclass of ValueError with the following additional properties:
[20] Fix | Delete
[21] Fix | Delete
msg: The unformatted error message
[22] Fix | Delete
doc: The JSON document being parsed
[23] Fix | Delete
pos: The start index of doc where parsing failed
[24] Fix | Delete
lineno: The line corresponding to pos
[25] Fix | Delete
colno: The column corresponding to pos
[26] Fix | Delete
[27] Fix | Delete
"""
[28] Fix | Delete
# Note that this exception is used from _json
[29] Fix | Delete
def __init__(self, msg, doc, pos):
[30] Fix | Delete
lineno = doc.count('\n', 0, pos) + 1
[31] Fix | Delete
colno = pos - doc.rfind('\n', 0, pos)
[32] Fix | Delete
errmsg = '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
[33] Fix | Delete
ValueError.__init__(self, errmsg)
[34] Fix | Delete
self.msg = msg
[35] Fix | Delete
self.doc = doc
[36] Fix | Delete
self.pos = pos
[37] Fix | Delete
self.lineno = lineno
[38] Fix | Delete
self.colno = colno
[39] Fix | Delete
[40] Fix | Delete
def __reduce__(self):
[41] Fix | Delete
return self.__class__, (self.msg, self.doc, self.pos)
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
_CONSTANTS = {
[45] Fix | Delete
'-Infinity': NegInf,
[46] Fix | Delete
'Infinity': PosInf,
[47] Fix | Delete
'NaN': NaN,
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
[51] Fix | Delete
STRINGCHUNK = re.compile(r'(.*?)(["\\\x00-\x1f])', FLAGS)
[52] Fix | Delete
BACKSLASH = {
[53] Fix | Delete
'"': '"', '\\': '\\', '/': '/',
[54] Fix | Delete
'b': '\b', 'f': '\f', 'n': '\n', 'r': '\r', 't': '\t',
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
def _decode_uXXXX(s, pos):
[58] Fix | Delete
esc = s[pos + 1:pos + 5]
[59] Fix | Delete
if len(esc) == 4 and esc[1] not in 'xX':
[60] Fix | Delete
try:
[61] Fix | Delete
return int(esc, 16)
[62] Fix | Delete
except ValueError:
[63] Fix | Delete
pass
[64] Fix | Delete
msg = "Invalid \\uXXXX escape"
[65] Fix | Delete
raise JSONDecodeError(msg, s, pos)
[66] Fix | Delete
[67] Fix | Delete
def py_scanstring(s, end, strict=True,
[68] Fix | Delete
_b=BACKSLASH, _m=STRINGCHUNK.match):
[69] Fix | Delete
"""Scan the string s for a JSON string. End is the index of the
[70] Fix | Delete
character in s after the quote that started the JSON string.
[71] Fix | Delete
Unescapes all valid JSON string escape sequences and raises ValueError
[72] Fix | Delete
on attempt to decode an invalid string. If strict is False then literal
[73] Fix | Delete
control characters are allowed in the string.
[74] Fix | Delete
[75] Fix | Delete
Returns a tuple of the decoded string and the index of the character in s
[76] Fix | Delete
after the end quote."""
[77] Fix | Delete
chunks = []
[78] Fix | Delete
_append = chunks.append
[79] Fix | Delete
begin = end - 1
[80] Fix | Delete
while 1:
[81] Fix | Delete
chunk = _m(s, end)
[82] Fix | Delete
if chunk is None:
[83] Fix | Delete
raise JSONDecodeError("Unterminated string starting at", s, begin)
[84] Fix | Delete
end = chunk.end()
[85] Fix | Delete
content, terminator = chunk.groups()
[86] Fix | Delete
# Content is contains zero or more unescaped string characters
[87] Fix | Delete
if content:
[88] Fix | Delete
_append(content)
[89] Fix | Delete
# Terminator is the end of string, a literal control character,
[90] Fix | Delete
# or a backslash denoting that an escape sequence follows
[91] Fix | Delete
if terminator == '"':
[92] Fix | Delete
break
[93] Fix | Delete
elif terminator != '\\':
[94] Fix | Delete
if strict:
[95] Fix | Delete
#msg = "Invalid control character %r at" % (terminator,)
[96] Fix | Delete
msg = "Invalid control character {0!r} at".format(terminator)
[97] Fix | Delete
raise JSONDecodeError(msg, s, end)
[98] Fix | Delete
else:
[99] Fix | Delete
_append(terminator)
[100] Fix | Delete
continue
[101] Fix | Delete
try:
[102] Fix | Delete
esc = s[end]
[103] Fix | Delete
except IndexError:
[104] Fix | Delete
raise JSONDecodeError("Unterminated string starting at", s, begin)
[105] Fix | Delete
# If not a unicode escape sequence, must be in the lookup table
[106] Fix | Delete
if esc != 'u':
[107] Fix | Delete
try:
[108] Fix | Delete
char = _b[esc]
[109] Fix | Delete
except KeyError:
[110] Fix | Delete
msg = "Invalid \\escape: {0!r}".format(esc)
[111] Fix | Delete
raise JSONDecodeError(msg, s, end)
[112] Fix | Delete
end += 1
[113] Fix | Delete
else:
[114] Fix | Delete
uni = _decode_uXXXX(s, end)
[115] Fix | Delete
end += 5
[116] Fix | Delete
if 0xd800 <= uni <= 0xdbff and s[end:end + 2] == '\\u':
[117] Fix | Delete
uni2 = _decode_uXXXX(s, end + 1)
[118] Fix | Delete
if 0xdc00 <= uni2 <= 0xdfff:
[119] Fix | Delete
uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00))
[120] Fix | Delete
end += 6
[121] Fix | Delete
char = chr(uni)
[122] Fix | Delete
_append(char)
[123] Fix | Delete
return ''.join(chunks), end
[124] Fix | Delete
[125] Fix | Delete
[126] Fix | Delete
# Use speedup if available
[127] Fix | Delete
scanstring = c_scanstring or py_scanstring
[128] Fix | Delete
[129] Fix | Delete
WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS)
[130] Fix | Delete
WHITESPACE_STR = ' \t\n\r'
[131] Fix | Delete
[132] Fix | Delete
[133] Fix | Delete
def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
[134] Fix | Delete
memo=None, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
[135] Fix | Delete
s, end = s_and_end
[136] Fix | Delete
pairs = []
[137] Fix | Delete
pairs_append = pairs.append
[138] Fix | Delete
# Backwards compatibility
[139] Fix | Delete
if memo is None:
[140] Fix | Delete
memo = {}
[141] Fix | Delete
memo_get = memo.setdefault
[142] Fix | Delete
# Use a slice to prevent IndexError from being raised, the following
[143] Fix | Delete
# check will raise a more specific ValueError if the string is empty
[144] Fix | Delete
nextchar = s[end:end + 1]
[145] Fix | Delete
# Normally we expect nextchar == '"'
[146] Fix | Delete
if nextchar != '"':
[147] Fix | Delete
if nextchar in _ws:
[148] Fix | Delete
end = _w(s, end).end()
[149] Fix | Delete
nextchar = s[end:end + 1]
[150] Fix | Delete
# Trivial empty object
[151] Fix | Delete
if nextchar == '}':
[152] Fix | Delete
if object_pairs_hook is not None:
[153] Fix | Delete
result = object_pairs_hook(pairs)
[154] Fix | Delete
return result, end + 1
[155] Fix | Delete
pairs = {}
[156] Fix | Delete
if object_hook is not None:
[157] Fix | Delete
pairs = object_hook(pairs)
[158] Fix | Delete
return pairs, end + 1
[159] Fix | Delete
elif nextchar != '"':
[160] Fix | Delete
raise JSONDecodeError(
[161] Fix | Delete
"Expecting property name enclosed in double quotes", s, end)
[162] Fix | Delete
end += 1
[163] Fix | Delete
while True:
[164] Fix | Delete
key, end = scanstring(s, end, strict)
[165] Fix | Delete
key = memo_get(key, key)
[166] Fix | Delete
# To skip some function call overhead we optimize the fast paths where
[167] Fix | Delete
# the JSON key separator is ": " or just ":".
[168] Fix | Delete
if s[end:end + 1] != ':':
[169] Fix | Delete
end = _w(s, end).end()
[170] Fix | Delete
if s[end:end + 1] != ':':
[171] Fix | Delete
raise JSONDecodeError("Expecting ':' delimiter", s, end)
[172] Fix | Delete
end += 1
[173] Fix | Delete
[174] Fix | Delete
try:
[175] Fix | Delete
if s[end] in _ws:
[176] Fix | Delete
end += 1
[177] Fix | Delete
if s[end] in _ws:
[178] Fix | Delete
end = _w(s, end + 1).end()
[179] Fix | Delete
except IndexError:
[180] Fix | Delete
pass
[181] Fix | Delete
[182] Fix | Delete
try:
[183] Fix | Delete
value, end = scan_once(s, end)
[184] Fix | Delete
except StopIteration as err:
[185] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[186] Fix | Delete
pairs_append((key, value))
[187] Fix | Delete
try:
[188] Fix | Delete
nextchar = s[end]
[189] Fix | Delete
if nextchar in _ws:
[190] Fix | Delete
end = _w(s, end + 1).end()
[191] Fix | Delete
nextchar = s[end]
[192] Fix | Delete
except IndexError:
[193] Fix | Delete
nextchar = ''
[194] Fix | Delete
end += 1
[195] Fix | Delete
[196] Fix | Delete
if nextchar == '}':
[197] Fix | Delete
break
[198] Fix | Delete
elif nextchar != ',':
[199] Fix | Delete
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
[200] Fix | Delete
end = _w(s, end).end()
[201] Fix | Delete
nextchar = s[end:end + 1]
[202] Fix | Delete
end += 1
[203] Fix | Delete
if nextchar != '"':
[204] Fix | Delete
raise JSONDecodeError(
[205] Fix | Delete
"Expecting property name enclosed in double quotes", s, end - 1)
[206] Fix | Delete
if object_pairs_hook is not None:
[207] Fix | Delete
result = object_pairs_hook(pairs)
[208] Fix | Delete
return result, end
[209] Fix | Delete
pairs = dict(pairs)
[210] Fix | Delete
if object_hook is not None:
[211] Fix | Delete
pairs = object_hook(pairs)
[212] Fix | Delete
return pairs, end
[213] Fix | Delete
[214] Fix | Delete
def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
[215] Fix | Delete
s, end = s_and_end
[216] Fix | Delete
values = []
[217] Fix | Delete
nextchar = s[end:end + 1]
[218] Fix | Delete
if nextchar in _ws:
[219] Fix | Delete
end = _w(s, end + 1).end()
[220] Fix | Delete
nextchar = s[end:end + 1]
[221] Fix | Delete
# Look-ahead for trivial empty array
[222] Fix | Delete
if nextchar == ']':
[223] Fix | Delete
return values, end + 1
[224] Fix | Delete
_append = values.append
[225] Fix | Delete
while True:
[226] Fix | Delete
try:
[227] Fix | Delete
value, end = scan_once(s, end)
[228] Fix | Delete
except StopIteration as err:
[229] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[230] Fix | Delete
_append(value)
[231] Fix | Delete
nextchar = s[end:end + 1]
[232] Fix | Delete
if nextchar in _ws:
[233] Fix | Delete
end = _w(s, end + 1).end()
[234] Fix | Delete
nextchar = s[end:end + 1]
[235] Fix | Delete
end += 1
[236] Fix | Delete
if nextchar == ']':
[237] Fix | Delete
break
[238] Fix | Delete
elif nextchar != ',':
[239] Fix | Delete
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
[240] Fix | Delete
try:
[241] Fix | Delete
if s[end] in _ws:
[242] Fix | Delete
end += 1
[243] Fix | Delete
if s[end] in _ws:
[244] Fix | Delete
end = _w(s, end + 1).end()
[245] Fix | Delete
except IndexError:
[246] Fix | Delete
pass
[247] Fix | Delete
[248] Fix | Delete
return values, end
[249] Fix | Delete
[250] Fix | Delete
[251] Fix | Delete
class JSONDecoder(object):
[252] Fix | Delete
"""Simple JSON <http://json.org> decoder
[253] Fix | Delete
[254] Fix | Delete
Performs the following translations in decoding by default:
[255] Fix | Delete
[256] Fix | Delete
+---------------+-------------------+
[257] Fix | Delete
| JSON | Python |
[258] Fix | Delete
+===============+===================+
[259] Fix | Delete
| object | dict |
[260] Fix | Delete
+---------------+-------------------+
[261] Fix | Delete
| array | list |
[262] Fix | Delete
+---------------+-------------------+
[263] Fix | Delete
| string | str |
[264] Fix | Delete
+---------------+-------------------+
[265] Fix | Delete
| number (int) | int |
[266] Fix | Delete
+---------------+-------------------+
[267] Fix | Delete
| number (real) | float |
[268] Fix | Delete
+---------------+-------------------+
[269] Fix | Delete
| true | True |
[270] Fix | Delete
+---------------+-------------------+
[271] Fix | Delete
| false | False |
[272] Fix | Delete
+---------------+-------------------+
[273] Fix | Delete
| null | None |
[274] Fix | Delete
+---------------+-------------------+
[275] Fix | Delete
[276] Fix | Delete
It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as
[277] Fix | Delete
their corresponding ``float`` values, which is outside the JSON spec.
[278] Fix | Delete
[279] Fix | Delete
"""
[280] Fix | Delete
[281] Fix | Delete
def __init__(self, *, object_hook=None, parse_float=None,
[282] Fix | Delete
parse_int=None, parse_constant=None, strict=True,
[283] Fix | Delete
object_pairs_hook=None):
[284] Fix | Delete
"""``object_hook``, if specified, will be called with the result
[285] Fix | Delete
of every JSON object decoded and its return value will be used in
[286] Fix | Delete
place of the given ``dict``. This can be used to provide custom
[287] Fix | Delete
deserializations (e.g. to support JSON-RPC class hinting).
[288] Fix | Delete
[289] Fix | Delete
``object_pairs_hook``, if specified will be called with the result of
[290] Fix | Delete
every JSON object decoded with an ordered list of pairs. The return
[291] Fix | Delete
value of ``object_pairs_hook`` will be used instead of the ``dict``.
[292] Fix | Delete
This feature can be used to implement custom decoders that rely on the
[293] Fix | Delete
order that the key and value pairs are decoded (for example,
[294] Fix | Delete
collections.OrderedDict will remember the order of insertion). If
[295] Fix | Delete
``object_hook`` is also defined, the ``object_pairs_hook`` takes
[296] Fix | Delete
priority.
[297] Fix | Delete
[298] Fix | Delete
``parse_float``, if specified, will be called with the string
[299] Fix | Delete
of every JSON float to be decoded. By default this is equivalent to
[300] Fix | Delete
float(num_str). This can be used to use another datatype or parser
[301] Fix | Delete
for JSON floats (e.g. decimal.Decimal).
[302] Fix | Delete
[303] Fix | Delete
``parse_int``, if specified, will be called with the string
[304] Fix | Delete
of every JSON int to be decoded. By default this is equivalent to
[305] Fix | Delete
int(num_str). This can be used to use another datatype or parser
[306] Fix | Delete
for JSON integers (e.g. float).
[307] Fix | Delete
[308] Fix | Delete
``parse_constant``, if specified, will be called with one of the
[309] Fix | Delete
following strings: -Infinity, Infinity, NaN.
[310] Fix | Delete
This can be used to raise an exception if invalid JSON numbers
[311] Fix | Delete
are encountered.
[312] Fix | Delete
[313] Fix | Delete
If ``strict`` is false (true is the default), then control
[314] Fix | Delete
characters will be allowed inside strings. Control characters in
[315] Fix | Delete
this context are those with character codes in the 0-31 range,
[316] Fix | Delete
including ``'\\t'`` (tab), ``'\\n'``, ``'\\r'`` and ``'\\0'``.
[317] Fix | Delete
[318] Fix | Delete
"""
[319] Fix | Delete
self.object_hook = object_hook
[320] Fix | Delete
self.parse_float = parse_float or float
[321] Fix | Delete
self.parse_int = parse_int or int
[322] Fix | Delete
self.parse_constant = parse_constant or _CONSTANTS.__getitem__
[323] Fix | Delete
self.strict = strict
[324] Fix | Delete
self.object_pairs_hook = object_pairs_hook
[325] Fix | Delete
self.parse_object = JSONObject
[326] Fix | Delete
self.parse_array = JSONArray
[327] Fix | Delete
self.parse_string = scanstring
[328] Fix | Delete
self.memo = {}
[329] Fix | Delete
self.scan_once = scanner.make_scanner(self)
[330] Fix | Delete
[331] Fix | Delete
[332] Fix | Delete
def decode(self, s, _w=WHITESPACE.match):
[333] Fix | Delete
"""Return the Python representation of ``s`` (a ``str`` instance
[334] Fix | Delete
containing a JSON document).
[335] Fix | Delete
[336] Fix | Delete
"""
[337] Fix | Delete
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
[338] Fix | Delete
end = _w(s, end).end()
[339] Fix | Delete
if end != len(s):
[340] Fix | Delete
raise JSONDecodeError("Extra data", s, end)
[341] Fix | Delete
return obj
[342] Fix | Delete
[343] Fix | Delete
def raw_decode(self, s, idx=0):
[344] Fix | Delete
"""Decode a JSON document from ``s`` (a ``str`` beginning with
[345] Fix | Delete
a JSON document) and return a 2-tuple of the Python
[346] Fix | Delete
representation and the index in ``s`` where the document ended.
[347] Fix | Delete
[348] Fix | Delete
This can be used to decode a JSON document from a string that may
[349] Fix | Delete
have extraneous data at the end.
[350] Fix | Delete
[351] Fix | Delete
"""
[352] Fix | Delete
try:
[353] Fix | Delete
obj, end = self.scan_once(s, idx)
[354] Fix | Delete
except StopIteration as err:
[355] Fix | Delete
raise JSONDecodeError("Expecting value", s, err.value) from None
[356] Fix | Delete
return obj, end
[357] Fix | Delete
[358] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function