ó
jš¤Mc           @   si  d  Z  d d l Z d d l m Z d „  Z e ƒ  \ Z Z d d l m Z e j	 d ƒ Z
 e j	 d ƒ Z e j	 d ƒ Z i d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6Z x1 e d ƒ D]# Z e j e e ƒ d e f ƒ q» We Z d „  Z d „  Z e pe Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e e e e e e e e  e! e" e# e$ e% e& d „ Z' d S(    s   Implementation of JSONEncoder
iÿÿÿÿN(   t   Decimalc          C   s=   y$ d d l  m }  |  j |  j f SWn t k
 r8 d SXd  S(   Niÿÿÿÿ(   t	   _speedups(   NN(   t
   simplejsonR   t   encode_basestring_asciit   make_encodert   ImportErrort   None(   R   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   _import_speedups   s
    (   t   PosInfs   [\x00-\x1f\\"\b\f\n\r\t]s   ([\\"]|[^\ -~])s   [\x80-\xff]s   \\s   \s   \"t   "s   \bs   s   \fs   s   \ns   
s   \rs   s   \ts   	i    s   \u%04xc         C   sW   t  |  t ƒ r6 t j |  ƒ d k	 r6 |  j d ƒ }  n  d „  } d t j | |  ƒ d S(   s5   Return a JSON representation of a Python string

    s   utf-8c         S   s   t  |  j d ƒ S(   Ni    (   t
   ESCAPE_DCTt   group(   t   match(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   replace(   s    u   "N(   t
   isinstancet   strt   HAS_UTF8t   searchR   t   decodet   ESCAPEt   sub(   t   sR   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   encode_basestring"   s    $	c         C   s]   t  |  t ƒ r6 t j |  ƒ d k	 r6 |  j d ƒ }  n  d „  } d t t j | |  ƒ ƒ d S(   sA   Return an ASCII-only JSON representation of a Python string

    s   utf-8c         S   sŽ   |  j  d ƒ } y t | SWnl t k
 r‰ t | ƒ } | d k  rN d | f S| d 8} d | d ?d @B} d | d @B} d | | f Sn Xd  S(	   Ni    i   s   \u%04xi Ø  i
   iÿ  i Ü  s   \u%04x\u%04x(   R   R
   t   KeyErrort   ord(   R   R   t   nt   s1t   s2(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR   3   s    
R	   N(   R   R   R   R   R   R   t   ESCAPE_ASCIIR   (   R   R   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   py_encode_basestring_ascii-   s    $	t   JSONEncoderc           B   s_   e  Z d  Z d Z d Z e e e e e d d d d e d „
 Z d „  Z	 d „  Z
 e d „ Z RS(	   sZ  Extensible JSON <http://json.org> encoder for Python data structures.

    Supports the following objects and types by default:

    +-------------------+---------------+
    | Python            | JSON          |
    +===================+===============+
    | dict              | object        |
    +-------------------+---------------+
    | list, tuple       | array         |
    +-------------------+---------------+
    | str, unicode      | string        |
    +-------------------+---------------+
    | int, long, 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``).

    s   , s   : s   utf-8c         C   s¿   | |  _  | |  _ | |  _ | |  _ | |  _ |
 |  _ t | t t f ƒ rX d | } n  | |  _	 | d k	 r‚ | \ |  _ |  _ n | d k	 rš d |  _ n  |	 d k	 r² |	 |  _ n  | |  _ d S(   sÁ	  Constructor for JSONEncoder, with sensible defaults.

        If skipkeys is false, then it is a TypeError to attempt
        encoding of keys that are not str, int, long, 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 unicode characters escaped.  If
        ensure_ascii is false, the output will be unicode object.

        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 string, then JSON array elements and object members
        will be pretty-printed with a newline followed by that string repeated
        for each level of nesting. ``None`` (the default) selects the most compact
        representation without any newlines. For backwards compatibility with
        versions of simplejson earlier than 2.1.0, an integer is also accepted
        and is converted to a string with that many spaces.

        If specified, separators should be a (item_separator, key_separator)
        tuple.  The default is (', ', ': ').  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``.

        If encoding is not None, then all input strings will be
        transformed into unicode using that encoding prior to JSON-encoding.
        The default is UTF-8.

        If use_decimal is true (not the default), ``decimal.Decimal`` will
        be supported directly by the encoder. For the inverse, decode JSON
        with ``parse_float=decimal.Decimal``.

        t    t   ,N(   t   skipkeyst   ensure_asciit   check_circulart	   allow_nant	   sort_keyst   use_decimalR   t   intt   longt   indentR   t   item_separatort   key_separatort   defaultt   encoding(   t   selfR!   R"   R#   R$   R%   R)   t
   separatorsR-   R,   R&   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   __init__h   s     5							c         C   s   t  t | ƒ d ƒ ‚ d S(   s$  Implement 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)
                return JSONEncoder.default(self, o)

        s    is not JSON serializableN(   t	   TypeErrort   repr(   R.   t   o(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR,   ®   s    c         C   sÕ   t  | t ƒ ru t  | t ƒ rU |  j } | d k	 rU | d k rU | j | ƒ } qU n  |  j rh t | ƒ St | ƒ Sn  |  j	 | d t
 ƒ} t  | t t f ƒ s® t | ƒ } n  |  j rÄ d j | ƒ Sd j | ƒ Sd S(   sÕ   Return a JSON string representation of a Python data structure.

        >>> from simplejson import JSONEncoder
        >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
        '{"foo": ["bar", "baz"]}'

        s   utf-8t	   _one_shott    u    N(   R   t
   basestringR   R-   R   R   R"   R   R   t
   iterencodet   Truet   listt   tuplet   join(   R.   R3   t	   _encodingt   chunks(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   encodeÂ   s    			
	c         C   s;  |  j  r i  } n d } |  j r* t } n t } |  j d k rT | |  j d „ } n  |  j t t t d „ } i  } | rÙ t	 d k	 rÙ |  j
 d k rÙ t	 | |  j | |  j
 |  j |  j |  j |  j |  j | |  j ƒ } n? t | |  j | |  j
 | |  j |  j |  j |  j | |  j ƒ } z | | d ƒ SWd | j ƒ  Xd S(   sØ   Encode the given object and yield each string
        representation as available.

        For example::

            for chunk in JSONEncoder().iterencode(bigobject):
                mysocket.write(chunk)

        s   utf-8c         S   s+   t  |  t ƒ r! |  j | ƒ }  n  | |  ƒ S(   N(   R   R   R   (   R3   t   _orig_encoderR<   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   _encoderó   s    c         S   sl   |  |  k r d } n4 |  | k r* d } n |  | k r? d } n
 | |  ƒ S| sh t  d t |  ƒ ƒ ‚ n  | S(   Nt   NaNt   Infinitys	   -Infinitys2   Out of range float values are not JSON compliant: (   t
   ValueErrorR2   (   R3   R$   t   _reprt   _inft   _neginft   text(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   floatstrø   s    			
i    N(   R#   R   R"   R   R   R-   R$   t
   FLOAT_REPRR   t   c_make_encoderR)   R,   R+   R*   R%   R!   R&   t   _make_iterencodet   clear(   R.   R3   R4   t   markersR@   RH   t   key_memot   _iterencode(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR7   à   s0    
				N(   t   __name__t
   __module__t   __doc__R*   R+   t   FalseR8   R   R0   R,   R>   R7   (    (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR   I   s   	C		t   JSONEncoderForHTMLc           B   s#   e  Z d  Z d „  Z e d „ Z RS(   s"  An encoder that produces JSON safe to embed in HTML.

    To embed JSON content in, say, a script tag on a web page, the
    characters &, < and > should be escaped. They cannot be escaped
    with the usual entities (e.g. &amp;) because they are not expanded
    within <script> tags.
    c         C   s9   |  j  | t ƒ } |  j r( d j | ƒ Sd j | ƒ Sd  S(   NR5   u    (   R7   R8   R"   R;   (   R.   R3   R=   (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR>   *  s    	c         c   sk   t  t |  ƒ j | | ƒ } xI | D]A } | j d d ƒ } | j d d ƒ } | j d d ƒ } | Vq" Wd  S(   Nt   &s   \u0026t   <s   \u003ct   >s   \u003e(   t   superRT   R7   R   (   R.   R3   R4   R=   t   chunk(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyR7   3  s    (   RP   RQ   RR   R>   RS   R7   (    (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyRT   !  s   		c            sý   ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡	 ‡
 ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  ‰ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡  ‡	 ‡
 ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡	 ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d †  ‰ ˆ S(   Nc   
      3   sW  |  s d Vd  Sˆ d  k	 rO ˆ |  ƒ } | ˆ k rB ˆ d ƒ ‚ n  |  ˆ | <n  d } ˆ d  k	 r | d 7} d ˆ | } ˆ	 | } | | 7} n d  } ˆ	 } ˆ } xm|  D]e} | r¾ ˆ } n | } ˆ | ˆ ƒ rå | ˆ  | ƒ Vq© | d  k rý | d Vq© | ˆ k r| d Vq© | ˆ k r-| d Vq© ˆ | ˆ ˆ
 f ƒ rT| ˆ | ƒ Vq© ˆ | ˆ ƒ ru| ˆ | ƒ Vq© ˆ rœˆ | ˆ ƒ rœ| ˆ | ƒ Vq© | Vˆ | ˆ ˆ f ƒ rÈˆ | | ƒ } n0 ˆ | ˆ ƒ réˆ | | ƒ } n ˆ | | ƒ } x | D] }	 |	 VqÿWq© W| d  k	 r8| d 8} d ˆ | Vn  d	 Vˆ d  k	 rSˆ | =n  d  S(
   Ns   []s   Circular reference detectedt   [i   s   
t   nullt   truet   falset   ](   R   (
   t   lstt   _current_indent_levelt   markeridt   buft   newline_indentt	   separatort   firstt   valueR=   RY   (   R@   R'   t   floatt	   _floatstrRM   R   t   idt   _iterencode_listR6   t   _item_separatorR(   t   dictt   _iterencode_dictR8   RC   t   _use_decimalR:   R    t   _indentRS   R9   R   RO   (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyRj   P  sb    

	
c         3   s2  |  s d Vd  Sˆ d  k	 rO ˆ |  ƒ } | ˆ k rB ˆ d ƒ ‚ n  |  ˆ | <n  d Vˆ  d  k	 rŠ | d 7} d ˆ  | } ˆ | } | Vn d  } ˆ } ˆ } ˆ rÄ |  j ƒ  } | j d d „  ƒ n |  j ƒ  } x| D]\ } } ˆ | ˆ
 ƒ rõ n§ ˆ | ˆ ƒ rˆ | ƒ } n‰ | ˆ k r(d } nt | ˆ k r=d	 } n_ | d  k rRd
 } nJ ˆ | ˆ ˆ f ƒ rvˆ | ƒ } n& ˆ r‚q× n t d t | ƒ d ƒ ‚ | r«ˆ } n | Vˆ	 | ƒ Vˆ Vˆ | ˆ
 ƒ rÝˆ	 | ƒ Vq× | d  k rñd
 Vq× | ˆ k rd Vq× | ˆ k rd	 Vq× ˆ | ˆ ˆ f ƒ r<ˆ | ƒ Vq× ˆ | ˆ ƒ rYˆ | ƒ Vq× ˆ r|ˆ | ˆ ƒ r|ˆ | ƒ Vq× ˆ | ˆ ˆ f ƒ r£ˆ | | ƒ }	 n0 ˆ | ˆ ƒ rÄˆ | | ƒ }	 n ˆ | | ƒ }	 x |	 D] }
 |
 VqÚWq× W| d  k	 r| d 8} d ˆ  | Vn  d Vˆ d  k	 r.ˆ | =n  d  S(   Ns   {}s   Circular reference detectedt   {i   s   
t   keyc         S   s   |  d S(   Ni    (    (   t   kv(    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   <lambda>œ  s    R\   R]   R[   s   key s    is not a stringt   }(   R   t   itemst   sortt	   iteritemsR1   R2   (   t   dctR`   Ra   Rc   R*   Re   Ru   Rq   Rf   R=   RY   (   Ro   t   _key_separatorR'   Rg   Rh   R(   R   Ri   Rj   R@   R6   Rk   RM   t
   _sort_keysRl   Rm   t	   _skipkeysR8   RC   Rn   R:   R    RS   R9   R   RO   (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyRm   ‡  sŠ    

				
c         3   s©  ˆ |  ˆ ƒ r ˆ  |  ƒ Vnˆ|  d  k r1 d Vnt|  ˆ k rE d Vn`|  ˆ k rY d VnLˆ |  ˆ ˆ	 f ƒ r| ˆ |  ƒ Vn)ˆ |  ˆ ƒ r™ ˆ |  ƒ Vnˆ |  ˆ ˆ f ƒ rÐ xô ˆ |  | ƒ D] } | Vq¾ WnÕ ˆ |  ˆ
 ƒ rxÃ ˆ |  | ƒ D] } | Vqï Wn¤ ˆ r$ˆ |  ˆ ƒ r$ˆ |  ƒ Vn ˆ d  k	 rdˆ |  ƒ } | ˆ k rWˆ d ƒ ‚ n  |  ˆ | <n  ˆ |  ƒ }  x ˆ |  | ƒ D] } | Vq€Wˆ d  k	 r¥ˆ | =n  d  S(   NR[   R\   R]   s   Circular reference detected(   R   (   R3   R`   RY   Ra   (   R@   R'   Rg   Rh   RM   R   Ri   Rj   R6   R(   Rl   Rm   t   _defaultR8   RC   Rn   R:   R    RS   R9   R   RO   (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyRO   Ö  s<    	(    (   RM   R|   R@   Ro   Rh   Ry   Rk   Rz   R{   R4   Rn   RS   R8   RC   R6   R    Rl   Rg   Ri   R'   R   R9   R(   R   R:   (    (   R@   Ry   R'   Rg   Rh   R(   R   Ri   Rj   R6   Rk   RM   Rz   Rl   Rm   R|   R{   R8   RC   Rn   R:   R    Ro   RS   R9   R   RO   s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyRK   <  s    Q7ZON!((   RR   t   ret   decimalR    R   t   c_encode_basestring_asciiRJ   t   simplejson.decoderR   t   compileR   R   R   R
   t   ranget   it
   setdefaultt   chrR2   RI   R   R   R   t   objectR   RT   RS   R8   RC   R6   Rl   Rg   Ri   R'   R   R9   R(   R   R:   RK   (    (    (    s6   /usr/lib/python2.7/dist-packages/simplejson/encoder.pyt   <module>   sL   	
!		Ø