U e5d> @s>dZddlZzddlmZWnek r4dZYnXzddlmZWnek r^dZYnXzddlmZ Wnek rdZ YnXe dZ e dZ e dZ d d d d d dddZedD]ZeeedeqedZddZepeZddZepeZGdddeZeeeeeeee e!ej"f ddZ#dS)zImplementation of JSONEncoder N)encode_basestring_ascii)encode_basestring) make_encoderz[\x00-\x1f\\"\b\f\n\r\t]z([\\"]|[^\ -~])s[-]z\\z\"z\bz\fz\nz\rz\t)\"    \u{0:04x}infcCsdd}dt||dS)z5Return a JSON representation of a Python string cSst|dS)Nr) ESCAPE_DCTgroup)matchr$/usr/lib64/python3.8/json/encoder.pyreplace(sz%py_encode_basestring..replacer)ESCAPEsubsrrrrpy_encode_basestring$srcCsdd}dt||dS)zAReturn an ASCII-only JSON representation of a Python string cSs|d}z t|WStk rzt|}|dkrBd|YS|d8}d|d?d@B}d|d@B}d||YSYnXdS) Nrir i iiz\u{0:04x}\u{1:04x})rrKeyErrorordformat)rrns1s2rrrr4s   z+py_encode_basestring_ascii..replacer) ESCAPE_ASCIIrrrrrpy_encode_basestring_ascii0sr"c @sNeZdZdZdZdZddddddddddd Zd d Zd d ZdddZ dS) JSONEncoderaZExtensible JSON encoder for Python data structures. Supports the following objects and types by default: +-------------------+---------------+ | Python | JSON | +===================+===============+ | dict | object | +-------------------+---------------+ | list, tuple | array | +-------------------+---------------+ | str | string | +-------------------+---------------+ | int, float | number | +-------------------+---------------+ | True | true | +-------------------+---------------+ | False | false | +-------------------+---------------+ | None | null | +-------------------+---------------+ To extend this to recognize other objects, subclass and implement a ``.default()`` method with another method that returns a serializable object for ``o`` if possible, otherwise it should call the superclass implementation (to raise ``TypeError``). z, z: FTN)skipkeys ensure_asciicheck_circular allow_nan sort_keysindent separatorsdefaultc CsZ||_||_||_||_||_||_|dk r:|\|_|_n|dk rHd|_|dk rV||_dS)aConstructor for JSONEncoder, with sensible defaults. If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped. If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters. If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place. If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats. If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis. If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation. If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if *indent* is ``None`` and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace. If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a ``TypeError``. N,) r$r%r&r'r(r)item_separator key_separatorr+) selfr$r%r&r'r(r)r*r+rrr__init__hs+zJSONEncoder.__init__cCstd|jjddS)alImplement this method in a subclass such that it returns a serializable object for ``o``, or calls the base implementation (to raise a ``TypeError``). For example, to support arbitrary iterators, you could implement default like this:: def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o) zObject of type z is not JSON serializableN) TypeError __class____name__)r/orrrr+szJSONEncoder.defaultcCsNt|tr |jrt|St|S|j|dd}t|ttfsDt|}d|S)zReturn a JSON string representation of a Python data structure. >>> from json.encoder import JSONEncoder >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) '{"foo": ["bar", "baz"]}' T) _one_shot) isinstancestrr%rr iterencodelisttuplejoin)r/r4chunksrrrencodes zJSONEncoder.encodec Cs|jr i}nd}|jrt}nt}|jtjtt fdd}|rvtdk rv|j dkrvt||j ||j |j |j |j |j|j }n&t||j ||j ||j |j |j |j| }||dS)zEncode the given object and yield each string representation as available. For example:: for chunk in JSONEncoder().iterencode(bigobject): mysocket.write(chunk) NcSsJ||krd}n$||krd}n||kr*d}n||S|sFtdt||S)NZNaNZInfinityz -Infinityz2Out of range float values are not JSON compliant: ) ValueErrorrepr)r4r'Z_reprZ_infZ_neginftextrrrfloatstrsz(JSONEncoder.iterencode..floatstrr)r&r%rrr'float__repr__INFINITYc_make_encoderr)r+r.r-r(r$_make_iterencode)r/r4r5markers_encoderrB _iterencoderrrr9sL   zJSONEncoder.iterencode)F) r3 __module__ __qualname____doc__r-r.r0r+r>r9rrrrr#Is 8r#csdk rsd fdd  fdd fddS)N c 3s|sdVdSdk r6 |}|kr.d||<d}dk rh|d7}d|}|}||7}nd}}d}|D]}|rd}n|} |r||Vqx|dkr|dVqx|dkr|d Vqx|dkr|d Vqx | r||Vqx | r||Vqx|V |fr8||}n" | rP||}n ||}|EdHqx|dk r|d8}d|Vd Vdk r|=dS) Nz[]Circular reference detected[r TFnulltruefalse]r) Zlst_current_indent_levelmarkeridZbufnewline_indentZ separatorfirstvaluer=)r?rI _floatstr_indent_intstr_item_separatorrJ_iterencode_dict_iterencode_listdictrCidintr7r:rHr8r;rrr`s\               z*_make_iterencode.._iterencode_listc 3s:|sdVdSdk r6|}|kr.d||<dVdk rh|d7}d|}|}|Vnd}}d} rt|}n|}|D]j\}}|rnn| r|}nZ|dkrd}nL|dkrd }n>|dkrd }n0|r|}n rqntd |jj|r"d}n|V|V V|rP|Vq|dkrbd Vq|dkrtdVq|dkrd Vq|r|Vq| r|Vq|fr҈||} n"| r||} n ||} | EdHq|dk r |d8}d|Vd Vdk r6|=dS) Nz{}rO{rQr TrSFrTrRz0keys must be str, int, float, bool or None, not })sorteditemsr1r2r3) ZdctrVrWrXr-rYrgkeyrZr=)r?rIr[r\r]r^rJr_r`_key_separator _skipkeys _sort_keysrarCrbrcr7r:rHr8r;rrr_Ms                       z*_make_iterencode.._iterencode_dictc3s |r|Vn|dkr&dVn|dkr6dVn|dkrFdVn | r\|Vn | rr|Vn | fr||EdHnj |r||EdHnNdk rֈ |}|krΈd||<|}||EdHdk r|=dS)NrRTrSFrTrOr)r4rVrW)r?_defaultrIr[r]rJr_r`rarCrbrcr7r:rHr8r;rrrJs2       z%_make_iterencode.._iterencoder)rHrlrIr\r[rir^rkrjr5r?rarCrbrcr7r:r8r;r]r)r?rlrIr[r\r]r^rJr_r`rirjrkrarCrbrcr7r:rHr8r;rrGs .84P,rG)$rMreZ_jsonrZc_encode_basestring_ascii ImportErrorrZc_encode_basestringrrFcompilerr!ZHAS_UTF8rrangei setdefaultchrrrCrErr"objectr#r?rarbrcr7r:r8r;rDrGrrrrsZ        >