U
    O5[8                  	   @   s   d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
mZ dd Ze Zd	gZejejB ejB Zd
d Ze \ZZZeeedZedeZdddddddddZdZddeejdjeejfddZepeZ edeZ!dZ"de!je"fddZ#e!je"fd d!Z$G d"d	 d	e%Z&dS )#zImplementation of JSONDecoder
    )absolute_importN   )PY3unichr)make_scannerJSONDecodeErrorc                  C   s.   zddl m}  | W S  tk
r(   Y d S X d S )Nr   
scanstring)Z	_speedupsr	   ImportErrorr    r   4/usr/lib/python3/dist-packages/simplejson/decoder.py_import_c_scanstring
   s
    r   JSONDecoderc                  C   sB   t jdk r&dd} td| \}}ntd}td}||| fS )N)      Z 7FF80000000000007FF0000000000000hexz>ddnaninf)sysversion_infodecodestructZunpackfloat)Z_BYTESr   r   r   r   r   _floatconstants   s    

r   )z	-InfinityZInfinityNaNz(.*?)(["\\\x00-\x1f])"\/
	)r   r   r   bfnrtzutf-8T c	                 C   s  |dkrt }g }	|	j}
|d }|| |}|dkr<td| || }| \}}|rt|slt|tslt||}|
| |dkrqn(|dkr|rd}t|| |n
|
| qz| | }W n  tk
r   td| |Y nX |dkr"z|| }W n& tk
r   d}t|| |Y nX |d7 }nld	}| |d |d
  }|dd }t	|dksh|dksh|dkrxt|| |d zt
|d}W n& tk
r   t|| |d Y nX |d
7 }|dkr|d@ dkr| ||d  dkr| |d |d  }|dd }t	|dkr|dks|dkszt
|d}W n" tk
rV   t|| |Y nX |d@ dkrd|d d> |d B  }|d7 }t|}|
| q||	|fS )a  Scan the string s for a JSON string. End is the index of the
    character in s after the quote that started the JSON string.
    Unescapes all valid JSON string escape sequences and raises ValueError
    on attempt to decode an invalid string. If strict is False then literal
    control characters are allowed in the string.

    Returns a tuple of the decoded string and the index of the character in s
    after the end quote.Nr   zUnterminated string starting atr   r   zInvalid control character %r atuzInvalid \X escape sequence %rzInvalid \uXXXX escape sequence   r      xX   i  i   i   z\ur   i   i   
   )DEFAULT_ENCODINGappendr   endgroups
isinstanceZunicode
IndexErrorKeyErrorlenint
ValueErrorr   )sr2   encodingstrictZ_b_mZ_join_PY3Z_maxunicodeZchunks_appendZbeginchunkZcontent
terminatormsgZesccharZescXZuniZesc2Zuni2r   r   r   py_scanstring1   s    
  
  

"

"
rD   z
[ \t\n\r]*z 	
c	                 C   s  | \}	}
|d kri }|j }g }|	|
|
d  }|dkr||kr\||	|
 }
|	|
|
d  }|dkr|d k	r||}||
d fS i }|d k	r||}||
d fS |dkrtd|	|
|
d7 }
t|	|
||\}}
|||}|	|
|
d  dkr||	|
 }
|	|
|
d  dkrtd|	|
|
d7 }
z:|	|
 |kr^|
d7 }
|	|
 |kr^||	|
d  }
W n tk
rv   Y nX ||	|
\}}
|||f z0|	|
 }||kr||	|
d  }
|	|
 }W n tk
r   d}Y nX |
d7 }
|dkrqn|dkrtd	|	|
d zJ|	|
 }||krZ|
d7 }
|	|
 }||krZ||	|
d  }
|	|
 }W n tk
rv   d}Y nX |
d7 }
|dkrtd|	|
d q|d k	r||}||
fS t|}|d k	r||}||
fS )
Nr   r   }z1Expecting property name enclosed in double quotes:zExpecting ':' delimiterr(   ,zExpecting ',' delimiter or '}')
setdefaultr2   r   r	   r5   r1   dict)stater;   r<   	scan_onceobject_hookobject_pairs_hookmemo_w_wsr:   r2   Zmemo_getZpairsnextcharresultkeyvaluer   r   r   
JSONObject   s     







 

rU   c           
      C   sN  | \}}g }|||d  }||krF|||d   }|||d  }|dkrZ||d fS |dkrntd|||j}|||\}	}||	 |||d  }||kr|||d   }|||d  }|d7 }|dkrڐqFn|dkrtd||d z:|| |kr*|d7 }|| |kr*|||d   }W qt tk
rB   Y qtX qt||fS )Nr   ]r(   zExpecting value or ']'rG   zExpecting ',' delimiter or ']')r2   r   r1   r5   )
rJ   rK   rO   rP   r:   r2   valuesrQ   r?   rT   r   r   r   	JSONArray   s<    rX   c                   @   s<   e Zd ZdZdddZejefddZdejefd	d
Z	dS )r   a  Simple JSON <http://json.org> decoder

    Performs the following translations in decoding by default:

    +---------------+-------------------+
    | JSON          | Python            |
    +===============+===================+
    | object        | dict              |
    +---------------+-------------------+
    | array         | list              |
    +---------------+-------------------+
    | string        | str, unicode      |
    +---------------+-------------------+
    | number (int)  | int, long         |
    +---------------+-------------------+
    | number (real) | float             |
    +---------------+-------------------+
    | true          | True              |
    +---------------+-------------------+
    | false         | False             |
    +---------------+-------------------+
    | null          | None              |
    +---------------+-------------------+

    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as
    their corresponding ``float`` values, which is outside the JSON spec.

    NTc                 C   sj   |dkrt }|| _|| _|| _|p$t| _|p.t| _|p:tj	| _
|| _t| _t| _t| _i | _t| | _dS )a	  
        *encoding* determines the encoding used to interpret any
        :class:`str` objects decoded by this instance (``'utf-8'`` by
        default).  It has no effect when decoding :class:`unicode` objects.

        Note that currently only encodings that are a superset of ASCII work,
        strings of other encodings should be passed in as :class:`unicode`.

        *object_hook*, if specified, will be called with the result of every
        JSON object decoded and its return value will be used in place of the
        given :class:`dict`.  This can be used to provide custom
        deserializations (e.g. to support JSON-RPC class hinting).

        *object_pairs_hook* is an optional function that will be called with
        the result of any object literal decode with an ordered list of pairs.
        The return value of *object_pairs_hook* will be used instead of the
        :class:`dict`.  This feature can be used to implement custom decoders
        that rely on the order that the key and value pairs are decoded (for
        example, :func:`collections.OrderedDict` will remember the order of
        insertion). If *object_hook* is also defined, the *object_pairs_hook*
        takes priority.

        *parse_float*, if specified, will be called with the string of every
        JSON float to be decoded.  By default, this is equivalent to
        ``float(num_str)``. This can be used to use another datatype or parser
        for JSON floats (e.g. :class:`decimal.Decimal`).

        *parse_int*, if specified, will be called with the string of every
        JSON int to be decoded.  By default, this is equivalent to
        ``int(num_str)``.  This can be used to use another datatype or parser
        for JSON integers (e.g. :class:`float`).

        *parse_constant*, if specified, will be called with one of the
        following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.  This
        can be used to raise an exception if invalid JSON numbers are
        encountered.

        *strict* controls the parser's behavior when it encounters an
        invalid control character in a string. The default setting of
        ``True`` means that unescaped control characters are parse errors, if
        ``False`` then control characters will be allowed in strings.

        N)r0   r;   rL   rM   r   parse_floatr8   	parse_int
_CONSTANTS__getitem__parse_constantr<   rU   Zparse_objectrX   Zparse_arrayr	   Zparse_stringrN   r   rK   )selfr;   rL   rY   rZ   r]   r<   rM   r   r   r   __init__.  s    .

zJSONDecoder.__init__c                 C   sX   |rt |trt|| j}| |\}}||| }|t|krTtd||t||S )zzReturn the Python representation of ``s`` (a ``str`` or ``unicode``
        instance containing a JSON document)

        z
Extra data)r4   bytesstrr;   
raw_decoder2   r7   r   )r^   r:   rO   r>   objr2   r   r   r   r   k  s    zJSONDecoder.decoder   c                 C   s   |dk rt d|||r*t|ts*tdt||krxt|| }|dkrT|d7 }n$|dkrx|||d  dkrx|d7 }| j|||| d	S )
a  Decode a JSON document from ``s`` (a ``str`` or ``unicode``
        beginning with a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.
        Optionally, ``idx`` can be used to specify an offset in ``s`` where
        the JSON document begins.

        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.

        r   zExpecting valuez$Input string must be text, not bytesi  r         u   ï»¿)idx)r   r4   ra   	TypeErrorr7   ordrK   r2   )r^   r:   rf   rO   r>   Zord0r   r   r   rb   x  s    
zJSONDecoder.raw_decode)NNNNNTN)
__name__
__module____qualname____doc__r_   
WHITESPACEmatchr   r   rb   r   r   r   r   r     s          
=)'rl   Z
__future__r   rer   r   compatr   r   scannerr   r   r   Zc_scanstring__all__VERBOSE	MULTILINEDOTALLFLAGSr   r   ZPosInfZNegInfr[   compileZSTRINGCHUNKZ	BACKSLASHr0   rn   join
maxunicoderD   r	   rm   ZWHITESPACE_STRrU   rX   objectr   r   r   r   r   <module>   sV   	         
X 
^$