Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/simplejson/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/simplejson/encoder.pyc


³Nc@swdZddlZddlmZdZe\ZZddlmZej	dZ
ej	dZej	dZi	d	d
6dd6d
d6dd6dd6dd6dd6dd6dd6Z
x1edD]#Ze
jeedefqWeZdZdZepeZdefd YZd!efd"YZeeeeeeee e!e"e#e$e%e&d#Z'dS($sImplementation of JSONEncoder
iN(tDecimalcCs=y$ddlm}|j|jfSWntk
r8dSXdS(Ni(t	_speedups(NN(t
simplejsonRtencode_basestring_asciitmake_encodertImportErrortNone(R((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyt_import_speedupss

(tPosInfu[\x00-\x1f\\"\b\f\n\r\t

]s([\\"]|[^\ -~])s[\x80-\xff]s\\s\s\"t"s\bss\fss\ns
s\rs
s\ts	s\u2028u
s\u2029u
i s\u%04xcCsWt|tr6tj|dk	r6|jd}nd}dtj||dS(s5Return a JSON representation of a Python string

    sutf-8cSst|jdS(Ni(t
ESCAPE_DCTtgroup(tmatch((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pytreplace*su"N(t
isinstancetstrtHAS_UTF8tsearchRtdecodetESCAPEtsub(tsR
((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pytencode_basestring$s$	cCs]t|tr6tj|dk	r6|jd}nd}dttj||dS(sAReturn an ASCII-only JSON representation of a Python string

    sutf-8cSs|jd}yt|SWnltk
rt|}|dkrNd|fS|d8}d|d?d@B}d|d@B}d||fSnXdS(	Niis\u%04xii
iis\u%04x\u%04x(RR
tKeyErrortord(RRtnts1ts2((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyR
5s

R	N(RRRRRRtESCAPE_ASCIIR(RR
((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pytpy_encode_basestring_ascii/s$	tJSONEncoderc
BseeZdZdZdZeeeeeddddeeedZdZ	dZ
edZRS(	sZExtensible JSON <http://json.org> encoder for Python data structures.

    Supports the following objects and types by default:

    +-------------------+---------------+
    | Python            | JSON          |
    +===================+===============+
    | dict, namedtuple  | 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: sutf-8c

Cs||_||_||_||_||_|
|_||_||_t|t	t
frjd|}n||_|dk	r|\|_
|_n|dk	rd|_
n|	dk	r|	|_n||_dS(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``.

        If namedtuple_as_object is true (the default), objects with
        ``_asdict()`` methods will be encoded as JSON objects.

        If tuple_as_array is true (the default), tuple (and subclasses) will
        be encoded as JSON arrays.
        t t,N(tskipkeystensure_asciitcheck_circulart	allow_nant	sort_keystuse_decimaltnamedtuple_as_objectttuple_as_arrayRtinttlongtindentRtitem_separatort
key_separatortdefaulttencoding(
tselfR!R"R#R$R%R+t
separatorsR/R.R&R'R(((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyt__init__js$;								
	cCstt|ddS(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	TypeErrortrepr(R0to((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyR.scCst|trut|trU|j}|dk	rU|dkrU|j|}qUn|jrht|St|Sn|j	|dt
}t|ttfst|}n|jrdj
|Sdj
|SdS(sReturn a JSON string representation of a Python data structure.

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

        sutf-8t	_one_shottuN(Rt
basestringRR/RRR"RRt
iterencodetTruetlistttupletjoin(R0R5t	_encodingtchunks((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pytencodes		
	

	
cCsS|jri}nd}|jr*t}nt}|jdkrT||jd}n|jtttd}i}|rt	dk	r|j
dkrt	||j||j
|j|j
|j|j|j||j|j|j
}nKt||j||j
||j|j
|j|j||j|j|j
}z||dSWd|jXdS(sEncode the given object and yield each string
        representation as available.

        For example::

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

        sutf-8cSs+t|tr!|j|}n||S(N(RRR(R5t
_orig_encoderR>((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyt_encoderscSsl||krd}n4||kr*d}n||kr?d}n
||S|shtdt|n|S(NtNaNtInfinitys	-Infinitys2Out of range float values are not JSON compliant: (t
ValueErrorR4(R5R$t_reprt_inft_neginfttext((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pytfloatstrs			
iN(R#RR"RRR/R$t
FLOAT_REPRRtc_make_encoderR+R.R-R,R%R!R&R'R(t_make_iterencodetclear(R0R5R6tmarkersRBRJtkey_memot_iterencode((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyR9s4
				N(t__name__t
__module__t__doc__R,R-tFalseR:RR2R.R@R9(((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRKs	J		tJSONEncoderForHTMLcBs#eZdZdZedZRS(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.
    cCs9|j|t}|jr(dj|Sdj|SdS(NR7u(R9R:R"R=(R0R5R?((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyR@6s	
ccsktt|j||}xI|D]A}|jdd}|jdd}|jdd}|Vq"WdS(Nt&s\u0026t<s\u003ct>s\u003e(tsuperRVR9R
(R0R5R6R?tchunk((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyR9?s
(RRRSRTR@RUR9(((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRV-s		cs	
fd		

fd	
fdS(Nc3s|sdVdSdk	rO|}|krBdn||<nd}dk	r|d7}d|}
|}||7}nd}
}}x|D]}|r}n|}|	r||Vq|dkr|dVq|kr|dVq|kr-|dVq|frT||Vq|ru||Vqr|r||Vq|V|r||}not|d	d}	|	rt|	r
|	|}nWr(|r(||}n0|rI
||}n||}x|D]}
|
Vq_WqW|dk	r|d8}d|Vnd
Vdk	r|=ndS(Ns[]sCircular reference detectedt[is
tnullttruetfalset_asdictt](Rtgetattrtcallable(tlstt_current_indent_leveltmarkeridtbuftnewline_indentt	separatortfirsttvalueR?R`R[(t_namedtuple_as_objectRBR)tfloatt	_floatstrRORtidt_iterencode_listR8t_item_separatorR*tdictt_iterencode_dictR:REt_use_decimalR<Rt_indentRUt_tuple_as_arrayR;RRQ(s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRp\sn




		


c3s|sdVdS
dk	rO|}|
krBdn|
|<ndVdk	r|d7}d|}|}|Vnd}}}r|j}|jddn|j}xz|D]r\}}|rn|r|}n|kr(d}nt|kr=d	}n_|dkrRd
}nJ|frv|}n&rqntdt|d|r}n|V
|VV|r
|Vq|dkrd
Vq|krdVq|krd	Vq|fr<|Vq|rY|Vqr||r||Vq|r	||}	not|d
d}
|
rt|
r|
|}	nWr|r	||}	n0|r$||}	n||}	x|	D]}|Vq:WqW|dk	rs|d8}d|VndV
dk	r
|=ndS(Ns{}sCircular reference detectedt{is
tkeycSs|dS(Ni((tkv((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyt<lambda>sR^R_R]skey s is not a stringR`t}(Rtitemstsortt	iteritemsR3R4RbRc(tdctReRfRhR,RjR|RxRkR?R`R[(RlRut_key_separatorR)RmRnR*RRoRpRBR8RqROt
_sort_keysRrRst	_skipkeysR:RERtR<RRURvR;RRQ(s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRss


					


c3s)|	r|Vn|dkr1dVn|krEdVn|krYdVn|
fr||Vn|r|Vn|rxz||D]}|VqWn[ot|dd}|rt|rx.||D]}|VqWnrP|rPx||D]}|Vq>Wn|rx||D]}|VqoWnr|r|Vndk	r|}|krdn||<n
|}x||D]}|VqWdk	r%|=ndS(NR]R^R_R`sCircular reference detected(RRbRc(R5ReR[R`Rf(RlRBR)RmRnRORRoRpR8R*RrRst_defaultR:RERtR<RRURvR;RRQ(s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRQsJ
	((RORRBRuRnRRqRRR6RtRlRvRUR:RER8RRrRmRoR)RR;R*RR<((RlRBRR)RmRnR*RRoRpR8RqRORRrRsRRR:RERtR<RRuRURvR;RRQs6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyRMHsW>`VT)((RTtretdecimalRRtc_encode_basestring_asciiRLtsimplejson.decoderRtcompileRRRR
trangetit
setdefaulttchrR4RKRRRtobjectRRVRUR:RER8RrRmRoR)RR;R*RR<RM(((s6/usr/lib/python2.7/dist-packages/simplejson/encoder.pyt<module>sP	
!