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