Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3..../json
File: encoder.py
"""Implementation of JSONEncoder
[0] Fix | Delete
"""
[1] Fix | Delete
import re
[2] Fix | Delete
[3] Fix | Delete
try:
[4] Fix | Delete
from _json import encode_basestring_ascii as c_encode_basestring_ascii
[5] Fix | Delete
except ImportError:
[6] Fix | Delete
c_encode_basestring_ascii = None
[7] Fix | Delete
try:
[8] Fix | Delete
from _json import encode_basestring as c_encode_basestring
[9] Fix | Delete
except ImportError:
[10] Fix | Delete
c_encode_basestring = None
[11] Fix | Delete
try:
[12] Fix | Delete
from _json import make_encoder as c_make_encoder
[13] Fix | Delete
except ImportError:
[14] Fix | Delete
c_make_encoder = None
[15] Fix | Delete
[16] Fix | Delete
ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]')
[17] Fix | Delete
ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])')
[18] Fix | Delete
HAS_UTF8 = re.compile(b'[\x80-\xff]')
[19] Fix | Delete
ESCAPE_DCT = {
[20] Fix | Delete
'\\': '\\\\',
[21] Fix | Delete
'"': '\\"',
[22] Fix | Delete
'\b': '\\b',
[23] Fix | Delete
'\f': '\\f',
[24] Fix | Delete
'\n': '\\n',
[25] Fix | Delete
'\r': '\\r',
[26] Fix | Delete
'\t': '\\t',
[27] Fix | Delete
}
[28] Fix | Delete
for i in range(0x20):
[29] Fix | Delete
ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i))
[30] Fix | Delete
#ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,))
[31] Fix | Delete
[32] Fix | Delete
INFINITY = float('inf')
[33] Fix | Delete
[34] Fix | Delete
def py_encode_basestring(s):
[35] Fix | Delete
"""Return a JSON representation of a Python string
[36] Fix | Delete
[37] Fix | Delete
"""
[38] Fix | Delete
def replace(match):
[39] Fix | Delete
return ESCAPE_DCT[match.group(0)]
[40] Fix | Delete
return '"' + ESCAPE.sub(replace, s) + '"'
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
encode_basestring = (c_encode_basestring or py_encode_basestring)
[44] Fix | Delete
[45] Fix | Delete
[46] Fix | Delete
def py_encode_basestring_ascii(s):
[47] Fix | Delete
"""Return an ASCII-only JSON representation of a Python string
[48] Fix | Delete
[49] Fix | Delete
"""
[50] Fix | Delete
def replace(match):
[51] Fix | Delete
s = match.group(0)
[52] Fix | Delete
try:
[53] Fix | Delete
return ESCAPE_DCT[s]
[54] Fix | Delete
except KeyError:
[55] Fix | Delete
n = ord(s)
[56] Fix | Delete
if n < 0x10000:
[57] Fix | Delete
return '\\u{0:04x}'.format(n)
[58] Fix | Delete
#return '\\u%04x' % (n,)
[59] Fix | Delete
else:
[60] Fix | Delete
# surrogate pair
[61] Fix | Delete
n -= 0x10000
[62] Fix | Delete
s1 = 0xd800 | ((n >> 10) & 0x3ff)
[63] Fix | Delete
s2 = 0xdc00 | (n & 0x3ff)
[64] Fix | Delete
return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
[65] Fix | Delete
return '"' + ESCAPE_ASCII.sub(replace, s) + '"'
[66] Fix | Delete
[67] Fix | Delete
[68] Fix | Delete
encode_basestring_ascii = (
[69] Fix | Delete
c_encode_basestring_ascii or py_encode_basestring_ascii)
[70] Fix | Delete
[71] Fix | Delete
class JSONEncoder(object):
[72] Fix | Delete
"""Extensible JSON <http://json.org> encoder for Python data structures.
[73] Fix | Delete
[74] Fix | Delete
Supports the following objects and types by default:
[75] Fix | Delete
[76] Fix | Delete
+-------------------+---------------+
[77] Fix | Delete
| Python | JSON |
[78] Fix | Delete
+===================+===============+
[79] Fix | Delete
| dict | object |
[80] Fix | Delete
+-------------------+---------------+
[81] Fix | Delete
| list, tuple | array |
[82] Fix | Delete
+-------------------+---------------+
[83] Fix | Delete
| str | string |
[84] Fix | Delete
+-------------------+---------------+
[85] Fix | Delete
| int, float | number |
[86] Fix | Delete
+-------------------+---------------+
[87] Fix | Delete
| True | true |
[88] Fix | Delete
+-------------------+---------------+
[89] Fix | Delete
| False | false |
[90] Fix | Delete
+-------------------+---------------+
[91] Fix | Delete
| None | null |
[92] Fix | Delete
+-------------------+---------------+
[93] Fix | Delete
[94] Fix | Delete
To extend this to recognize other objects, subclass and implement a
[95] Fix | Delete
``.default()`` method with another method that returns a serializable
[96] Fix | Delete
object for ``o`` if possible, otherwise it should call the superclass
[97] Fix | Delete
implementation (to raise ``TypeError``).
[98] Fix | Delete
[99] Fix | Delete
"""
[100] Fix | Delete
item_separator = ', '
[101] Fix | Delete
key_separator = ': '
[102] Fix | Delete
def __init__(self, *, skipkeys=False, ensure_ascii=True,
[103] Fix | Delete
check_circular=True, allow_nan=True, sort_keys=False,
[104] Fix | Delete
indent=None, separators=None, default=None):
[105] Fix | Delete
"""Constructor for JSONEncoder, with sensible defaults.
[106] Fix | Delete
[107] Fix | Delete
If skipkeys is false, then it is a TypeError to attempt
[108] Fix | Delete
encoding of keys that are not str, int, float or None. If
[109] Fix | Delete
skipkeys is True, such items are simply skipped.
[110] Fix | Delete
[111] Fix | Delete
If ensure_ascii is true, the output is guaranteed to be str
[112] Fix | Delete
objects with all incoming non-ASCII characters escaped. If
[113] Fix | Delete
ensure_ascii is false, the output can contain non-ASCII characters.
[114] Fix | Delete
[115] Fix | Delete
If check_circular is true, then lists, dicts, and custom encoded
[116] Fix | Delete
objects will be checked for circular references during encoding to
[117] Fix | Delete
prevent an infinite recursion (which would cause an OverflowError).
[118] Fix | Delete
Otherwise, no such check takes place.
[119] Fix | Delete
[120] Fix | Delete
If allow_nan is true, then NaN, Infinity, and -Infinity will be
[121] Fix | Delete
encoded as such. This behavior is not JSON specification compliant,
[122] Fix | Delete
but is consistent with most JavaScript based encoders and decoders.
[123] Fix | Delete
Otherwise, it will be a ValueError to encode such floats.
[124] Fix | Delete
[125] Fix | Delete
If sort_keys is true, then the output of dictionaries will be
[126] Fix | Delete
sorted by key; this is useful for regression tests to ensure
[127] Fix | Delete
that JSON serializations can be compared on a day-to-day basis.
[128] Fix | Delete
[129] Fix | Delete
If indent is a non-negative integer, then JSON array
[130] Fix | Delete
elements and object members will be pretty-printed with that
[131] Fix | Delete
indent level. An indent level of 0 will only insert newlines.
[132] Fix | Delete
None is the most compact representation.
[133] Fix | Delete
[134] Fix | Delete
If specified, separators should be an (item_separator, key_separator)
[135] Fix | Delete
tuple. The default is (', ', ': ') if *indent* is ``None`` and
[136] Fix | Delete
(',', ': ') otherwise. To get the most compact JSON representation,
[137] Fix | Delete
you should specify (',', ':') to eliminate whitespace.
[138] Fix | Delete
[139] Fix | Delete
If specified, default is a function that gets called for objects
[140] Fix | Delete
that can't otherwise be serialized. It should return a JSON encodable
[141] Fix | Delete
version of the object or raise a ``TypeError``.
[142] Fix | Delete
[143] Fix | Delete
"""
[144] Fix | Delete
[145] Fix | Delete
self.skipkeys = skipkeys
[146] Fix | Delete
self.ensure_ascii = ensure_ascii
[147] Fix | Delete
self.check_circular = check_circular
[148] Fix | Delete
self.allow_nan = allow_nan
[149] Fix | Delete
self.sort_keys = sort_keys
[150] Fix | Delete
self.indent = indent
[151] Fix | Delete
if separators is not None:
[152] Fix | Delete
self.item_separator, self.key_separator = separators
[153] Fix | Delete
elif indent is not None:
[154] Fix | Delete
self.item_separator = ','
[155] Fix | Delete
if default is not None:
[156] Fix | Delete
self.default = default
[157] Fix | Delete
[158] Fix | Delete
def default(self, o):
[159] Fix | Delete
"""Implement this method in a subclass such that it returns
[160] Fix | Delete
a serializable object for ``o``, or calls the base implementation
[161] Fix | Delete
(to raise a ``TypeError``).
[162] Fix | Delete
[163] Fix | Delete
For example, to support arbitrary iterators, you could
[164] Fix | Delete
implement default like this::
[165] Fix | Delete
[166] Fix | Delete
def default(self, o):
[167] Fix | Delete
try:
[168] Fix | Delete
iterable = iter(o)
[169] Fix | Delete
except TypeError:
[170] Fix | Delete
pass
[171] Fix | Delete
else:
[172] Fix | Delete
return list(iterable)
[173] Fix | Delete
# Let the base class default method raise the TypeError
[174] Fix | Delete
return JSONEncoder.default(self, o)
[175] Fix | Delete
[176] Fix | Delete
"""
[177] Fix | Delete
raise TypeError("Object of type '%s' is not JSON serializable" %
[178] Fix | Delete
o.__class__.__name__)
[179] Fix | Delete
[180] Fix | Delete
def encode(self, o):
[181] Fix | Delete
"""Return a JSON string representation of a Python data structure.
[182] Fix | Delete
[183] Fix | Delete
>>> from json.encoder import JSONEncoder
[184] Fix | Delete
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
[185] Fix | Delete
'{"foo": ["bar", "baz"]}'
[186] Fix | Delete
[187] Fix | Delete
"""
[188] Fix | Delete
# This is for extremely simple cases and benchmarks.
[189] Fix | Delete
if isinstance(o, str):
[190] Fix | Delete
if self.ensure_ascii:
[191] Fix | Delete
return encode_basestring_ascii(o)
[192] Fix | Delete
else:
[193] Fix | Delete
return encode_basestring(o)
[194] Fix | Delete
# This doesn't pass the iterator directly to ''.join() because the
[195] Fix | Delete
# exceptions aren't as detailed. The list call should be roughly
[196] Fix | Delete
# equivalent to the PySequence_Fast that ''.join() would do.
[197] Fix | Delete
chunks = self.iterencode(o, _one_shot=True)
[198] Fix | Delete
if not isinstance(chunks, (list, tuple)):
[199] Fix | Delete
chunks = list(chunks)
[200] Fix | Delete
return ''.join(chunks)
[201] Fix | Delete
[202] Fix | Delete
def iterencode(self, o, _one_shot=False):
[203] Fix | Delete
"""Encode the given object and yield each string
[204] Fix | Delete
representation as available.
[205] Fix | Delete
[206] Fix | Delete
For example::
[207] Fix | Delete
[208] Fix | Delete
for chunk in JSONEncoder().iterencode(bigobject):
[209] Fix | Delete
mysocket.write(chunk)
[210] Fix | Delete
[211] Fix | Delete
"""
[212] Fix | Delete
if self.check_circular:
[213] Fix | Delete
markers = {}
[214] Fix | Delete
else:
[215] Fix | Delete
markers = None
[216] Fix | Delete
if self.ensure_ascii:
[217] Fix | Delete
_encoder = encode_basestring_ascii
[218] Fix | Delete
else:
[219] Fix | Delete
_encoder = encode_basestring
[220] Fix | Delete
[221] Fix | Delete
def floatstr(o, allow_nan=self.allow_nan,
[222] Fix | Delete
_repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY):
[223] Fix | Delete
# Check for specials. Note that this type of test is processor
[224] Fix | Delete
# and/or platform-specific, so do tests which don't depend on the
[225] Fix | Delete
# internals.
[226] Fix | Delete
[227] Fix | Delete
if o != o:
[228] Fix | Delete
text = 'NaN'
[229] Fix | Delete
elif o == _inf:
[230] Fix | Delete
text = 'Infinity'
[231] Fix | Delete
elif o == _neginf:
[232] Fix | Delete
text = '-Infinity'
[233] Fix | Delete
else:
[234] Fix | Delete
return _repr(o)
[235] Fix | Delete
[236] Fix | Delete
if not allow_nan:
[237] Fix | Delete
raise ValueError(
[238] Fix | Delete
"Out of range float values are not JSON compliant: " +
[239] Fix | Delete
repr(o))
[240] Fix | Delete
[241] Fix | Delete
return text
[242] Fix | Delete
[243] Fix | Delete
[244] Fix | Delete
if (_one_shot and c_make_encoder is not None
[245] Fix | Delete
and self.indent is None):
[246] Fix | Delete
_iterencode = c_make_encoder(
[247] Fix | Delete
markers, self.default, _encoder, self.indent,
[248] Fix | Delete
self.key_separator, self.item_separator, self.sort_keys,
[249] Fix | Delete
self.skipkeys, self.allow_nan)
[250] Fix | Delete
else:
[251] Fix | Delete
_iterencode = _make_iterencode(
[252] Fix | Delete
markers, self.default, _encoder, self.indent, floatstr,
[253] Fix | Delete
self.key_separator, self.item_separator, self.sort_keys,
[254] Fix | Delete
self.skipkeys, _one_shot)
[255] Fix | Delete
return _iterencode(o, 0)
[256] Fix | Delete
[257] Fix | Delete
def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
[258] Fix | Delete
_key_separator, _item_separator, _sort_keys, _skipkeys, _one_shot,
[259] Fix | Delete
## HACK: hand-optimized bytecode; turn globals into locals
[260] Fix | Delete
ValueError=ValueError,
[261] Fix | Delete
dict=dict,
[262] Fix | Delete
float=float,
[263] Fix | Delete
id=id,
[264] Fix | Delete
int=int,
[265] Fix | Delete
isinstance=isinstance,
[266] Fix | Delete
list=list,
[267] Fix | Delete
str=str,
[268] Fix | Delete
tuple=tuple,
[269] Fix | Delete
_intstr=int.__str__,
[270] Fix | Delete
):
[271] Fix | Delete
[272] Fix | Delete
if _indent is not None and not isinstance(_indent, str):
[273] Fix | Delete
_indent = ' ' * _indent
[274] Fix | Delete
[275] Fix | Delete
def _iterencode_list(lst, _current_indent_level):
[276] Fix | Delete
if not lst:
[277] Fix | Delete
yield '[]'
[278] Fix | Delete
return
[279] Fix | Delete
if markers is not None:
[280] Fix | Delete
markerid = id(lst)
[281] Fix | Delete
if markerid in markers:
[282] Fix | Delete
raise ValueError("Circular reference detected")
[283] Fix | Delete
markers[markerid] = lst
[284] Fix | Delete
buf = '['
[285] Fix | Delete
if _indent is not None:
[286] Fix | Delete
_current_indent_level += 1
[287] Fix | Delete
newline_indent = '\n' + _indent * _current_indent_level
[288] Fix | Delete
separator = _item_separator + newline_indent
[289] Fix | Delete
buf += newline_indent
[290] Fix | Delete
else:
[291] Fix | Delete
newline_indent = None
[292] Fix | Delete
separator = _item_separator
[293] Fix | Delete
first = True
[294] Fix | Delete
for value in lst:
[295] Fix | Delete
if first:
[296] Fix | Delete
first = False
[297] Fix | Delete
else:
[298] Fix | Delete
buf = separator
[299] Fix | Delete
if isinstance(value, str):
[300] Fix | Delete
yield buf + _encoder(value)
[301] Fix | Delete
elif value is None:
[302] Fix | Delete
yield buf + 'null'
[303] Fix | Delete
elif value is True:
[304] Fix | Delete
yield buf + 'true'
[305] Fix | Delete
elif value is False:
[306] Fix | Delete
yield buf + 'false'
[307] Fix | Delete
elif isinstance(value, int):
[308] Fix | Delete
# Subclasses of int/float may override __str__, but we still
[309] Fix | Delete
# want to encode them as integers/floats in JSON. One example
[310] Fix | Delete
# within the standard library is IntEnum.
[311] Fix | Delete
yield buf + _intstr(value)
[312] Fix | Delete
elif isinstance(value, float):
[313] Fix | Delete
# see comment above for int
[314] Fix | Delete
yield buf + _floatstr(value)
[315] Fix | Delete
else:
[316] Fix | Delete
yield buf
[317] Fix | Delete
if isinstance(value, (list, tuple)):
[318] Fix | Delete
chunks = _iterencode_list(value, _current_indent_level)
[319] Fix | Delete
elif isinstance(value, dict):
[320] Fix | Delete
chunks = _iterencode_dict(value, _current_indent_level)
[321] Fix | Delete
else:
[322] Fix | Delete
chunks = _iterencode(value, _current_indent_level)
[323] Fix | Delete
yield from chunks
[324] Fix | Delete
if newline_indent is not None:
[325] Fix | Delete
_current_indent_level -= 1
[326] Fix | Delete
yield '\n' + _indent * _current_indent_level
[327] Fix | Delete
yield ']'
[328] Fix | Delete
if markers is not None:
[329] Fix | Delete
del markers[markerid]
[330] Fix | Delete
[331] Fix | Delete
def _iterencode_dict(dct, _current_indent_level):
[332] Fix | Delete
if not dct:
[333] Fix | Delete
yield '{}'
[334] Fix | Delete
return
[335] Fix | Delete
if markers is not None:
[336] Fix | Delete
markerid = id(dct)
[337] Fix | Delete
if markerid in markers:
[338] Fix | Delete
raise ValueError("Circular reference detected")
[339] Fix | Delete
markers[markerid] = dct
[340] Fix | Delete
yield '{'
[341] Fix | Delete
if _indent is not None:
[342] Fix | Delete
_current_indent_level += 1
[343] Fix | Delete
newline_indent = '\n' + _indent * _current_indent_level
[344] Fix | Delete
item_separator = _item_separator + newline_indent
[345] Fix | Delete
yield newline_indent
[346] Fix | Delete
else:
[347] Fix | Delete
newline_indent = None
[348] Fix | Delete
item_separator = _item_separator
[349] Fix | Delete
first = True
[350] Fix | Delete
if _sort_keys:
[351] Fix | Delete
items = sorted(dct.items(), key=lambda kv: kv[0])
[352] Fix | Delete
else:
[353] Fix | Delete
items = dct.items()
[354] Fix | Delete
for key, value in items:
[355] Fix | Delete
if isinstance(key, str):
[356] Fix | Delete
pass
[357] Fix | Delete
# JavaScript is weakly typed for these, so it makes sense to
[358] Fix | Delete
# also allow them. Many encoders seem to do something like this.
[359] Fix | Delete
elif isinstance(key, float):
[360] Fix | Delete
# see comment for int/float in _make_iterencode
[361] Fix | Delete
key = _floatstr(key)
[362] Fix | Delete
elif key is True:
[363] Fix | Delete
key = 'true'
[364] Fix | Delete
elif key is False:
[365] Fix | Delete
key = 'false'
[366] Fix | Delete
elif key is None:
[367] Fix | Delete
key = 'null'
[368] Fix | Delete
elif isinstance(key, int):
[369] Fix | Delete
# see comment for int/float in _make_iterencode
[370] Fix | Delete
key = _intstr(key)
[371] Fix | Delete
elif _skipkeys:
[372] Fix | Delete
continue
[373] Fix | Delete
else:
[374] Fix | Delete
raise TypeError("key " + repr(key) + " is not a string")
[375] Fix | Delete
if first:
[376] Fix | Delete
first = False
[377] Fix | Delete
else:
[378] Fix | Delete
yield item_separator
[379] Fix | Delete
yield _encoder(key)
[380] Fix | Delete
yield _key_separator
[381] Fix | Delete
if isinstance(value, str):
[382] Fix | Delete
yield _encoder(value)
[383] Fix | Delete
elif value is None:
[384] Fix | Delete
yield 'null'
[385] Fix | Delete
elif value is True:
[386] Fix | Delete
yield 'true'
[387] Fix | Delete
elif value is False:
[388] Fix | Delete
yield 'false'
[389] Fix | Delete
elif isinstance(value, int):
[390] Fix | Delete
# see comment for int/float in _make_iterencode
[391] Fix | Delete
yield _intstr(value)
[392] Fix | Delete
elif isinstance(value, float):
[393] Fix | Delete
# see comment for int/float in _make_iterencode
[394] Fix | Delete
yield _floatstr(value)
[395] Fix | Delete
else:
[396] Fix | Delete
if isinstance(value, (list, tuple)):
[397] Fix | Delete
chunks = _iterencode_list(value, _current_indent_level)
[398] Fix | Delete
elif isinstance(value, dict):
[399] Fix | Delete
chunks = _iterencode_dict(value, _current_indent_level)
[400] Fix | Delete
else:
[401] Fix | Delete
chunks = _iterencode(value, _current_indent_level)
[402] Fix | Delete
yield from chunks
[403] Fix | Delete
if newline_indent is not None:
[404] Fix | Delete
_current_indent_level -= 1
[405] Fix | Delete
yield '\n' + _indent * _current_indent_level
[406] Fix | Delete
yield '}'
[407] Fix | Delete
if markers is not None:
[408] Fix | Delete
del markers[markerid]
[409] Fix | Delete
[410] Fix | Delete
def _iterencode(o, _current_indent_level):
[411] Fix | Delete
if isinstance(o, str):
[412] Fix | Delete
yield _encoder(o)
[413] Fix | Delete
elif o is None:
[414] Fix | Delete
yield 'null'
[415] Fix | Delete
elif o is True:
[416] Fix | Delete
yield 'true'
[417] Fix | Delete
elif o is False:
[418] Fix | Delete
yield 'false'
[419] Fix | Delete
elif isinstance(o, int):
[420] Fix | Delete
# see comment for int/float in _make_iterencode
[421] Fix | Delete
yield _intstr(o)
[422] Fix | Delete
elif isinstance(o, float):
[423] Fix | Delete
# see comment for int/float in _make_iterencode
[424] Fix | Delete
yield _floatstr(o)
[425] Fix | Delete
elif isinstance(o, (list, tuple)):
[426] Fix | Delete
yield from _iterencode_list(o, _current_indent_level)
[427] Fix | Delete
elif isinstance(o, dict):
[428] Fix | Delete
yield from _iterencode_dict(o, _current_indent_level)
[429] Fix | Delete
else:
[430] Fix | Delete
if markers is not None:
[431] Fix | Delete
markerid = id(o)
[432] Fix | Delete
if markerid in markers:
[433] Fix | Delete
raise ValueError("Circular reference detected")
[434] Fix | Delete
markers[markerid] = o
[435] Fix | Delete
o = _default(o)
[436] Fix | Delete
yield from _iterencode(o, _current_indent_level)
[437] Fix | Delete
if markers is not None:
[438] Fix | Delete
del markers[markerid]
[439] Fix | Delete
return _iterencode
[440] Fix | Delete
[441] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function