diff options
-rw-r--r-- | docs/class-simplejson.JSONDecoder.html | 30 | ||||
-rw-r--r-- | docs/class-simplejson.JSONEncoder.html | 14 | ||||
-rw-r--r-- | docs/index.html | 35 | ||||
-rw-r--r-- | docs/module-index.html | 22 | ||||
-rw-r--r-- | docs/module-simplejson-index.html | 10 | ||||
-rw-r--r-- | docs/module-simplejson.html | 35 | ||||
-rw-r--r-- | docs/simplejson/__init__.py.html | 488 | ||||
-rw-r--r-- | docs/simplejson/decoder.py.html | 463 | ||||
-rw-r--r-- | simplejson/__init__.py | 68 | ||||
-rw-r--r-- | simplejson/decoder.py | 37 |
10 files changed, 702 insertions, 500 deletions
diff --git a/docs/class-simplejson.JSONDecoder.html b/docs/class-simplejson.JSONDecoder.html index 29322d2..d3a4503 100644 --- a/docs/class-simplejson.JSONDecoder.html +++ b/docs/class-simplejson.JSONDecoder.html @@ -9,7 +9,7 @@ <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -67,7 +67,7 @@ </LI> <LI> - <A HREF="simplejson/decoder.py.html?f=196&l=272#196" CLASS="pudge-section-link"> + <A HREF="simplejson/decoder.py.html?f=204&l=299#204" CLASS="pudge-section-link"> Source </A> </LI> @@ -75,7 +75,7 @@ </DIV> <DIV STYLE="clear: left"></DIV> <DIV CLASS="rst pudge-module-doc"> - <P>Performs the following translations in decoding:</P> + <P>Performs the following translations in decoding by default:</P> <TABLE BORDER="1" CLASS="docutils"> <COLGROUP> <COL WIDTH="44%"> @@ -126,8 +126,8 @@ their corresponding <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN>< <DIV CLASS="pudge-member routine alias"> <A NAME="__init__"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="class-simplejson.JSONDecoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, encoding=None, object_hook=None)</TT> - <A HREF="simplejson/decoder.py.html?f=229&l=245#229" TITLE="View Source">...</A> + <TT><A HREF="class-simplejson.JSONDecoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, encoding=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None)</TT> + <A HREF="simplejson/decoder.py.html?f=237&l=272#237" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -142,14 +142,26 @@ strings of other encodings should be passed in as <TT CLASS="docutils literal">< of every JSON object decoded and its return value will be used in place of the given <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT>. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_float</SPAN></TT>, 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. decimal.Decimal).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_int</SPAN></TT>, 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. float).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_constant</SPAN></TT>, if specified, will be called with one of the +following strings: -Infinity, Infinity, NaN, null, true, false. +This can be used to raise an exception if invalid JSON numbers +are encountered.</P> </DIV> </DIV><DIV CLASS="pudge-member routine alias"> <A NAME="decode"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="class-simplejson.JSONDecoder.html#decode" CLASS="pudge-obj-link">decode</A>(self, s, _w=<built-in method match of _sre.SRE_Pattern object at 0x1539770>)</TT> - <A HREF="simplejson/decoder.py.html?f=246&l=256#246" TITLE="View Source">...</A> + <TT><A HREF="class-simplejson.JSONDecoder.html#decode" CLASS="pudge-obj-link">decode</A>(self, s, _w=<built-in method match of _sre.SRE_Pattern object at 0x15a3728>)</TT> + <A HREF="simplejson/decoder.py.html?f=273&l=283#273" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -164,7 +176,7 @@ instance containing a JSON document) <A NAME="raw_decode"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="class-simplejson.JSONDecoder.html#raw_decode" CLASS="pudge-obj-link">raw_decode</A>(self, s, **kw)</TT> - <A HREF="simplejson/decoder.py.html?f=257&l=272#257" TITLE="View Source">...</A> + <A HREF="simplejson/decoder.py.html?f=284&l=299#284" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -187,7 +199,7 @@ have extraneous data at the end.</P> See - <A HREF="simplejson/decoder.py.html?f=196&l=272#196" TITLE="simplejson/decoder.py:196">the source</A> + <A HREF="simplejson/decoder.py.html?f=204&l=299#204" TITLE="simplejson/decoder.py:204">the source</A> for more information. </SMALL> </P> diff --git a/docs/class-simplejson.JSONEncoder.html b/docs/class-simplejson.JSONEncoder.html index 25bb919..6011119 100644 --- a/docs/class-simplejson.JSONEncoder.html +++ b/docs/class-simplejson.JSONEncoder.html @@ -9,7 +9,7 @@ <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -67,7 +67,7 @@ </LI> <LI> - <A HREF="simplejson/encoder.py.html?f=80&l=370#80" CLASS="pudge-section-link"> + <A HREF="simplejson/encoder.py.html?f=79&l=369#79" CLASS="pudge-section-link"> Source </A> </LI> @@ -126,7 +126,7 @@ implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeErro <A NAME="__init__"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="class-simplejson.JSONEncoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8')</TT> - <A HREF="simplejson/encoder.py.html?f=112&l=164#112" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=111&l=163#111" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -167,7 +167,7 @@ The default is UTF-8.</P> <A NAME="default"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="class-simplejson.JSONEncoder.html#default" CLASS="pudge-obj-link">default</A>(self, o)</TT> - <A HREF="simplejson/encoder.py.html?f=314&l=333#314" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=313&l=332#313" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -195,7 +195,7 @@ def default(self, o): <A NAME="encode"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="class-simplejson.JSONEncoder.html#encode" CLASS="pudge-obj-link">encode</A>(self, o)</TT> - <A HREF="simplejson/encoder.py.html?f=334&l=354#334" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=333&l=353#333" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -213,7 +213,7 @@ def default(self, o): <A NAME="iterencode"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="class-simplejson.JSONEncoder.html#iterencode" CLASS="pudge-obj-link">iterencode</A>(self, o)</TT> - <A HREF="simplejson/encoder.py.html?f=355&l=370#355" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=354&l=369#354" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -238,7 +238,7 @@ for chunk in JSONEncoder().iterencode(bigobject): See - <A HREF="simplejson/encoder.py.html?f=80&l=370#80" TITLE="simplejson/encoder.py:80">the source</A> + <A HREF="simplejson/encoder.py.html?f=79&l=369#79" TITLE="simplejson/encoder.py:79">the source</A> for more information. </SMALL> </P> diff --git a/docs/index.html b/docs/index.html index b65dcec..0d08a07 100644 --- a/docs/index.html +++ b/docs/index.html @@ -9,7 +9,7 @@ <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -144,6 +144,9 @@ u'"foo\x08ar' >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', ... object_hook=as_complex) (1+2j) +>>> import decimal +>>> simplejson.loads('1.1', parse_float=decimal.Decimal) +decimal.Decimal(1.1) </PRE> <P>Extending JSONEncoder:</P> <PRE CLASS="literal-block"> @@ -175,7 +178,7 @@ is a subset of YAML, so it may be used as a serializer for that as well.</P> <A NAME="dump"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="module-simplejson.html#dump" CLASS="pudge-obj-link">dump</A>(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=108&l=164#108" TITLE="View Source">...</A> + <A HREF="simplejson/__init__.py.html?f=111&l=167#111" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -215,7 +218,7 @@ the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> <A NAME="dumps"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="module-simplejson.html#dumps" CLASS="pudge-obj-link">dumps</A>(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=166&l=217#166" TITLE="View Source">...</A> + <A HREF="simplejson/__init__.py.html?f=169&l=220#169" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -252,8 +255,8 @@ the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> </DIV><DIV CLASS="pudge-member routine "> <A NAME="load"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="module-simplejson.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None, object_hook=None, **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=220&l=242#220" TITLE="View Source">...</A> + <TT><A HREF="module-simplejson.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw)</TT> + <A HREF="simplejson/__init__.py.html?f=223&l=248#223" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -279,8 +282,8 @@ kwarg.</P> </DIV><DIV CLASS="pudge-member routine "> <A NAME="loads"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="module-simplejson.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None, object_hook=None, **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=243&l=268#243" TITLE="View Source">...</A> + <TT><A HREF="module-simplejson.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw)</TT> + <A HREF="simplejson/__init__.py.html?f=249&l=298#249" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -296,6 +299,18 @@ are not allowed and should be decoded to <TT CLASS="docutils literal"><SPAN CLAS result of any object literal decode (a <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT>). The return value of <TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> will be used instead of the <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT>. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_float</SPAN></TT>, 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. decimal.Decimal).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_int</SPAN></TT>, 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. float).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_constant</SPAN></TT>, if specified, will be called with one of the +following strings: -Infinity, Infinity, NaN, null, true, false. +This can be used to raise an exception if invalid JSON numbers +are encountered.</P> <P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONDecoder</SPAN></TT> subclass, specify it with the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> @@ -310,7 +325,7 @@ kwarg.</P> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">C</SPAN> <TT> <A HREF="class-simplejson.JSONEncoder.html" CLASS="pudge-obj-link">JSONEncoder</A>(...)</TT> - <A HREF="simplejson/encoder.py.html?f=80&l=370#80" CLASS="pudge-member-view-source" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=79&l=369#79" CLASS="pudge-member-view-source" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -368,14 +383,14 @@ implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeErro <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">C</SPAN> <TT> <A HREF="class-simplejson.JSONDecoder.html" CLASS="pudge-obj-link">JSONDecoder</A>(...)</TT> - <A HREF="simplejson/decoder.py.html?f=196&l=272#196" CLASS="pudge-member-view-source" TITLE="View Source">...</A> + <A HREF="simplejson/decoder.py.html?f=204&l=299#204" CLASS="pudge-member-view-source" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> <P CLASS="pudge-member-blurb"> Simple JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> decoder </P> - <P>Performs the following translations in decoding:</P> + <P>Performs the following translations in decoding by default:</P> <TABLE BORDER="1" CLASS="docutils"> <COLGROUP> <COL WIDTH="44%"> diff --git a/docs/module-index.html b/docs/module-index.html index f3c9d32..2dcc000 100644 --- a/docs/module-index.html +++ b/docs/module-index.html @@ -2,14 +2,14 @@ <HTML> <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> - <TITLE>simplejson 1.7.5 -- Module Reference</TITLE> + <TITLE>simplejson 1.8 -- Module Reference</TITLE> <LINK HREF="layout.css" TYPE="text/css" REL="stylesheet"> </HEAD> <BODY> <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -31,7 +31,7 @@ <DIV ID="main-content"> <H1 CLASS="pudge-member-page-heading"> - simplejson 1.7.5 + simplejson 1.8 </H1> <H4 CLASS="pudge-member-page-subheading"> Module Reference @@ -57,6 +57,14 @@ Implementation of JSONEncoder </DD> + <DT><TT><A HREF="module-simplejson.scanner.html" CLASS="pudge-obj-link"> + simplejson.scanner + </A></TT> + </DT> + <DD> + Iterator based sre token scanner + </DD> + <DT><TT><A HREF="module-simplejson.jsonfilter.html" CLASS="pudge-obj-link"> simplejson.jsonfilter </A></TT> @@ -77,14 +85,6 @@ Implementation of JSONDecoder </DD> - <DT><TT><A HREF="module-simplejson.scanner.html" CLASS="pudge-obj-link"> - simplejson.scanner - </A></TT> - </DT> - <DD> - Iterator based sre token scanner - </DD> - <DT><TT><A HREF="module-simplejson._speedups.html" CLASS="pudge-obj-link"> simplejson._speedups </A></TT> diff --git a/docs/module-simplejson-index.html b/docs/module-simplejson-index.html index 1e70d99..685fe97 100644 --- a/docs/module-simplejson-index.html +++ b/docs/module-simplejson-index.html @@ -9,7 +9,7 @@ <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -61,7 +61,7 @@ <A HREF="module-simplejson.html#load"> <TT>load</TT> </A> - <A HREF="simplejson/__init__.py.html?f=220&l=242#220">...</A> + <A HREF="simplejson/__init__.py.html?f=223&l=248#223">...</A> <SMALL> - <SPAN CLASS="rst">Deserialize <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> (a <TT CLASS="docutils literal"><SPAN CLASS="pre">.read()</SPAN></TT>-supporting file-like object containing a JSON document) to a Python object.</SPAN> @@ -72,7 +72,7 @@ a JSON document) to a Python object.</SPAN> <A HREF="module-simplejson.html#dump"> <TT>dump</TT> </A> - <A HREF="simplejson/__init__.py.html?f=108&l=164#108">...</A> + <A HREF="simplejson/__init__.py.html?f=111&l=167#111">...</A> <SMALL> - <SPAN CLASS="rst">Serialize <TT CLASS="docutils literal"><SPAN CLASS="pre">obj</SPAN></TT> as a JSON formatted stream to <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> (a <TT CLASS="docutils literal"><SPAN CLASS="pre">.write()</SPAN></TT>-supporting file-like object).</SPAN> @@ -83,7 +83,7 @@ a JSON document) to a Python object.</SPAN> <A HREF="module-simplejson.html#dumps"> <TT>dumps</TT> </A> - <A HREF="simplejson/__init__.py.html?f=166&l=217#166">...</A> + <A HREF="simplejson/__init__.py.html?f=169&l=220#169">...</A> <SMALL> - <SPAN CLASS="rst">Serialize <TT CLASS="docutils literal"><SPAN CLASS="pre">obj</SPAN></TT> to a JSON formatted <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>.</SPAN> </SMALL> @@ -93,7 +93,7 @@ a JSON document) to a Python object.</SPAN> <A HREF="module-simplejson.html#loads"> <TT>loads</TT> </A> - <A HREF="simplejson/__init__.py.html?f=243&l=268#243">...</A> + <A HREF="simplejson/__init__.py.html?f=249&l=298#249">...</A> <SMALL> - <SPAN CLASS="rst">Deserialize <TT CLASS="docutils literal"><SPAN CLASS="pre">s</SPAN></TT> (a <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> or <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> instance containing a JSON document) to a Python object.</SPAN> diff --git a/docs/module-simplejson.html b/docs/module-simplejson.html index b65dcec..0d08a07 100644 --- a/docs/module-simplejson.html +++ b/docs/module-simplejson.html @@ -9,7 +9,7 @@ <DIV ID="page"> <DIV ID="top-nav"> - <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.7.5</A></H1> + <H1 ID="doc-title"><A HREF="index.html" REL="index">simplejson 1.8</A></H1> <DIV CLASS="online-navigation"> <A HREF="index.html" REL="index">index</A> <SPAN ID="nav-docs"> @@ -144,6 +144,9 @@ u'"foo\x08ar' >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', ... object_hook=as_complex) (1+2j) +>>> import decimal +>>> simplejson.loads('1.1', parse_float=decimal.Decimal) +decimal.Decimal(1.1) </PRE> <P>Extending JSONEncoder:</P> <PRE CLASS="literal-block"> @@ -175,7 +178,7 @@ is a subset of YAML, so it may be used as a serializer for that as well.</P> <A NAME="dump"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="module-simplejson.html#dump" CLASS="pudge-obj-link">dump</A>(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=108&l=164#108" TITLE="View Source">...</A> + <A HREF="simplejson/__init__.py.html?f=111&l=167#111" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -215,7 +218,7 @@ the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> <A NAME="dumps"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> <TT><A HREF="module-simplejson.html#dumps" CLASS="pudge-obj-link">dumps</A>(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=166&l=217#166" TITLE="View Source">...</A> + <A HREF="simplejson/__init__.py.html?f=169&l=220#169" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -252,8 +255,8 @@ the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> </DIV><DIV CLASS="pudge-member routine "> <A NAME="load"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="module-simplejson.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None, object_hook=None, **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=220&l=242#220" TITLE="View Source">...</A> + <TT><A HREF="module-simplejson.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw)</TT> + <A HREF="simplejson/__init__.py.html?f=223&l=248#223" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -279,8 +282,8 @@ kwarg.</P> </DIV><DIV CLASS="pudge-member routine "> <A NAME="loads"></A> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">f</SPAN> - <TT><A HREF="module-simplejson.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None, object_hook=None, **kw)</TT> - <A HREF="simplejson/__init__.py.html?f=243&l=268#243" TITLE="View Source">...</A> + <TT><A HREF="module-simplejson.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, **kw)</TT> + <A HREF="simplejson/__init__.py.html?f=249&l=298#249" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -296,6 +299,18 @@ are not allowed and should be decoded to <TT CLASS="docutils literal"><SPAN CLAS result of any object literal decode (a <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT>). The return value of <TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> will be used instead of the <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT>. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_float</SPAN></TT>, 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. decimal.Decimal).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_int</SPAN></TT>, 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. float).</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">parse_constant</SPAN></TT>, if specified, will be called with one of the +following strings: -Infinity, Infinity, NaN, null, true, false. +This can be used to raise an exception if invalid JSON numbers +are encountered.</P> <P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONDecoder</SPAN></TT> subclass, specify it with the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> @@ -310,7 +325,7 @@ kwarg.</P> <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">C</SPAN> <TT> <A HREF="class-simplejson.JSONEncoder.html" CLASS="pudge-obj-link">JSONEncoder</A>(...)</TT> - <A HREF="simplejson/encoder.py.html?f=80&l=370#80" CLASS="pudge-member-view-source" TITLE="View Source">...</A> + <A HREF="simplejson/encoder.py.html?f=79&l=369#79" CLASS="pudge-member-view-source" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> @@ -368,14 +383,14 @@ implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeErro <H4 CLASS="pudge-member-name"><SPAN CLASS="prefix">C</SPAN> <TT> <A HREF="class-simplejson.JSONDecoder.html" CLASS="pudge-obj-link">JSONDecoder</A>(...)</TT> - <A HREF="simplejson/decoder.py.html?f=196&l=272#196" CLASS="pudge-member-view-source" TITLE="View Source">...</A> + <A HREF="simplejson/decoder.py.html?f=204&l=299#204" CLASS="pudge-member-view-source" TITLE="View Source">...</A> </H4> <DIV CLASS="pudge-section rst"> <P CLASS="pudge-member-blurb"> Simple JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> decoder </P> - <P>Performs the following translations in decoding:</P> + <P>Performs the following translations in decoding by default:</P> <TABLE BORDER="1" CLASS="docutils"> <COLGROUP> <COL WIDTH="44%"> diff --git a/docs/simplejson/__init__.py.html b/docs/simplejson/__init__.py.html index f4767ef..b6f9090 100644 --- a/docs/simplejson/__init__.py.html +++ b/docs/simplejson/__init__.py.html @@ -137,221 +137,275 @@ function show_line_range() { <a class="lnum" href="#65" name="65">0065</a><span class="string">    >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}',</span><br /> <a class="lnum" href="#66" name="66">0066</a><span class="string">    ...     object_hook=as_complex)</span><br /> <a class="lnum" href="#67" name="67">0067</a><span class="string">    (1+2j)</span><br /> -<a class="lnum" href="#68" name="68">0068</a><span class="string"></span><br /> -<a class="lnum" href="#69" name="69">0069</a><span class="string">Extending JSONEncoder::</span><br /> -<a class="lnum" href="#70" name="70">0070</a><span class="string">    </span><br /> -<a class="lnum" href="#71" name="71">0071</a><span class="string">    >>> import simplejson</span><br /> -<a class="lnum" href="#72" name="72">0072</a><span class="string">    >>> class ComplexEncoder(simplejson.JSONEncoder):</span><br /> -<a class="lnum" href="#73" name="73">0073</a><span class="string">    ...     def default(self, obj):</span><br /> -<a class="lnum" href="#74" name="74">0074</a><span class="string">    ...         if isinstance(obj, complex):</span><br /> -<a class="lnum" href="#75" name="75">0075</a><span class="string">    ...             return [obj.real, obj.imag]</span><br /> -<a class="lnum" href="#76" name="76">0076</a><span class="string">    ...         return simplejson.JSONEncoder.default(self, obj)</span><br /> -<a class="lnum" href="#77" name="77">0077</a><span class="string">    ... </span><br /> -<a class="lnum" href="#78" name="78">0078</a><span class="string">    >>> dumps(2 + 1j, cls=ComplexEncoder)</span><br /> -<a class="lnum" href="#79" name="79">0079</a><span class="string">    '[2.0, 1.0]'</span><br /> -<a class="lnum" href="#80" name="80">0080</a><span class="string">    >>> ComplexEncoder().encode(2 + 1j)</span><br /> -<a class="lnum" href="#81" name="81">0081</a><span class="string">    '[2.0, 1.0]'</span><br /> -<a class="lnum" href="#82" name="82">0082</a><span class="string">    >>> list(ComplexEncoder().iterencode(2 + 1j))</span><br /> -<a class="lnum" href="#83" name="83">0083</a><span class="string">    ['[', '2.0', ', ', '1.0', ']']</span><br /> -<a class="lnum" href="#84" name="84">0084</a><span class="string">    </span><br /> -<a class="lnum" href="#85" name="85">0085</a><span class="string"></span><br /> -<a class="lnum" href="#86" name="86">0086</a><span class="string">Note that the JSON produced by this module's default settings</span><br /> -<a class="lnum" href="#87" name="87">0087</a><span class="string">is a subset of YAML, so it may be used as a serializer for that as well.</span><br /> -<a class="lnum" href="#88" name="88">0088</a><span class="string">"""</span><br /> -<a class="lnum" href="#89" name="89">0089</a><span class="name">__version__</span> <span class="op">=</span> <span class="string">'1.7.5'</span><br /> -<a class="lnum" href="#90" name="90">0090</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><br /> -<a class="lnum" href="#91" name="91">0091</a>    <span class="string">'dump'</span><span class="op">,</span> <span class="string">'dumps'</span><span class="op">,</span> <span class="string">'load'</span><span class="op">,</span> <span class="string">'loads'</span><span class="op">,</span><br /> -<a class="lnum" href="#92" name="92">0092</a>    <span class="string">'JSONDecoder'</span><span class="op">,</span> <span class="string">'JSONEncoder'</span><span class="op">,</span><br /> -<a class="lnum" href="#93" name="93">0093</a><span class="op">]</span><br /> -<a class="lnum" href="#94" name="94">0094</a><br /> -<a class="lnum" href="#95" name="95">0095</a><span class="keyword">from</span> <span class="name">decoder</span> <span class="keyword">import</span> <span class="name">JSONDecoder</span><br /> -<a class="lnum" href="#96" name="96">0096</a><span class="keyword">from</span> <span class="name">encoder</span> <span class="keyword">import</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#68" name="68">0068</a><span class="string">    >>> import decimal</span><br /> +<a class="lnum" href="#69" name="69">0069</a><span class="string">    >>> simplejson.loads('1.1', parse_float=decimal.Decimal)</span><br /> +<a class="lnum" href="#70" name="70">0070</a><span class="string">    decimal.Decimal(1.1)</span><br /> +<a class="lnum" href="#71" name="71">0071</a><span class="string"></span><br /> +<a class="lnum" href="#72" name="72">0072</a><span class="string">Extending JSONEncoder::</span><br /> +<a class="lnum" href="#73" name="73">0073</a><span class="string">    </span><br /> +<a class="lnum" href="#74" name="74">0074</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#75" name="75">0075</a><span class="string">    >>> class ComplexEncoder(simplejson.JSONEncoder):</span><br /> +<a class="lnum" href="#76" name="76">0076</a><span class="string">    ...     def default(self, obj):</span><br /> +<a class="lnum" href="#77" name="77">0077</a><span class="string">    ...         if isinstance(obj, complex):</span><br /> +<a class="lnum" href="#78" name="78">0078</a><span class="string">    ...             return [obj.real, obj.imag]</span><br /> +<a class="lnum" href="#79" name="79">0079</a><span class="string">    ...         return simplejson.JSONEncoder.default(self, obj)</span><br /> +<a class="lnum" href="#80" name="80">0080</a><span class="string">    ... </span><br /> +<a class="lnum" href="#81" name="81">0081</a><span class="string">    >>> dumps(2 + 1j, cls=ComplexEncoder)</span><br /> +<a class="lnum" href="#82" name="82">0082</a><span class="string">    '[2.0, 1.0]'</span><br /> +<a class="lnum" href="#83" name="83">0083</a><span class="string">    >>> ComplexEncoder().encode(2 + 1j)</span><br /> +<a class="lnum" href="#84" name="84">0084</a><span class="string">    '[2.0, 1.0]'</span><br /> +<a class="lnum" href="#85" name="85">0085</a><span class="string">    >>> list(ComplexEncoder().iterencode(2 + 1j))</span><br /> +<a class="lnum" href="#86" name="86">0086</a><span class="string">    ['[', '2.0', ', ', '1.0', ']']</span><br /> +<a class="lnum" href="#87" name="87">0087</a><span class="string">    </span><br /> +<a class="lnum" href="#88" name="88">0088</a><span class="string"></span><br /> +<a class="lnum" href="#89" name="89">0089</a><span class="string">Note that the JSON produced by this module's default settings</span><br /> +<a class="lnum" href="#90" name="90">0090</a><span class="string">is a subset of YAML, so it may be used as a serializer for that as well.</span><br /> +<a class="lnum" href="#91" name="91">0091</a><span class="string">"""</span><br /> +<a class="lnum" href="#92" name="92">0092</a><span class="name">__version__</span> <span class="op">=</span> <span class="string">'1.8'</span><br /> +<a class="lnum" href="#93" name="93">0093</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><br /> +<a class="lnum" href="#94" name="94">0094</a>    <span class="string">'dump'</span><span class="op">,</span> <span class="string">'dumps'</span><span class="op">,</span> <span class="string">'load'</span><span class="op">,</span> <span class="string">'loads'</span><span class="op">,</span><br /> +<a class="lnum" href="#95" name="95">0095</a>    <span class="string">'JSONDecoder'</span><span class="op">,</span> <span class="string">'JSONEncoder'</span><span class="op">,</span><br /> +<a class="lnum" href="#96" name="96">0096</a><span class="op">]</span><br /> <a class="lnum" href="#97" name="97">0097</a><br /> -<a class="lnum" href="#98" name="98">0098</a><span class="name">_default_encoder</span> <span class="op">=</span> <span class="name">JSONEncoder</span><span class="op">(</span><br /> -<a class="lnum" href="#99" name="99">0099</a>    <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span><br /> -<a class="lnum" href="#100" name="100">0100</a>    <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> -<a class="lnum" href="#101" name="101">0101</a>    <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> -<a class="lnum" href="#102" name="102">0102</a>    <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> -<a class="lnum" href="#103" name="103">0103</a>    <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> -<a class="lnum" href="#104" name="104">0104</a>    <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> -<a class="lnum" href="#105" name="105">0105</a>    <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><br /> -<a class="lnum" href="#106" name="106">0106</a><span class="op">)</span><br /> -<a class="lnum" href="#107" name="107">0107</a><br /> -<a class="lnum" href="#108" name="108">0108</a><span class="keyword">def</span> <span class="name">dump</span><span class="op">(</span><span class="name">obj</span><span class="op">,</span> <span class="name">fp</span><span class="op">,</span> <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> -<a class="lnum" href="#109" name="109">0109</a>        <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> -<a class="lnum" href="#110" name="110">0110</a>        <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#111" name="111">0111</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#112" name="112">0112</a><span class="string">    Serialize ``obj`` as a JSON formatted stream to ``fp`` (a</span><br /> -<a class="lnum" href="#113" name="113">0113</a><span class="string">    ``.write()``-supporting file-like object).</span><br /> -<a class="lnum" href="#114" name="114">0114</a><span class="string"></span><br /> -<a class="lnum" href="#115" name="115">0115</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> -<a class="lnum" href="#116" name="116">0116</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> -<a class="lnum" href="#117" name="117">0117</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> -<a class="lnum" href="#118" name="118">0118</a><span class="string"></span><br /> -<a class="lnum" href="#119" name="119">0119</a><span class="string">    If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``</span><br /> -<a class="lnum" href="#120" name="120">0120</a><span class="string">    may be ``unicode`` instances, subject to normal Python ``str`` to</span><br /> -<a class="lnum" href="#121" name="121">0121</a><span class="string">    ``unicode`` coercion rules. Unless ``fp.write()`` explicitly</span><br /> -<a class="lnum" href="#122" name="122">0122</a><span class="string">    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely</span><br /> -<a class="lnum" href="#123" name="123">0123</a><span class="string">    to cause an error.</span><br /> -<a class="lnum" href="#124" name="124">0124</a><span class="string"></span><br /> -<a class="lnum" href="#125" name="125">0125</a><span class="string">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> -<a class="lnum" href="#126" name="126">0126</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> -<a class="lnum" href="#127" name="127">0127</a><span class="string">    result in an ``OverflowError`` (or worse).</span><br /> -<a class="lnum" href="#128" name="128">0128</a><span class="string"></span><br /> -<a class="lnum" href="#129" name="129">0129</a><span class="string">    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> -<a class="lnum" href="#130" name="130">0130</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)</span><br /> -<a class="lnum" href="#131" name="131">0131</a><span class="string">    in strict compliance of the JSON specification, instead of using the</span><br /> -<a class="lnum" href="#132" name="132">0132</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> -<a class="lnum" href="#133" name="133">0133</a><span class="string"></span><br /> -<a class="lnum" href="#134" name="134">0134</a><span class="string">    If ``indent`` is a non-negative integer, then JSON array elements and object</span><br /> -<a class="lnum" href="#135" name="135">0135</a><span class="string">    members will be pretty-printed with that indent level. An indent level</span><br /> -<a class="lnum" href="#136" name="136">0136</a><span class="string">    of 0 will only insert newlines. ``None`` is the most compact representation.</span><br /> -<a class="lnum" href="#137" name="137">0137</a><span class="string"></span><br /> -<a class="lnum" href="#138" name="138">0138</a><span class="string">    If ``separators`` is an ``(item_separator, dict_separator)`` tuple</span><br /> -<a class="lnum" href="#139" name="139">0139</a><span class="string">    then it will be used instead of the default ``(', ', ': ')`` separators.</span><br /> -<a class="lnum" href="#140" name="140">0140</a><span class="string">    ``(',', ':')`` is the most compact JSON representation.</span><br /> -<a class="lnum" href="#141" name="141">0141</a><span class="string"></span><br /> -<a class="lnum" href="#142" name="142">0142</a><span class="string">    ``encoding`` is the character encoding for str instances, default is UTF-8.</span><br /> -<a class="lnum" href="#143" name="143">0143</a><span class="string"></span><br /> -<a class="lnum" href="#144" name="144">0144</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> -<a class="lnum" href="#145" name="145">0145</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> -<a class="lnum" href="#146" name="146">0146</a><span class="string">    the ``cls`` kwarg.</span><br /> -<a class="lnum" href="#147" name="147">0147</a><span class="string">    """</span><br /> -<a class="lnum" href="#148" name="148">0148</a>    <span class="comment"># cached encoder</span><br /> -<a class="lnum" href="#149" name="149">0149</a><span class="comment"></span>    <span class="keyword">if</span> <span class="op">(</span><span class="name">skipkeys</span> <span class="keyword">is</span> <span class="name">False</span> <span class="keyword">and</span> <span class="name">ensure_ascii</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#150" name="150">0150</a>        <span class="name">check_circular</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span> <span class="name">allow_nan</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#151" name="151">0151</a>        <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">indent</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">separators</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#152" name="152">0152</a>        <span class="name">encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#153" name="153">0153</a>        <span class="name">iterable</span> <span class="op">=</span> <span class="name">_default_encoder</span><span class="op">.</span><span class="name">iterencode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> -<a class="lnum" href="#154" name="154">0154</a>    <span class="keyword">else</span><span class="op">:</span><br /> -<a class="lnum" href="#155" name="155">0155</a>        <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#156" name="156">0156</a>            <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> -<a class="lnum" href="#157" name="157">0157</a>        <span class="name">iterable</span> <span class="op">=</span> <span class="name">cls</span><span class="op">(</span><span class="name">skipkeys</span><span class="op">=</span><span class="name">skipkeys</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">ensure_ascii</span><span class="op">,</span><br /> -<a class="lnum" href="#158" name="158">0158</a>            <span class="name">check_circular</span><span class="op">=</span><span class="name">check_circular</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">allow_nan</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">indent</span><span class="op">,</span><br /> -<a class="lnum" href="#159" name="159">0159</a>            <span class="name">separators</span><span class="op">=</span><span class="name">separators</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">iterencode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> -<a class="lnum" href="#160" name="160">0160</a>    <span class="comment"># could accelerate with writelines in some versions of Python, at</span><br /> -<a class="lnum" href="#161" name="161">0161</a><span class="comment"></span>    <span class="comment"># a debuggability cost</span><br /> -<a class="lnum" href="#162" name="162">0162</a><span class="comment"></span>    <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">iterable</span><span class="op">:</span><br /> -<a class="lnum" href="#163" name="163">0163</a>        <span class="name">fp</span><span class="op">.</span><span class="name">write</span><span class="op">(</span><span class="name">chunk</span><span class="op">)</span><br /> -<a class="lnum" href="#164" name="164">0164</a><br /> -<a class="lnum" href="#165" name="165">0165</a><br /> -<a class="lnum" href="#166" name="166">0166</a><span class="keyword">def</span> <span class="name">dumps</span><span class="op">(</span><span class="name">obj</span><span class="op">,</span> <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> -<a class="lnum" href="#167" name="167">0167</a>        <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> -<a class="lnum" href="#168" name="168">0168</a>        <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#169" name="169">0169</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#170" name="170">0170</a><span class="string">    Serialize ``obj`` to a JSON formatted ``str``.</span><br /> -<a class="lnum" href="#171" name="171">0171</a><span class="string"></span><br /> -<a class="lnum" href="#172" name="172">0172</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> -<a class="lnum" href="#173" name="173">0173</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> -<a class="lnum" href="#174" name="174">0174</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> -<a class="lnum" href="#175" name="175">0175</a><span class="string"></span><br /> -<a class="lnum" href="#176" name="176">0176</a><span class="string">    If ``ensure_ascii`` is ``False``, then the return value will be a</span><br /> -<a class="lnum" href="#177" name="177">0177</a><span class="string">    ``unicode`` instance subject to normal Python ``str`` to ``unicode``</span><br /> -<a class="lnum" href="#178" name="178">0178</a><span class="string">    coercion rules instead of being escaped to an ASCII ``str``.</span><br /> -<a class="lnum" href="#179" name="179">0179</a><span class="string"></span><br /> -<a class="lnum" href="#180" name="180">0180</a><span class="string">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> -<a class="lnum" href="#181" name="181">0181</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> -<a class="lnum" href="#182" name="182">0182</a><span class="string">    result in an ``OverflowError`` (or worse).</span><br /> -<a class="lnum" href="#183" name="183">0183</a><span class="string"></span><br /> -<a class="lnum" href="#184" name="184">0184</a><span class="string">    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> -<a class="lnum" href="#185" name="185">0185</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in</span><br /> -<a class="lnum" href="#186" name="186">0186</a><span class="string">    strict compliance of the JSON specification, instead of using the</span><br /> -<a class="lnum" href="#187" name="187">0187</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> -<a class="lnum" href="#188" name="188">0188</a><span class="string"></span><br /> -<a class="lnum" href="#189" name="189">0189</a><span class="string">    If ``indent`` is a non-negative integer, then JSON array elements and</span><br /> -<a class="lnum" href="#190" name="190">0190</a><span class="string">    object members will be pretty-printed with that indent level. An indent</span><br /> -<a class="lnum" href="#191" name="191">0191</a><span class="string">    level of 0 will only insert newlines. ``None`` is the most compact</span><br /> -<a class="lnum" href="#192" name="192">0192</a><span class="string">    representation.</span><br /> -<a class="lnum" href="#193" name="193">0193</a><span class="string"></span><br /> -<a class="lnum" href="#194" name="194">0194</a><span class="string">    If ``separators`` is an ``(item_separator, dict_separator)`` tuple</span><br /> -<a class="lnum" href="#195" name="195">0195</a><span class="string">    then it will be used instead of the default ``(', ', ': ')`` separators.</span><br /> -<a class="lnum" href="#196" name="196">0196</a><span class="string">    ``(',', ':')`` is the most compact JSON representation.</span><br /> -<a class="lnum" href="#197" name="197">0197</a><span class="string"></span><br /> -<a class="lnum" href="#198" name="198">0198</a><span class="string">    ``encoding`` is the character encoding for str instances, default is UTF-8.</span><br /> -<a class="lnum" href="#199" name="199">0199</a><span class="string"></span><br /> -<a class="lnum" href="#200" name="200">0200</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> -<a class="lnum" href="#201" name="201">0201</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> -<a class="lnum" href="#202" name="202">0202</a><span class="string">    the ``cls`` kwarg.</span><br /> -<a class="lnum" href="#203" name="203">0203</a><span class="string">    """</span><br /> -<a class="lnum" href="#204" name="204">0204</a>    <span class="comment"># cached encoder</span><br /> -<a class="lnum" href="#205" name="205">0205</a><span class="comment"></span>    <span class="keyword">if</span> <span class="op">(</span><span class="name">skipkeys</span> <span class="keyword">is</span> <span class="name">False</span> <span class="keyword">and</span> <span class="name">ensure_ascii</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#206" name="206">0206</a>        <span class="name">check_circular</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span> <span class="name">allow_nan</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#207" name="207">0207</a>        <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">indent</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">separators</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> -<a class="lnum" href="#208" name="208">0208</a>        <span class="name">encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#209" name="209">0209</a>        <span class="keyword">return</span> <span class="name">_default_encoder</span><span class="op">.</span><span class="name">encode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> -<a class="lnum" href="#210" name="210">0210</a>    <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#211" name="211">0211</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> -<a class="lnum" href="#212" name="212">0212</a>    <span class="keyword">return</span> <span class="name">cls</span><span class="op">(</span><br /> -<a class="lnum" href="#213" name="213">0213</a>        <span class="name">skipkeys</span><span class="op">=</span><span class="name">skipkeys</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">ensure_ascii</span><span class="op">,</span><br /> -<a class="lnum" href="#214" name="214">0214</a>        <span class="name">check_circular</span><span class="op">=</span><span class="name">check_circular</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">allow_nan</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">indent</span><span class="op">,</span><br /> -<a class="lnum" href="#215" name="215">0215</a>        <span class="name">separators</span><span class="op">=</span><span class="name">separators</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span><br /> -<a class="lnum" href="#216" name="216">0216</a>        <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">encode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> -<a class="lnum" href="#217" name="217">0217</a><br /> -<a class="lnum" href="#218" name="218">0218</a><span class="name">_default_decoder</span> <span class="op">=</span> <span class="name">JSONDecoder</span><span class="op">(</span><span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><br /> -<a class="lnum" href="#219" name="219">0219</a><br /> -<a class="lnum" href="#220" name="220">0220</a><span class="keyword">def</span> <span class="name">load</span><span class="op">(</span><span class="name">fp</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#221" name="221">0221</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#222" name="222">0222</a><span class="string">    Deserialize ``fp`` (a ``.read()``-supporting file-like object containing</span><br /> -<a class="lnum" href="#223" name="223">0223</a><span class="string">    a JSON document) to a Python object.</span><br /> -<a class="lnum" href="#224" name="224">0224</a><span class="string"></span><br /> -<a class="lnum" href="#225" name="225">0225</a><span class="string">    If the contents of ``fp`` is encoded with an ASCII based encoding other</span><br /> -<a class="lnum" href="#226" name="226">0226</a><span class="string">    than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must</span><br /> -<a class="lnum" href="#227" name="227">0227</a><span class="string">    be specified. Encodings that are not ASCII based (such as UCS-2) are</span><br /> -<a class="lnum" href="#228" name="228">0228</a><span class="string">    not allowed, and should be wrapped with</span><br /> -<a class="lnum" href="#229" name="229">0229</a><span class="string">    ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``</span><br /> -<a class="lnum" href="#230" name="230">0230</a><span class="string">    object and passed to ``loads()``</span><br /> -<a class="lnum" href="#231" name="231">0231</a><span class="string"></span><br /> -<a class="lnum" href="#232" name="232">0232</a><span class="string">    ``object_hook`` is an optional function that will be called with the</span><br /> -<a class="lnum" href="#233" name="233">0233</a><span class="string">    result of any object literal decode (a ``dict``). The return value of</span><br /> -<a class="lnum" href="#234" name="234">0234</a><span class="string">    ``object_hook`` will be used instead of the ``dict``. This feature</span><br /> -<a class="lnum" href="#235" name="235">0235</a><span class="string">    can be used to implement custom decoders (e.g. JSON-RPC class hinting).</span><br /> -<a class="lnum" href="#236" name="236">0236</a><span class="string">    </span><br /> -<a class="lnum" href="#237" name="237">0237</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> -<a class="lnum" href="#238" name="238">0238</a><span class="string">    kwarg.</span><br /> -<a class="lnum" href="#239" name="239">0239</a><span class="string">    """</span><br /> -<a class="lnum" href="#240" name="240">0240</a>    <span class="keyword">return</span> <span class="name">loads</span><span class="op">(</span><span class="name">fp</span><span class="op">.</span><span class="name">read</span><span class="op">(</span><span class="op">)</span><span class="op">,</span><br /> -<a class="lnum" href="#241" name="241">0241</a>        <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">cls</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">object_hook</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><br /> -<a class="lnum" href="#242" name="242">0242</a><br /> -<a class="lnum" href="#243" name="243">0243</a><span class="keyword">def</span> <span class="name">loads</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#244" name="244">0244</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#245" name="245">0245</a><span class="string">    Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON</span><br /> -<a class="lnum" href="#246" name="246">0246</a><span class="string">    document) to a Python object.</span><br /> -<a class="lnum" href="#247" name="247">0247</a><span class="string"></span><br /> -<a class="lnum" href="#248" name="248">0248</a><span class="string">    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding</span><br /> -<a class="lnum" href="#249" name="249">0249</a><span class="string">    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name</span><br /> -<a class="lnum" href="#250" name="250">0250</a><span class="string">    must be specified. Encodings that are not ASCII based (such as UCS-2)</span><br /> -<a class="lnum" href="#251" name="251">0251</a><span class="string">    are not allowed and should be decoded to ``unicode`` first.</span><br /> -<a class="lnum" href="#252" name="252">0252</a><span class="string"></span><br /> -<a class="lnum" href="#253" name="253">0253</a><span class="string">    ``object_hook`` is an optional function that will be called with the</span><br /> -<a class="lnum" href="#254" name="254">0254</a><span class="string">    result of any object literal decode (a ``dict``). The return value of</span><br /> -<a class="lnum" href="#255" name="255">0255</a><span class="string">    ``object_hook`` will be used instead of the ``dict``. This feature</span><br /> -<a class="lnum" href="#256" name="256">0256</a><span class="string">    can be used to implement custom decoders (e.g. JSON-RPC class hinting).</span><br /> -<a class="lnum" href="#257" name="257">0257</a><span class="string"></span><br /> -<a class="lnum" href="#258" name="258">0258</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> -<a class="lnum" href="#259" name="259">0259</a><span class="string">    kwarg.</span><br /> -<a class="lnum" href="#260" name="260">0260</a><span class="string">    """</span><br /> -<a class="lnum" href="#261" name="261">0261</a>    <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">encoding</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">:</span><br /> -<a class="lnum" href="#262" name="262">0262</a>        <span class="keyword">return</span> <span class="name">_default_decoder</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> -<a class="lnum" href="#263" name="263">0263</a>    <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#264" name="264">0264</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONDecoder</span><br /> -<a class="lnum" href="#265" name="265">0265</a>    <span class="keyword">if</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#266" name="266">0266</a>        <span class="name">kw</span><span class="op">[</span><span class="string">'object_hook'</span><span class="op">]</span> <span class="op">=</span> <span class="name">object_hook</span><br /> -<a class="lnum" href="#267" name="267">0267</a>    <span class="keyword">return</span> <span class="name">cls</span><span class="op">(</span><span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> -<a class="lnum" href="#268" name="268">0268</a><br /> -<a class="lnum" href="#269" name="269">0269</a><span class="keyword">def</span> <span class="name">read</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#270" name="270">0270</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#271" name="271">0271</a><span class="string">    json-py API compatibility hook. Use loads(s) instead.</span><br /> -<a class="lnum" href="#272" name="272">0272</a><span class="string">    """</span><br /> -<a class="lnum" href="#273" name="273">0273</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> -<a class="lnum" href="#274" name="274">0274</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.loads(s) should be used instead of read(s)"</span><span class="op">,</span><br /> -<a class="lnum" href="#275" name="275">0275</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> -<a class="lnum" href="#276" name="276">0276</a>    <span class="keyword">return</span> <span class="name">loads</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> -<a class="lnum" href="#277" name="277">0277</a><br /> -<a class="lnum" href="#278" name="278">0278</a><span class="keyword">def</span> <span class="name">write</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#279" name="279">0279</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#280" name="280">0280</a><span class="string">    json-py API compatibility hook. Use dumps(s) instead.</span><br /> -<a class="lnum" href="#281" name="281">0281</a><span class="string">    """</span><br /> -<a class="lnum" href="#282" name="282">0282</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> -<a class="lnum" href="#283" name="283">0283</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.dumps(s) should be used instead of write(s)"</span><span class="op">,</span><br /> -<a class="lnum" href="#284" name="284">0284</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> -<a class="lnum" href="#285" name="285">0285</a>    <span class="keyword">return</span> <span class="name">dumps</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span></code></div></body></html>
\ No newline at end of file +<a class="lnum" href="#98" name="98">0098</a><span class="keyword">from</span> <span class="name">decoder</span> <span class="keyword">import</span> <span class="name">JSONDecoder</span><br /> +<a class="lnum" href="#99" name="99">0099</a><span class="keyword">from</span> <span class="name">encoder</span> <span class="keyword">import</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#100" name="100">0100</a><br /> +<a class="lnum" href="#101" name="101">0101</a><span class="name">_default_encoder</span> <span class="op">=</span> <span class="name">JSONEncoder</span><span class="op">(</span><br /> +<a class="lnum" href="#102" name="102">0102</a>    <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span><br /> +<a class="lnum" href="#103" name="103">0103</a>    <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#104" name="104">0104</a>    <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#105" name="105">0105</a>    <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#106" name="106">0106</a>    <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#107" name="107">0107</a>    <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#108" name="108">0108</a>    <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><br /> +<a class="lnum" href="#109" name="109">0109</a><span class="op">)</span><br /> +<a class="lnum" href="#110" name="110">0110</a><br /> +<a class="lnum" href="#111" name="111">0111</a><span class="keyword">def</span> <span class="name">dump</span><span class="op">(</span><span class="name">obj</span><span class="op">,</span> <span class="name">fp</span><span class="op">,</span> <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#112" name="112">0112</a>        <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#113" name="113">0113</a>        <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#114" name="114">0114</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#115" name="115">0115</a><span class="string">    Serialize ``obj`` as a JSON formatted stream to ``fp`` (a</span><br /> +<a class="lnum" href="#116" name="116">0116</a><span class="string">    ``.write()``-supporting file-like object).</span><br /> +<a class="lnum" href="#117" name="117">0117</a><span class="string"></span><br /> +<a class="lnum" href="#118" name="118">0118</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> +<a class="lnum" href="#119" name="119">0119</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> +<a class="lnum" href="#120" name="120">0120</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> +<a class="lnum" href="#121" name="121">0121</a><span class="string"></span><br /> +<a class="lnum" href="#122" name="122">0122</a><span class="string">    If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``</span><br /> +<a class="lnum" href="#123" name="123">0123</a><span class="string">    may be ``unicode`` instances, subject to normal Python ``str`` to</span><br /> +<a class="lnum" href="#124" name="124">0124</a><span class="string">    ``unicode`` coercion rules. Unless ``fp.write()`` explicitly</span><br /> +<a class="lnum" href="#125" name="125">0125</a><span class="string">    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely</span><br /> +<a class="lnum" href="#126" name="126">0126</a><span class="string">    to cause an error.</span><br /> +<a class="lnum" href="#127" name="127">0127</a><span class="string"></span><br /> +<a class="lnum" href="#128" name="128">0128</a><span class="string">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> +<a class="lnum" href="#129" name="129">0129</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> +<a class="lnum" href="#130" name="130">0130</a><span class="string">    result in an ``OverflowError`` (or worse).</span><br /> +<a class="lnum" href="#131" name="131">0131</a><span class="string"></span><br /> +<a class="lnum" href="#132" name="132">0132</a><span class="string">    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> +<a class="lnum" href="#133" name="133">0133</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)</span><br /> +<a class="lnum" href="#134" name="134">0134</a><span class="string">    in strict compliance of the JSON specification, instead of using the</span><br /> +<a class="lnum" href="#135" name="135">0135</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> +<a class="lnum" href="#136" name="136">0136</a><span class="string"></span><br /> +<a class="lnum" href="#137" name="137">0137</a><span class="string">    If ``indent`` is a non-negative integer, then JSON array elements and object</span><br /> +<a class="lnum" href="#138" name="138">0138</a><span class="string">    members will be pretty-printed with that indent level. An indent level</span><br /> +<a class="lnum" href="#139" name="139">0139</a><span class="string">    of 0 will only insert newlines. ``None`` is the most compact representation.</span><br /> +<a class="lnum" href="#140" name="140">0140</a><span class="string"></span><br /> +<a class="lnum" href="#141" name="141">0141</a><span class="string">    If ``separators`` is an ``(item_separator, dict_separator)`` tuple</span><br /> +<a class="lnum" href="#142" name="142">0142</a><span class="string">    then it will be used instead of the default ``(', ', ': ')`` separators.</span><br /> +<a class="lnum" href="#143" name="143">0143</a><span class="string">    ``(',', ':')`` is the most compact JSON representation.</span><br /> +<a class="lnum" href="#144" name="144">0144</a><span class="string"></span><br /> +<a class="lnum" href="#145" name="145">0145</a><span class="string">    ``encoding`` is the character encoding for str instances, default is UTF-8.</span><br /> +<a class="lnum" href="#146" name="146">0146</a><span class="string"></span><br /> +<a class="lnum" href="#147" name="147">0147</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> +<a class="lnum" href="#148" name="148">0148</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> +<a class="lnum" href="#149" name="149">0149</a><span class="string">    the ``cls`` kwarg.</span><br /> +<a class="lnum" href="#150" name="150">0150</a><span class="string">    """</span><br /> +<a class="lnum" href="#151" name="151">0151</a>    <span class="comment"># cached encoder</span><br /> +<a class="lnum" href="#152" name="152">0152</a><span class="comment"></span>    <span class="keyword">if</span> <span class="op">(</span><span class="name">skipkeys</span> <span class="keyword">is</span> <span class="name">False</span> <span class="keyword">and</span> <span class="name">ensure_ascii</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#153" name="153">0153</a>        <span class="name">check_circular</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span> <span class="name">allow_nan</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#154" name="154">0154</a>        <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">indent</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">separators</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#155" name="155">0155</a>        <span class="name">encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#156" name="156">0156</a>        <span class="name">iterable</span> <span class="op">=</span> <span class="name">_default_encoder</span><span class="op">.</span><span class="name">iterencode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> +<a class="lnum" href="#157" name="157">0157</a>    <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#158" name="158">0158</a>        <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#159" name="159">0159</a>            <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#160" name="160">0160</a>        <span class="name">iterable</span> <span class="op">=</span> <span class="name">cls</span><span class="op">(</span><span class="name">skipkeys</span><span class="op">=</span><span class="name">skipkeys</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">ensure_ascii</span><span class="op">,</span><br /> +<a class="lnum" href="#161" name="161">0161</a>            <span class="name">check_circular</span><span class="op">=</span><span class="name">check_circular</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">allow_nan</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">indent</span><span class="op">,</span><br /> +<a class="lnum" href="#162" name="162">0162</a>            <span class="name">separators</span><span class="op">=</span><span class="name">separators</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">iterencode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> +<a class="lnum" href="#163" name="163">0163</a>    <span class="comment"># could accelerate with writelines in some versions of Python, at</span><br /> +<a class="lnum" href="#164" name="164">0164</a><span class="comment"></span>    <span class="comment"># a debuggability cost</span><br /> +<a class="lnum" href="#165" name="165">0165</a><span class="comment"></span>    <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">iterable</span><span class="op">:</span><br /> +<a class="lnum" href="#166" name="166">0166</a>        <span class="name">fp</span><span class="op">.</span><span class="name">write</span><span class="op">(</span><span class="name">chunk</span><span class="op">)</span><br /> +<a class="lnum" href="#167" name="167">0167</a><br /> +<a class="lnum" href="#168" name="168">0168</a><br /> +<a class="lnum" href="#169" name="169">0169</a><span class="keyword">def</span> <span class="name">dumps</span><span class="op">(</span><span class="name">obj</span><span class="op">,</span> <span class="name">skipkeys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#170" name="170">0170</a>        <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">separators</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#171" name="171">0171</a>        <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#172" name="172">0172</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#173" name="173">0173</a><span class="string">    Serialize ``obj`` to a JSON formatted ``str``.</span><br /> +<a class="lnum" href="#174" name="174">0174</a><span class="string"></span><br /> +<a class="lnum" href="#175" name="175">0175</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> +<a class="lnum" href="#176" name="176">0176</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> +<a class="lnum" href="#177" name="177">0177</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> +<a class="lnum" href="#178" name="178">0178</a><span class="string"></span><br /> +<a class="lnum" href="#179" name="179">0179</a><span class="string">    If ``ensure_ascii`` is ``False``, then the return value will be a</span><br /> +<a class="lnum" href="#180" name="180">0180</a><span class="string">    ``unicode`` instance subject to normal Python ``str`` to ``unicode``</span><br /> +<a class="lnum" href="#181" name="181">0181</a><span class="string">    coercion rules instead of being escaped to an ASCII ``str``.</span><br /> +<a class="lnum" href="#182" name="182">0182</a><span class="string"></span><br /> +<a class="lnum" href="#183" name="183">0183</a><span class="string">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> +<a class="lnum" href="#184" name="184">0184</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> +<a class="lnum" href="#185" name="185">0185</a><span class="string">    result in an ``OverflowError`` (or worse).</span><br /> +<a class="lnum" href="#186" name="186">0186</a><span class="string"></span><br /> +<a class="lnum" href="#187" name="187">0187</a><span class="string">    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> +<a class="lnum" href="#188" name="188">0188</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in</span><br /> +<a class="lnum" href="#189" name="189">0189</a><span class="string">    strict compliance of the JSON specification, instead of using the</span><br /> +<a class="lnum" href="#190" name="190">0190</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> +<a class="lnum" href="#191" name="191">0191</a><span class="string"></span><br /> +<a class="lnum" href="#192" name="192">0192</a><span class="string">    If ``indent`` is a non-negative integer, then JSON array elements and</span><br /> +<a class="lnum" href="#193" name="193">0193</a><span class="string">    object members will be pretty-printed with that indent level. An indent</span><br /> +<a class="lnum" href="#194" name="194">0194</a><span class="string">    level of 0 will only insert newlines. ``None`` is the most compact</span><br /> +<a class="lnum" href="#195" name="195">0195</a><span class="string">    representation.</span><br /> +<a class="lnum" href="#196" name="196">0196</a><span class="string"></span><br /> +<a class="lnum" href="#197" name="197">0197</a><span class="string">    If ``separators`` is an ``(item_separator, dict_separator)`` tuple</span><br /> +<a class="lnum" href="#198" name="198">0198</a><span class="string">    then it will be used instead of the default ``(', ', ': ')`` separators.</span><br /> +<a class="lnum" href="#199" name="199">0199</a><span class="string">    ``(',', ':')`` is the most compact JSON representation.</span><br /> +<a class="lnum" href="#200" name="200">0200</a><span class="string"></span><br /> +<a class="lnum" href="#201" name="201">0201</a><span class="string">    ``encoding`` is the character encoding for str instances, default is UTF-8.</span><br /> +<a class="lnum" href="#202" name="202">0202</a><span class="string"></span><br /> +<a class="lnum" href="#203" name="203">0203</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> +<a class="lnum" href="#204" name="204">0204</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> +<a class="lnum" href="#205" name="205">0205</a><span class="string">    the ``cls`` kwarg.</span><br /> +<a class="lnum" href="#206" name="206">0206</a><span class="string">    """</span><br /> +<a class="lnum" href="#207" name="207">0207</a>    <span class="comment"># cached encoder</span><br /> +<a class="lnum" href="#208" name="208">0208</a><span class="comment"></span>    <span class="keyword">if</span> <span class="op">(</span><span class="name">skipkeys</span> <span class="keyword">is</span> <span class="name">False</span> <span class="keyword">and</span> <span class="name">ensure_ascii</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#209" name="209">0209</a>        <span class="name">check_circular</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span> <span class="name">allow_nan</span> <span class="keyword">is</span> <span class="name">True</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#210" name="210">0210</a>        <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">indent</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">separators</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#211" name="211">0211</a>        <span class="name">encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#212" name="212">0212</a>        <span class="keyword">return</span> <span class="name">_default_encoder</span><span class="op">.</span><span class="name">encode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> +<a class="lnum" href="#213" name="213">0213</a>    <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#214" name="214">0214</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#215" name="215">0215</a>    <span class="keyword">return</span> <span class="name">cls</span><span class="op">(</span><br /> +<a class="lnum" href="#216" name="216">0216</a>        <span class="name">skipkeys</span><span class="op">=</span><span class="name">skipkeys</span><span class="op">,</span> <span class="name">ensure_ascii</span><span class="op">=</span><span class="name">ensure_ascii</span><span class="op">,</span><br /> +<a class="lnum" href="#217" name="217">0217</a>        <span class="name">check_circular</span><span class="op">=</span><span class="name">check_circular</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">allow_nan</span><span class="op">,</span> <span class="name">indent</span><span class="op">=</span><span class="name">indent</span><span class="op">,</span><br /> +<a class="lnum" href="#218" name="218">0218</a>        <span class="name">separators</span><span class="op">=</span><span class="name">separators</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span><br /> +<a class="lnum" href="#219" name="219">0219</a>        <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">encode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> +<a class="lnum" href="#220" name="220">0220</a><br /> +<a class="lnum" href="#221" name="221">0221</a><span class="name">_default_decoder</span> <span class="op">=</span> <span class="name">JSONDecoder</span><span class="op">(</span><span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><br /> +<a class="lnum" href="#222" name="222">0222</a><br /> +<a class="lnum" href="#223" name="223">0223</a><span class="keyword">def</span> <span class="name">load</span><span class="op">(</span><span class="name">fp</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_float</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#224" name="224">0224</a>        <span class="name">parse_int</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_constant</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#225" name="225">0225</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#226" name="226">0226</a><span class="string">    Deserialize ``fp`` (a ``.read()``-supporting file-like object containing</span><br /> +<a class="lnum" href="#227" name="227">0227</a><span class="string">    a JSON document) to a Python object.</span><br /> +<a class="lnum" href="#228" name="228">0228</a><span class="string"></span><br /> +<a class="lnum" href="#229" name="229">0229</a><span class="string">    If the contents of ``fp`` is encoded with an ASCII based encoding other</span><br /> +<a class="lnum" href="#230" name="230">0230</a><span class="string">    than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must</span><br /> +<a class="lnum" href="#231" name="231">0231</a><span class="string">    be specified. Encodings that are not ASCII based (such as UCS-2) are</span><br /> +<a class="lnum" href="#232" name="232">0232</a><span class="string">    not allowed, and should be wrapped with</span><br /> +<a class="lnum" href="#233" name="233">0233</a><span class="string">    ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``</span><br /> +<a class="lnum" href="#234" name="234">0234</a><span class="string">    object and passed to ``loads()``</span><br /> +<a class="lnum" href="#235" name="235">0235</a><span class="string"></span><br /> +<a class="lnum" href="#236" name="236">0236</a><span class="string">    ``object_hook`` is an optional function that will be called with the</span><br /> +<a class="lnum" href="#237" name="237">0237</a><span class="string">    result of any object literal decode (a ``dict``). The return value of</span><br /> +<a class="lnum" href="#238" name="238">0238</a><span class="string">    ``object_hook`` will be used instead of the ``dict``. This feature</span><br /> +<a class="lnum" href="#239" name="239">0239</a><span class="string">    can be used to implement custom decoders (e.g. JSON-RPC class hinting).</span><br /> +<a class="lnum" href="#240" name="240">0240</a><span class="string">    </span><br /> +<a class="lnum" href="#241" name="241">0241</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> +<a class="lnum" href="#242" name="242">0242</a><span class="string">    kwarg.</span><br /> +<a class="lnum" href="#243" name="243">0243</a><span class="string">    """</span><br /> +<a class="lnum" href="#244" name="244">0244</a>    <span class="keyword">return</span> <span class="name">loads</span><span class="op">(</span><span class="name">fp</span><span class="op">.</span><span class="name">read</span><span class="op">(</span><span class="op">)</span><span class="op">,</span><br /> +<a class="lnum" href="#245" name="245">0245</a>        <span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">cls</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">object_hook</span><span class="op">,</span><br /> +<a class="lnum" href="#246" name="246">0246</a>        <span class="name">parse_float</span><span class="op">=</span><span class="name">parse_float</span><span class="op">,</span> <span class="name">parse_int</span><span class="op">=</span><span class="name">parse_int</span><span class="op">,</span><br /> +<a class="lnum" href="#247" name="247">0247</a>        <span class="name">parse_constant</span><span class="op">=</span><span class="name">parse_constant</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><br /> +<a class="lnum" href="#248" name="248">0248</a><br /> +<a class="lnum" href="#249" name="249">0249</a><span class="keyword">def</span> <span class="name">loads</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">cls</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_float</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#250" name="250">0250</a>        <span class="name">parse_int</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_constant</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#251" name="251">0251</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#252" name="252">0252</a><span class="string">    Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON</span><br /> +<a class="lnum" href="#253" name="253">0253</a><span class="string">    document) to a Python object.</span><br /> +<a class="lnum" href="#254" name="254">0254</a><span class="string"></span><br /> +<a class="lnum" href="#255" name="255">0255</a><span class="string">    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding</span><br /> +<a class="lnum" href="#256" name="256">0256</a><span class="string">    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name</span><br /> +<a class="lnum" href="#257" name="257">0257</a><span class="string">    must be specified. Encodings that are not ASCII based (such as UCS-2)</span><br /> +<a class="lnum" href="#258" name="258">0258</a><span class="string">    are not allowed and should be decoded to ``unicode`` first.</span><br /> +<a class="lnum" href="#259" name="259">0259</a><span class="string"></span><br /> +<a class="lnum" href="#260" name="260">0260</a><span class="string">    ``object_hook`` is an optional function that will be called with the</span><br /> +<a class="lnum" href="#261" name="261">0261</a><span class="string">    result of any object literal decode (a ``dict``). The return value of</span><br /> +<a class="lnum" href="#262" name="262">0262</a><span class="string">    ``object_hook`` will be used instead of the ``dict``. This feature</span><br /> +<a class="lnum" href="#263" name="263">0263</a><span class="string">    can be used to implement custom decoders (e.g. JSON-RPC class hinting).</span><br /> +<a class="lnum" href="#264" name="264">0264</a><span class="string"></span><br /> +<a class="lnum" href="#265" name="265">0265</a><span class="string">    ``parse_float``, if specified, will be called with the string</span><br /> +<a class="lnum" href="#266" name="266">0266</a><span class="string">    of every JSON float to be decoded. By default this is equivalent to</span><br /> +<a class="lnum" href="#267" name="267">0267</a><span class="string">    float(num_str). This can be used to use another datatype or parser</span><br /> +<a class="lnum" href="#268" name="268">0268</a><span class="string">    for JSON floats (e.g. decimal.Decimal).</span><br /> +<a class="lnum" href="#269" name="269">0269</a><span class="string"></span><br /> +<a class="lnum" href="#270" name="270">0270</a><span class="string">    ``parse_int``, if specified, will be called with the string</span><br /> +<a class="lnum" href="#271" name="271">0271</a><span class="string">    of every JSON int to be decoded. By default this is equivalent to</span><br /> +<a class="lnum" href="#272" name="272">0272</a><span class="string">    int(num_str). This can be used to use another datatype or parser</span><br /> +<a class="lnum" href="#273" name="273">0273</a><span class="string">    for JSON integers (e.g. float).</span><br /> +<a class="lnum" href="#274" name="274">0274</a><span class="string"></span><br /> +<a class="lnum" href="#275" name="275">0275</a><span class="string">    ``parse_constant``, if specified, will be called with one of the</span><br /> +<a class="lnum" href="#276" name="276">0276</a><span class="string">    following strings: -Infinity, Infinity, NaN, null, true, false.</span><br /> +<a class="lnum" href="#277" name="277">0277</a><span class="string">    This can be used to raise an exception if invalid JSON numbers</span><br /> +<a class="lnum" href="#278" name="278">0278</a><span class="string">    are encountered.</span><br /> +<a class="lnum" href="#279" name="279">0279</a><span class="string"></span><br /> +<a class="lnum" href="#280" name="280">0280</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> +<a class="lnum" href="#281" name="281">0281</a><span class="string">    kwarg.</span><br /> +<a class="lnum" href="#282" name="282">0282</a><span class="string">    """</span><br /> +<a class="lnum" href="#283" name="283">0283</a>    <span class="keyword">if</span> <span class="op">(</span><span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">encoding</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#284" name="284">0284</a>            <span class="name">parse_int</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">parse_float</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span><br /> +<a class="lnum" href="#285" name="285">0285</a>            <span class="name">parse_constant</span> <span class="keyword">is</span> <span class="name">None</span> <span class="keyword">and</span> <span class="keyword">not</span> <span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#286" name="286">0286</a>        <span class="keyword">return</span> <span class="name">_default_decoder</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#287" name="287">0287</a>    <span class="keyword">if</span> <span class="name">cls</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#288" name="288">0288</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONDecoder</span><br /> +<a class="lnum" href="#289" name="289">0289</a>    <span class="keyword">if</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#290" name="290">0290</a>        <span class="name">kw</span><span class="op">[</span><span class="string">'object_hook'</span><span class="op">]</span> <span class="op">=</span> <span class="name">object_hook</span><br /> +<a class="lnum" href="#291" name="291">0291</a>    <span class="keyword">if</span> <span class="name">parse_float</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#292" name="292">0292</a>        <span class="name">kw</span><span class="op">[</span><span class="string">'parse_float'</span><span class="op">]</span> <span class="op">=</span> <span class="name">parse_float</span><br /> +<a class="lnum" href="#293" name="293">0293</a>    <span class="keyword">if</span> <span class="name">parse_int</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#294" name="294">0294</a>        <span class="name">kw</span><span class="op">[</span><span class="string">'parse_int'</span><span class="op">]</span> <span class="op">=</span> <span class="name">parse_int</span><br /> +<a class="lnum" href="#295" name="295">0295</a>    <span class="keyword">if</span> <span class="name">parse_constant</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#296" name="296">0296</a>        <span class="name">kw</span><span class="op">[</span><span class="string">'parse_constant'</span><span class="op">]</span> <span class="op">=</span> <span class="name">parse_constant</span><br /> +<a class="lnum" href="#297" name="297">0297</a>    <span class="keyword">return</span> <span class="name">cls</span><span class="op">(</span><span class="name">encoding</span><span class="op">=</span><span class="name">encoding</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#298" name="298">0298</a><br /> +<a class="lnum" href="#299" name="299">0299</a><span class="comment">#</span><br /> +<a class="lnum" href="#300" name="300">0300</a><span class="comment"></span><span class="comment"># Compatibility cruft from other libraries</span><br /> +<a class="lnum" href="#301" name="301">0301</a><span class="comment"></span><span class="comment">#</span><br /> +<a class="lnum" href="#302" name="302">0302</a><span class="comment"></span><br /> +<a class="lnum" href="#303" name="303">0303</a><span class="keyword">def</span> <span class="name">decode</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#304" name="304">0304</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#305" name="305">0305</a><span class="string">    demjson, python-cjson API compatibility hook. Use loads(s) instead.</span><br /> +<a class="lnum" href="#306" name="306">0306</a><span class="string">    """</span><br /> +<a class="lnum" href="#307" name="307">0307</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#308" name="308">0308</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.loads(s) should be used instead of decode(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#309" name="309">0309</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#310" name="310">0310</a>    <span class="keyword">return</span> <span class="name">loads</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#311" name="311">0311</a><br /> +<a class="lnum" href="#312" name="312">0312</a><span class="keyword">def</span> <span class="name">encode</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#313" name="313">0313</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#314" name="314">0314</a><span class="string">    demjson, python-cjson compatibility hook. Use dumps(s) instead.</span><br /> +<a class="lnum" href="#315" name="315">0315</a><span class="string">    """</span><br /> +<a class="lnum" href="#316" name="316">0316</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#317" name="317">0317</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.dumps(s) should be used instead of encode(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#318" name="318">0318</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#319" name="319">0319</a>    <span class="keyword">return</span> <span class="name">dumps</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><br /> +<a class="lnum" href="#320" name="320">0320</a><br /> +<a class="lnum" href="#321" name="321">0321</a><span class="keyword">def</span> <span class="name">read</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#322" name="322">0322</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#323" name="323">0323</a><span class="string">    jsonlib, JsonUtils, python-json, json-py API compatibility hook.</span><br /> +<a class="lnum" href="#324" name="324">0324</a><span class="string">    Use loads(s) instead.</span><br /> +<a class="lnum" href="#325" name="325">0325</a><span class="string">    """</span><br /> +<a class="lnum" href="#326" name="326">0326</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#327" name="327">0327</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.loads(s) should be used instead of read(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#328" name="328">0328</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#329" name="329">0329</a>    <span class="keyword">return</span> <span class="name">loads</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#330" name="330">0330</a><br /> +<a class="lnum" href="#331" name="331">0331</a><span class="keyword">def</span> <span class="name">write</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#332" name="332">0332</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#333" name="333">0333</a><span class="string">    jsonlib, JsonUtils, python-json, json-py API compatibility hook.</span><br /> +<a class="lnum" href="#334" name="334">0334</a><span class="string">    Use dumps(s) instead.</span><br /> +<a class="lnum" href="#335" name="335">0335</a><span class="string">    """</span><br /> +<a class="lnum" href="#336" name="336">0336</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#337" name="337">0337</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simplejson.dumps(s) should be used instead of write(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#338" name="338">0338</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#339" name="339">0339</a>    <span class="keyword">return</span> <span class="name">dumps</span><span class="op">(</span><span class="name">obj</span><span class="op">)</span></code></div></body></html>
\ No newline at end of file diff --git a/docs/simplejson/decoder.py.html b/docs/simplejson/decoder.py.html index d252e69..6edffb7 100644 --- a/docs/simplejson/decoder.py.html +++ b/docs/simplejson/decoder.py.html @@ -116,230 +116,257 @@ function show_line_range() { <a class="lnum" href="#44" name="44">0044</a><span class="op">}</span><br /> <a class="lnum" href="#45" name="45">0045</a><br /> <a class="lnum" href="#46" name="46">0046</a><span class="keyword">def</span> <span class="name">JSONConstant</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">,</span> <span class="name">c</span><span class="op">=</span><span class="name">_CONSTANTS</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#47" name="47">0047</a>    <span class="keyword">return</span> <span class="name">c</span><span class="op">[</span><span class="name">match</span><span class="op">.</span><span class="name">group</span><span class="op">(</span><span class="number">0</span><span class="op">)</span><span class="op">]</span><span class="op">,</span> <span class="name">None</span><br /> -<a class="lnum" href="#48" name="48">0048</a><span class="name">pattern</span><span class="op">(</span><span class="string">'(-?Infinity|NaN|true|false|null)'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONConstant</span><span class="op">)</span><br /> -<a class="lnum" href="#49" name="49">0049</a><br /> -<a class="lnum" href="#50" name="50">0050</a><span class="keyword">def</span> <span class="name">JSONNumber</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#51" name="51">0051</a>    <span class="name">match</span> <span class="op">=</span> <span class="name">JSONNumber</span><span class="op">.</span><span class="name">regex</span><span class="op">.</span><span class="name">match</span><span class="op">(</span><span class="name">match</span><span class="op">.</span><span class="name">string</span><span class="op">,</span> <span class="op">*</span><span class="name">match</span><span class="op">.</span><span class="name">span</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#52" name="52">0052</a>    <span class="name">integer</span><span class="op">,</span> <span class="name">frac</span><span class="op">,</span> <span class="name">exp</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">groups</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#53" name="53">0053</a>    <span class="keyword">if</span> <span class="name">frac</span> <span class="keyword">or</span> <span class="name">exp</span><span class="op">:</span><br /> -<a class="lnum" href="#54" name="54">0054</a>        <span class="name">res</span> <span class="op">=</span> <span class="name">float</span><span class="op">(</span><span class="name">integer</span> <span class="op">+</span> <span class="op">(</span><span class="name">frac</span> <span class="keyword">or</span> <span class="string">''</span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span><span class="name">exp</span> <span class="keyword">or</span> <span class="string">''</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#55" name="55">0055</a>    <span class="keyword">else</span><span class="op">:</span><br /> -<a class="lnum" href="#56" name="56">0056</a>        <span class="name">res</span> <span class="op">=</span> <span class="name">int</span><span class="op">(</span><span class="name">integer</span><span class="op">)</span><br /> -<a class="lnum" href="#57" name="57">0057</a>    <span class="keyword">return</span> <span class="name">res</span><span class="op">,</span> <span class="name">None</span><br /> -<a class="lnum" href="#58" name="58">0058</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONNumber</span><span class="op">)</span><br /> -<a class="lnum" href="#59" name="59">0059</a><br /> -<a class="lnum" href="#60" name="60">0060</a><span class="name">STRINGCHUNK</span> <span class="op">=</span> <span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="string">r'(.*?)(["\\])'</span><span class="op">,</span> <span class="name">FLAGS</span><span class="op">)</span><br /> -<a class="lnum" href="#61" name="61">0061</a><span class="name">BACKSLASH</span> <span class="op">=</span> <span class="op">{</span><br /> -<a class="lnum" href="#62" name="62">0062</a>    <span class="string">'"'</span><span class="op">:</span> <span class="string">u'"'</span><span class="op">,</span> <span class="string">'\\'</span><span class="op">:</span> <span class="string">u'\\'</span><span class="op">,</span> <span class="string">'/'</span><span class="op">:</span> <span class="string">u'/'</span><span class="op">,</span><br /> -<a class="lnum" href="#63" name="63">0063</a>    <span class="string">'b'</span><span class="op">:</span> <span class="string">u'\b'</span><span class="op">,</span> <span class="string">'f'</span><span class="op">:</span> <span class="string">u'\f'</span><span class="op">,</span> <span class="string">'n'</span><span class="op">:</span> <span class="string">u'\n'</span><span class="op">,</span> <span class="string">'r'</span><span class="op">:</span> <span class="string">u'\r'</span><span class="op">,</span> <span class="string">'t'</span><span class="op">:</span> <span class="string">u'\t'</span><span class="op">,</span><br /> -<a class="lnum" href="#64" name="64">0064</a><span class="op">}</span><br /> -<a class="lnum" href="#65" name="65">0065</a><br /> -<a class="lnum" href="#66" name="66">0066</a><span class="name">DEFAULT_ENCODING</span> <span class="op">=</span> <span class="string">"utf-8"</span><br /> +<a class="lnum" href="#47" name="47">0047</a>    <span class="name">s</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">group</span><span class="op">(</span><span class="number">0</span><span class="op">)</span><br /> +<a class="lnum" href="#48" name="48">0048</a>    <span class="name">fn</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'parse_constant'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> +<a class="lnum" href="#49" name="49">0049</a>    <span class="keyword">if</span> <span class="name">fn</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#50" name="50">0050</a>        <span class="name">rval</span> <span class="op">=</span> <span class="name">c</span><span class="op">[</span><span class="name">s</span><span class="op">]</span><br /> +<a class="lnum" href="#51" name="51">0051</a>    <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#52" name="52">0052</a>        <span class="name">rval</span> <span class="op">=</span> <span class="name">fn</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#53" name="53">0053</a>    <span class="keyword">return</span> <span class="name">rval</span><span class="op">,</span> <span class="name">None</span><br /> +<a class="lnum" href="#54" name="54">0054</a><span class="name">pattern</span><span class="op">(</span><span class="string">'(-?Infinity|NaN|true|false|null)'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONConstant</span><span class="op">)</span><br /> +<a class="lnum" href="#55" name="55">0055</a><br /> +<a class="lnum" href="#56" name="56">0056</a><span class="keyword">def</span> <span class="name">JSONNumber</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#57" name="57">0057</a>    <span class="name">match</span> <span class="op">=</span> <span class="name">JSONNumber</span><span class="op">.</span><span class="name">regex</span><span class="op">.</span><span class="name">match</span><span class="op">(</span><span class="name">match</span><span class="op">.</span><span class="name">string</span><span class="op">,</span> <span class="op">*</span><span class="name">match</span><span class="op">.</span><span class="name">span</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#58" name="58">0058</a>    <span class="name">integer</span><span class="op">,</span> <span class="name">frac</span><span class="op">,</span> <span class="name">exp</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">groups</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#59" name="59">0059</a>    <span class="keyword">if</span> <span class="name">frac</span> <span class="keyword">or</span> <span class="name">exp</span><span class="op">:</span><br /> +<a class="lnum" href="#60" name="60">0060</a>        <span class="name">fn</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'parse_float'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span> <span class="keyword">or</span> <span class="name">float</span><br /> +<a class="lnum" href="#61" name="61">0061</a>        <span class="name">res</span> <span class="op">=</span> <span class="name">fn</span><span class="op">(</span><span class="name">integer</span> <span class="op">+</span> <span class="op">(</span><span class="name">frac</span> <span class="keyword">or</span> <span class="string">''</span><span class="op">)</span> <span class="op">+</span> <span class="op">(</span><span class="name">exp</span> <span class="keyword">or</span> <span class="string">''</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#62" name="62">0062</a>    <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#63" name="63">0063</a>        <span class="name">fn</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'parse_int'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span> <span class="keyword">or</span> <span class="name">int</span><br /> +<a class="lnum" href="#64" name="64">0064</a>        <span class="name">res</span> <span class="op">=</span> <span class="name">fn</span><span class="op">(</span><span class="name">integer</span><span class="op">)</span><br /> +<a class="lnum" href="#65" name="65">0065</a>    <span class="keyword">return</span> <span class="name">res</span><span class="op">,</span> <span class="name">None</span><br /> +<a class="lnum" href="#66" name="66">0066</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONNumber</span><span class="op">)</span><br /> <a class="lnum" href="#67" name="67">0067</a><br /> -<a class="lnum" href="#68" name="68">0068</a><span class="keyword">def</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">_b</span><span class="op">=</span><span class="name">BACKSLASH</span><span class="op">,</span> <span class="name">_m</span><span class="op">=</span><span class="name">STRINGCHUNK</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#69" name="69">0069</a>    <span class="keyword">if</span> <span class="name">encoding</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#70" name="70">0070</a>        <span class="name">encoding</span> <span class="op">=</span> <span class="name">DEFAULT_ENCODING</span><br /> -<a class="lnum" href="#71" name="71">0071</a>    <span class="name">chunks</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><br /> -<a class="lnum" href="#72" name="72">0072</a>    <span class="name">_append</span> <span class="op">=</span> <span class="name">chunks</span><span class="op">.</span><span class="name">append</span><br /> -<a class="lnum" href="#73" name="73">0073</a>    <span class="name">begin</span> <span class="op">=</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><br /> -<a class="lnum" href="#74" name="74">0074</a>    <span class="keyword">while</span> <span class="number">1</span><span class="op">:</span><br /> -<a class="lnum" href="#75" name="75">0075</a>        <span class="name">chunk</span> <span class="op">=</span> <span class="name">_m</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><br /> -<a class="lnum" href="#76" name="76">0076</a>        <span class="keyword">if</span> <span class="name">chunk</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#77" name="77">0077</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> -<a class="lnum" href="#78" name="78">0078</a>                <span class="name">errmsg</span><span class="op">(</span><span class="string">"Unterminated string starting at"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">begin</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#79" name="79">0079</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">chunk</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#80" name="80">0080</a>        <span class="name">content</span><span class="op">,</span> <span class="name">terminator</span> <span class="op">=</span> <span class="name">chunk</span><span class="op">.</span><span class="name">groups</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#81" name="81">0081</a>        <span class="keyword">if</span> <span class="name">content</span><span class="op">:</span><br /> -<a class="lnum" href="#82" name="82">0082</a>            <span class="keyword">if</span> <span class="keyword">not</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">content</span><span class="op">,</span> <span class="name">unicode</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#83" name="83">0083</a>                <span class="name">content</span> <span class="op">=</span> <span class="name">unicode</span><span class="op">(</span><span class="name">content</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> -<a class="lnum" href="#84" name="84">0084</a>            <span class="name">_append</span><span class="op">(</span><span class="name">content</span><span class="op">)</span><br /> -<a class="lnum" href="#85" name="85">0085</a>        <span class="keyword">if</span> <span class="name">terminator</span> <span class="op">==</span> <span class="string">'"'</span><span class="op">:</span><br /> -<a class="lnum" href="#86" name="86">0086</a>            <span class="keyword">break</span><br /> -<a class="lnum" href="#87" name="87">0087</a>        <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#88" name="88">0088</a>            <span class="name">esc</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">]</span><br /> -<a class="lnum" href="#89" name="89">0089</a>        <span class="keyword">except</span> <span class="name">IndexError</span><span class="op">:</span><br /> -<a class="lnum" href="#90" name="90">0090</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> -<a class="lnum" href="#91" name="91">0091</a>                <span class="name">errmsg</span><span class="op">(</span><span class="string">"Unterminated string starting at"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">begin</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#92" name="92">0092</a>        <span class="keyword">if</span> <span class="name">esc</span> <span class="op">!=</span> <span class="string">'u'</span><span class="op">:</span><br /> -<a class="lnum" href="#93" name="93">0093</a>            <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#94" name="94">0094</a>                <span class="name">m</span> <span class="op">=</span> <span class="name">_b</span><span class="op">[</span><span class="name">esc</span><span class="op">]</span><br /> -<a class="lnum" href="#95" name="95">0095</a>            <span class="keyword">except</span> <span class="name">KeyError</span><span class="op">:</span><br /> -<a class="lnum" href="#96" name="96">0096</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> -<a class="lnum" href="#97" name="97">0097</a>                    <span class="name">errmsg</span><span class="op">(</span><span class="string">"Invalid \\escape: %r"</span> <span class="op">%</span> <span class="op">(</span><span class="name">esc</span><span class="op">,</span><span class="op">)</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#98" name="98">0098</a>            <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> -<a class="lnum" href="#99" name="99">0099</a>        <span class="keyword">else</span><span class="op">:</span><br /> -<a class="lnum" href="#100" name="100">0100</a>            <span class="name">esc</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">5</span><span class="op">]</span><br /> +<a class="lnum" href="#68" name="68">0068</a><span class="name">STRINGCHUNK</span> <span class="op">=</span> <span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="string">r'(.*?)(["\\])'</span><span class="op">,</span> <span class="name">FLAGS</span><span class="op">)</span><br /> +<a class="lnum" href="#69" name="69">0069</a><span class="name">BACKSLASH</span> <span class="op">=</span> <span class="op">{</span><br /> +<a class="lnum" href="#70" name="70">0070</a>    <span class="string">'"'</span><span class="op">:</span> <span class="string">u'"'</span><span class="op">,</span> <span class="string">'\\'</span><span class="op">:</span> <span class="string">u'\\'</span><span class="op">,</span> <span class="string">'/'</span><span class="op">:</span> <span class="string">u'/'</span><span class="op">,</span><br /> +<a class="lnum" href="#71" name="71">0071</a>    <span class="string">'b'</span><span class="op">:</span> <span class="string">u'\b'</span><span class="op">,</span> <span class="string">'f'</span><span class="op">:</span> <span class="string">u'\f'</span><span class="op">,</span> <span class="string">'n'</span><span class="op">:</span> <span class="string">u'\n'</span><span class="op">,</span> <span class="string">'r'</span><span class="op">:</span> <span class="string">u'\r'</span><span class="op">,</span> <span class="string">'t'</span><span class="op">:</span> <span class="string">u'\t'</span><span class="op">,</span><br /> +<a class="lnum" href="#72" name="72">0072</a><span class="op">}</span><br /> +<a class="lnum" href="#73" name="73">0073</a><br /> +<a class="lnum" href="#74" name="74">0074</a><span class="name">DEFAULT_ENCODING</span> <span class="op">=</span> <span class="string">"utf-8"</span><br /> +<a class="lnum" href="#75" name="75">0075</a><br /> +<a class="lnum" href="#76" name="76">0076</a><span class="keyword">def</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">_b</span><span class="op">=</span><span class="name">BACKSLASH</span><span class="op">,</span> <span class="name">_m</span><span class="op">=</span><span class="name">STRINGCHUNK</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#77" name="77">0077</a>    <span class="keyword">if</span> <span class="name">encoding</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#78" name="78">0078</a>        <span class="name">encoding</span> <span class="op">=</span> <span class="name">DEFAULT_ENCODING</span><br /> +<a class="lnum" href="#79" name="79">0079</a>    <span class="name">chunks</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><br /> +<a class="lnum" href="#80" name="80">0080</a>    <span class="name">_append</span> <span class="op">=</span> <span class="name">chunks</span><span class="op">.</span><span class="name">append</span><br /> +<a class="lnum" href="#81" name="81">0081</a>    <span class="name">begin</span> <span class="op">=</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><br /> +<a class="lnum" href="#82" name="82">0082</a>    <span class="keyword">while</span> <span class="number">1</span><span class="op">:</span><br /> +<a class="lnum" href="#83" name="83">0083</a>        <span class="name">chunk</span> <span class="op">=</span> <span class="name">_m</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><br /> +<a class="lnum" href="#84" name="84">0084</a>        <span class="keyword">if</span> <span class="name">chunk</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#85" name="85">0085</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> +<a class="lnum" href="#86" name="86">0086</a>                <span class="name">errmsg</span><span class="op">(</span><span class="string">"Unterminated string starting at"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">begin</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#87" name="87">0087</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">chunk</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#88" name="88">0088</a>        <span class="name">content</span><span class="op">,</span> <span class="name">terminator</span> <span class="op">=</span> <span class="name">chunk</span><span class="op">.</span><span class="name">groups</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#89" name="89">0089</a>        <span class="keyword">if</span> <span class="name">content</span><span class="op">:</span><br /> +<a class="lnum" href="#90" name="90">0090</a>            <span class="keyword">if</span> <span class="keyword">not</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">content</span><span class="op">,</span> <span class="name">unicode</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#91" name="91">0091</a>                <span class="name">content</span> <span class="op">=</span> <span class="name">unicode</span><span class="op">(</span><span class="name">content</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#92" name="92">0092</a>            <span class="name">_append</span><span class="op">(</span><span class="name">content</span><span class="op">)</span><br /> +<a class="lnum" href="#93" name="93">0093</a>        <span class="keyword">if</span> <span class="name">terminator</span> <span class="op">==</span> <span class="string">'"'</span><span class="op">:</span><br /> +<a class="lnum" href="#94" name="94">0094</a>            <span class="keyword">break</span><br /> +<a class="lnum" href="#95" name="95">0095</a>        <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#96" name="96">0096</a>            <span class="name">esc</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">]</span><br /> +<a class="lnum" href="#97" name="97">0097</a>        <span class="keyword">except</span> <span class="name">IndexError</span><span class="op">:</span><br /> +<a class="lnum" href="#98" name="98">0098</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> +<a class="lnum" href="#99" name="99">0099</a>                <span class="name">errmsg</span><span class="op">(</span><span class="string">"Unterminated string starting at"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">begin</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#100" name="100">0100</a>        <span class="keyword">if</span> <span class="name">esc</span> <span class="op">!=</span> <span class="string">'u'</span><span class="op">:</span><br /> <a class="lnum" href="#101" name="101">0101</a>            <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#102" name="102">0102</a>                <span class="name">m</span> <span class="op">=</span> <span class="name">unichr</span><span class="op">(</span><span class="name">int</span><span class="op">(</span><span class="name">esc</span><span class="op">,</span> <span class="number">16</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#103" name="103">0103</a>                <span class="keyword">if</span> <span class="name">len</span><span class="op">(</span><span class="name">esc</span><span class="op">)</span> <span class="op">!=</span> <span class="number">4</span> <span class="keyword">or</span> <span class="keyword">not</span> <span class="name">esc</span><span class="op">.</span><span class="name">isalnum</span><span class="op">(</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#104" name="104">0104</a>                    <span class="keyword">raise</span> <span class="name">ValueError</span><br /> -<a class="lnum" href="#105" name="105">0105</a>            <span class="keyword">except</span> <span class="name">ValueError</span><span class="op">:</span><br /> -<a class="lnum" href="#106" name="106">0106</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Invalid \\uXXXX escape"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#107" name="107">0107</a>            <span class="name">end</span> <span class="op">+=</span> <span class="number">5</span><br /> -<a class="lnum" href="#108" name="108">0108</a>        <span class="name">_append</span><span class="op">(</span><span class="name">m</span><span class="op">)</span><br /> -<a class="lnum" href="#109" name="109">0109</a>    <span class="keyword">return</span> <span class="string">u''</span><span class="op">.</span><span class="name">join</span><span class="op">(</span><span class="name">chunks</span><span class="op">)</span><span class="op">,</span> <span class="name">end</span><br /> -<a class="lnum" href="#110" name="110">0110</a><br /> -<a class="lnum" href="#111" name="111">0111</a><span class="keyword">def</span> <span class="name">JSONString</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#112" name="112">0112</a>    <span class="name">encoding</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'encoding'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> -<a class="lnum" href="#113" name="113">0113</a>    <span class="keyword">return</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">match</span><span class="op">.</span><span class="name">string</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> -<a class="lnum" href="#114" name="114">0114</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'"'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONString</span><span class="op">)</span><br /> -<a class="lnum" href="#115" name="115">0115</a><br /> -<a class="lnum" href="#116" name="116">0116</a><span class="name">WHITESPACE</span> <span class="op">=</span> <span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="string">r'\s*'</span><span class="op">,</span> <span class="name">FLAGS</span><span class="op">)</span><br /> -<a class="lnum" href="#117" name="117">0117</a><br /> -<a class="lnum" href="#118" name="118">0118</a><span class="keyword">def</span> <span class="name">JSONObject</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#119" name="119">0119</a>    <span class="name">pairs</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span><br /> -<a class="lnum" href="#120" name="120">0120</a>    <span class="name">s</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">string</span><br /> -<a class="lnum" href="#121" name="121">0121</a>    <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#122" name="122">0122</a>    <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> -<a class="lnum" href="#123" name="123">0123</a>    <span class="comment"># trivial empty object</span><br /> -<a class="lnum" href="#124" name="124">0124</a><span class="comment"></span>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">'}'</span><span class="op">:</span><br /> -<a class="lnum" href="#125" name="125">0125</a>        <span class="keyword">return</span> <span class="name">pairs</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><br /> -<a class="lnum" href="#126" name="126">0126</a>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">'"'</span><span class="op">:</span><br /> -<a class="lnum" href="#127" name="127">0127</a>        <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting property name"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#128" name="128">0128</a>    <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> -<a class="lnum" href="#129" name="129">0129</a>    <span class="name">encoding</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'encoding'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> -<a class="lnum" href="#130" name="130">0130</a>    <span class="name">iterscan</span> <span class="op">=</span> <span class="name">JSONScanner</span><span class="op">.</span><span class="name">iterscan</span><br /> -<a class="lnum" href="#131" name="131">0131</a>    <span class="keyword">while</span> <span class="name">True</span><span class="op">:</span><br /> -<a class="lnum" href="#132" name="132">0132</a>        <span class="name">key</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> -<a class="lnum" href="#133" name="133">0133</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#134" name="134">0134</a>        <span class="keyword">if</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span> <span class="op">!=</span> <span class="string">':'</span><span class="op">:</span><br /> -<a class="lnum" href="#135" name="135">0135</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting : delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#136" name="136">0136</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#137" name="137">0137</a>        <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#138" name="138">0138</a>            <span class="name">value</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">end</span><span class="op">,</span> <span class="name">context</span><span class="op">=</span><span class="name">context</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#139" name="139">0139</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> -<a class="lnum" href="#140" name="140">0140</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting object"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#141" name="141">0141</a>        <span class="name">pairs</span><span class="op">[</span><span class="name">key</span><span class="op">]</span> <span class="op">=</span> <span class="name">value</span><br /> -<a class="lnum" href="#142" name="142">0142</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#143" name="143">0143</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> -<a class="lnum" href="#144" name="144">0144</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> -<a class="lnum" href="#145" name="145">0145</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">'}'</span><span class="op">:</span><br /> -<a class="lnum" href="#146" name="146">0146</a>            <span class="keyword">break</span><br /> -<a class="lnum" href="#147" name="147">0147</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">','</span><span class="op">:</span><br /> -<a class="lnum" href="#148" name="148">0148</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting , delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#149" name="149">0149</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#150" name="150">0150</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> -<a class="lnum" href="#151" name="151">0151</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> -<a class="lnum" href="#152" name="152">0152</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">'"'</span><span class="op">:</span><br /> -<a class="lnum" href="#153" name="153">0153</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting property name"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#154" name="154">0154</a>    <span class="name">object_hook</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'object_hook'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> -<a class="lnum" href="#155" name="155">0155</a>    <span class="keyword">if</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> -<a class="lnum" href="#156" name="156">0156</a>        <span class="name">pairs</span> <span class="op">=</span> <span class="name">object_hook</span><span class="op">(</span><span class="name">pairs</span><span class="op">)</span><br /> -<a class="lnum" href="#157" name="157">0157</a>    <span class="keyword">return</span> <span class="name">pairs</span><span class="op">,</span> <span class="name">end</span><br /> -<a class="lnum" href="#158" name="158">0158</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'{'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONObject</span><span class="op">)</span><br /> -<a class="lnum" href="#159" name="159">0159</a><br /> -<a class="lnum" href="#160" name="160">0160</a><span class="keyword">def</span> <span class="name">JSONArray</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#161" name="161">0161</a>    <span class="name">values</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><br /> -<a class="lnum" href="#162" name="162">0162</a>    <span class="name">s</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">string</span><br /> -<a class="lnum" href="#163" name="163">0163</a>    <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#164" name="164">0164</a>    <span class="comment"># look-ahead for trivial empty array</span><br /> -<a class="lnum" href="#165" name="165">0165</a><span class="comment"></span>    <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> -<a class="lnum" href="#166" name="166">0166</a>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">']'</span><span class="op">:</span><br /> -<a class="lnum" href="#167" name="167">0167</a>        <span class="keyword">return</span> <span class="name">values</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><br /> -<a class="lnum" href="#168" name="168">0168</a>    <span class="name">iterscan</span> <span class="op">=</span> <span class="name">JSONScanner</span><span class="op">.</span><span class="name">iterscan</span><br /> -<a class="lnum" href="#169" name="169">0169</a>    <span class="keyword">while</span> <span class="name">True</span><span class="op">:</span><br /> -<a class="lnum" href="#170" name="170">0170</a>        <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#171" name="171">0171</a>            <span class="name">value</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">end</span><span class="op">,</span> <span class="name">context</span><span class="op">=</span><span class="name">context</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#172" name="172">0172</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> -<a class="lnum" href="#173" name="173">0173</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting object"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#174" name="174">0174</a>        <span class="name">values</span><span class="op">.</span><span class="name">append</span><span class="op">(</span><span class="name">value</span><span class="op">)</span><br /> -<a class="lnum" href="#175" name="175">0175</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#176" name="176">0176</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> -<a class="lnum" href="#177" name="177">0177</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> -<a class="lnum" href="#178" name="178">0178</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">']'</span><span class="op">:</span><br /> -<a class="lnum" href="#179" name="179">0179</a>            <span class="keyword">break</span><br /> -<a class="lnum" href="#180" name="180">0180</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">','</span><span class="op">:</span><br /> -<a class="lnum" href="#181" name="181">0181</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting , delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#182" name="182">0182</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#183" name="183">0183</a>    <span class="keyword">return</span> <span class="name">values</span><span class="op">,</span> <span class="name">end</span><br /> -<a class="lnum" href="#184" name="184">0184</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'\['</span><span class="op">)</span><span class="op">(</span><span class="name">JSONArray</span><span class="op">)</span><br /> -<a class="lnum" href="#185" name="185">0185</a><br /> -<a class="lnum" href="#186" name="186">0186</a><span class="name">ANYTHING</span> <span class="op">=</span> <span class="op">[</span><br /> -<a class="lnum" href="#187" name="187">0187</a>    <span class="name">JSONObject</span><span class="op">,</span><br /> -<a class="lnum" href="#188" name="188">0188</a>    <span class="name">JSONArray</span><span class="op">,</span><br /> -<a class="lnum" href="#189" name="189">0189</a>    <span class="name">JSONString</span><span class="op">,</span><br /> -<a class="lnum" href="#190" name="190">0190</a>    <span class="name">JSONConstant</span><span class="op">,</span><br /> -<a class="lnum" href="#191" name="191">0191</a>    <span class="name">JSONNumber</span><span class="op">,</span><br /> -<a class="lnum" href="#192" name="192">0192</a><span class="op">]</span><br /> +<a class="lnum" href="#102" name="102">0102</a>                <span class="name">m</span> <span class="op">=</span> <span class="name">_b</span><span class="op">[</span><span class="name">esc</span><span class="op">]</span><br /> +<a class="lnum" href="#103" name="103">0103</a>            <span class="keyword">except</span> <span class="name">KeyError</span><span class="op">:</span><br /> +<a class="lnum" href="#104" name="104">0104</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><br /> +<a class="lnum" href="#105" name="105">0105</a>                    <span class="name">errmsg</span><span class="op">(</span><span class="string">"Invalid \\escape: %r"</span> <span class="op">%</span> <span class="op">(</span><span class="name">esc</span><span class="op">,</span><span class="op">)</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#106" name="106">0106</a>            <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#107" name="107">0107</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#108" name="108">0108</a>            <span class="name">esc</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">5</span><span class="op">]</span><br /> +<a class="lnum" href="#109" name="109">0109</a>            <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#110" name="110">0110</a>                <span class="name">m</span> <span class="op">=</span> <span class="name">unichr</span><span class="op">(</span><span class="name">int</span><span class="op">(</span><span class="name">esc</span><span class="op">,</span> <span class="number">16</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#111" name="111">0111</a>                <span class="keyword">if</span> <span class="name">len</span><span class="op">(</span><span class="name">esc</span><span class="op">)</span> <span class="op">!=</span> <span class="number">4</span> <span class="keyword">or</span> <span class="keyword">not</span> <span class="name">esc</span><span class="op">.</span><span class="name">isalnum</span><span class="op">(</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#112" name="112">0112</a>                    <span class="keyword">raise</span> <span class="name">ValueError</span><br /> +<a class="lnum" href="#113" name="113">0113</a>            <span class="keyword">except</span> <span class="name">ValueError</span><span class="op">:</span><br /> +<a class="lnum" href="#114" name="114">0114</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Invalid \\uXXXX escape"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#115" name="115">0115</a>            <span class="name">end</span> <span class="op">+=</span> <span class="number">5</span><br /> +<a class="lnum" href="#116" name="116">0116</a>        <span class="name">_append</span><span class="op">(</span><span class="name">m</span><span class="op">)</span><br /> +<a class="lnum" href="#117" name="117">0117</a>    <span class="keyword">return</span> <span class="string">u''</span><span class="op">.</span><span class="name">join</span><span class="op">(</span><span class="name">chunks</span><span class="op">)</span><span class="op">,</span> <span class="name">end</span><br /> +<a class="lnum" href="#118" name="118">0118</a><br /> +<a class="lnum" href="#119" name="119">0119</a><span class="keyword">def</span> <span class="name">JSONString</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#120" name="120">0120</a>    <span class="name">encoding</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'encoding'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> +<a class="lnum" href="#121" name="121">0121</a>    <span class="keyword">return</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">match</span><span class="op">.</span><span class="name">string</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#122" name="122">0122</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'"'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONString</span><span class="op">)</span><br /> +<a class="lnum" href="#123" name="123">0123</a><br /> +<a class="lnum" href="#124" name="124">0124</a><span class="name">WHITESPACE</span> <span class="op">=</span> <span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="string">r'\s*'</span><span class="op">,</span> <span class="name">FLAGS</span><span class="op">)</span><br /> +<a class="lnum" href="#125" name="125">0125</a><br /> +<a class="lnum" href="#126" name="126">0126</a><span class="keyword">def</span> <span class="name">JSONObject</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#127" name="127">0127</a>    <span class="name">pairs</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span><br /> +<a class="lnum" href="#128" name="128">0128</a>    <span class="name">s</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">string</span><br /> +<a class="lnum" href="#129" name="129">0129</a>    <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#130" name="130">0130</a>    <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#131" name="131">0131</a>    <span class="comment"># trivial empty object</span><br /> +<a class="lnum" href="#132" name="132">0132</a><span class="comment"></span>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">'}'</span><span class="op">:</span><br /> +<a class="lnum" href="#133" name="133">0133</a>        <span class="keyword">return</span> <span class="name">pairs</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><br /> +<a class="lnum" href="#134" name="134">0134</a>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">'"'</span><span class="op">:</span><br /> +<a class="lnum" href="#135" name="135">0135</a>        <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting property name"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#136" name="136">0136</a>    <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#137" name="137">0137</a>    <span class="name">encoding</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'encoding'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> +<a class="lnum" href="#138" name="138">0138</a>    <span class="name">iterscan</span> <span class="op">=</span> <span class="name">JSONScanner</span><span class="op">.</span><span class="name">iterscan</span><br /> +<a class="lnum" href="#139" name="139">0139</a>    <span class="keyword">while</span> <span class="name">True</span><span class="op">:</span><br /> +<a class="lnum" href="#140" name="140">0140</a>        <span class="name">key</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">scanstring</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#141" name="141">0141</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#142" name="142">0142</a>        <span class="keyword">if</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span> <span class="op">!=</span> <span class="string">':'</span><span class="op">:</span><br /> +<a class="lnum" href="#143" name="143">0143</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting : delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#144" name="144">0144</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#145" name="145">0145</a>        <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#146" name="146">0146</a>            <span class="name">value</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">end</span><span class="op">,</span> <span class="name">context</span><span class="op">=</span><span class="name">context</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#147" name="147">0147</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> +<a class="lnum" href="#148" name="148">0148</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting object"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#149" name="149">0149</a>        <span class="name">pairs</span><span class="op">[</span><span class="name">key</span><span class="op">]</span> <span class="op">=</span> <span class="name">value</span><br /> +<a class="lnum" href="#150" name="150">0150</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#151" name="151">0151</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#152" name="152">0152</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#153" name="153">0153</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">'}'</span><span class="op">:</span><br /> +<a class="lnum" href="#154" name="154">0154</a>            <span class="keyword">break</span><br /> +<a class="lnum" href="#155" name="155">0155</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">','</span><span class="op">:</span><br /> +<a class="lnum" href="#156" name="156">0156</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting , delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#157" name="157">0157</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#158" name="158">0158</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#159" name="159">0159</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#160" name="160">0160</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">'"'</span><span class="op">:</span><br /> +<a class="lnum" href="#161" name="161">0161</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting property name"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span> <span class="op">-</span> <span class="number">1</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#162" name="162">0162</a>    <span class="name">object_hook</span> <span class="op">=</span> <span class="name">getattr</span><span class="op">(</span><span class="name">context</span><span class="op">,</span> <span class="string">'object_hook'</span><span class="op">,</span> <span class="name">None</span><span class="op">)</span><br /> +<a class="lnum" href="#163" name="163">0163</a>    <span class="keyword">if</span> <span class="name">object_hook</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#164" name="164">0164</a>        <span class="name">pairs</span> <span class="op">=</span> <span class="name">object_hook</span><span class="op">(</span><span class="name">pairs</span><span class="op">)</span><br /> +<a class="lnum" href="#165" name="165">0165</a>    <span class="keyword">return</span> <span class="name">pairs</span><span class="op">,</span> <span class="name">end</span><br /> +<a class="lnum" href="#166" name="166">0166</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'{'</span><span class="op">)</span><span class="op">(</span><span class="name">JSONObject</span><span class="op">)</span><br /> +<a class="lnum" href="#167" name="167">0167</a><br /> +<a class="lnum" href="#168" name="168">0168</a><span class="keyword">def</span> <span class="name">JSONArray</span><span class="op">(</span><span class="name">match</span><span class="op">,</span> <span class="name">context</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#169" name="169">0169</a>    <span class="name">values</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><br /> +<a class="lnum" href="#170" name="170">0170</a>    <span class="name">s</span> <span class="op">=</span> <span class="name">match</span><span class="op">.</span><span class="name">string</span><br /> +<a class="lnum" href="#171" name="171">0171</a>    <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">match</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#172" name="172">0172</a>    <span class="comment"># look-ahead for trivial empty array</span><br /> +<a class="lnum" href="#173" name="173">0173</a><span class="comment"></span>    <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#174" name="174">0174</a>    <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">']'</span><span class="op">:</span><br /> +<a class="lnum" href="#175" name="175">0175</a>        <span class="keyword">return</span> <span class="name">values</span><span class="op">,</span> <span class="name">end</span> <span class="op">+</span> <span class="number">1</span><br /> +<a class="lnum" href="#176" name="176">0176</a>    <span class="name">iterscan</span> <span class="op">=</span> <span class="name">JSONScanner</span><span class="op">.</span><span class="name">iterscan</span><br /> +<a class="lnum" href="#177" name="177">0177</a>    <span class="keyword">while</span> <span class="name">True</span><span class="op">:</span><br /> +<a class="lnum" href="#178" name="178">0178</a>        <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#179" name="179">0179</a>            <span class="name">value</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">end</span><span class="op">,</span> <span class="name">context</span><span class="op">=</span><span class="name">context</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#180" name="180">0180</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> +<a class="lnum" href="#181" name="181">0181</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting object"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#182" name="182">0182</a>        <span class="name">values</span><span class="op">.</span><span class="name">append</span><span class="op">(</span><span class="name">value</span><span class="op">)</span><br /> +<a class="lnum" href="#183" name="183">0183</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#184" name="184">0184</a>        <span class="name">nextchar</span> <span class="op">=</span> <span class="name">s</span><span class="op">[</span><span class="name">end</span><span class="op">:</span><span class="name">end</span> <span class="op">+</span> <span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#185" name="185">0185</a>        <span class="name">end</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#186" name="186">0186</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">==</span> <span class="string">']'</span><span class="op">:</span><br /> +<a class="lnum" href="#187" name="187">0187</a>            <span class="keyword">break</span><br /> +<a class="lnum" href="#188" name="188">0188</a>        <span class="keyword">if</span> <span class="name">nextchar</span> <span class="op">!=</span> <span class="string">','</span><span class="op">:</span><br /> +<a class="lnum" href="#189" name="189">0189</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Expecting , delimiter"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#190" name="190">0190</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#191" name="191">0191</a>    <span class="keyword">return</span> <span class="name">values</span><span class="op">,</span> <span class="name">end</span><br /> +<a class="lnum" href="#192" name="192">0192</a><span class="name">pattern</span><span class="op">(</span><span class="string">r'\['</span><span class="op">)</span><span class="op">(</span><span class="name">JSONArray</span><span class="op">)</span><br /> <a class="lnum" href="#193" name="193">0193</a><br /> -<a class="lnum" href="#194" name="194">0194</a><span class="name">JSONScanner</span> <span class="op">=</span> <span class="name">Scanner</span><span class="op">(</span><span class="name">ANYTHING</span><span class="op">)</span><br /> -<a class="lnum" href="#195" name="195">0195</a><br /> -<a class="lnum" href="#196" name="196">0196</a><span class="keyword">class</span> <span class="name">JSONDecoder</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#197" name="197">0197</a>    <span class="string">"""</span><br /> -<a class="lnum" href="#198" name="198">0198</a><span class="string">    Simple JSON <http://json.org> decoder</span><br /> -<a class="lnum" href="#199" name="199">0199</a><span class="string"></span><br /> -<a class="lnum" href="#200" name="200">0200</a><span class="string">    Performs the following translations in decoding:</span><br /> -<a class="lnum" href="#201" name="201">0201</a><span class="string">    </span><br /> -<a class="lnum" href="#202" name="202">0202</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#203" name="203">0203</a><span class="string">    | JSON          | Python            |</span><br /> -<a class="lnum" href="#204" name="204">0204</a><span class="string">    +===============+===================+</span><br /> -<a class="lnum" href="#205" name="205">0205</a><span class="string">    | object        | dict              |</span><br /> -<a class="lnum" href="#206" name="206">0206</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#207" name="207">0207</a><span class="string">    | array         | list              |</span><br /> -<a class="lnum" href="#208" name="208">0208</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#209" name="209">0209</a><span class="string">    | string        | unicode           |</span><br /> +<a class="lnum" href="#194" name="194">0194</a><span class="name">ANYTHING</span> <span class="op">=</span> <span class="op">[</span><br /> +<a class="lnum" href="#195" name="195">0195</a>    <span class="name">JSONObject</span><span class="op">,</span><br /> +<a class="lnum" href="#196" name="196">0196</a>    <span class="name">JSONArray</span><span class="op">,</span><br /> +<a class="lnum" href="#197" name="197">0197</a>    <span class="name">JSONString</span><span class="op">,</span><br /> +<a class="lnum" href="#198" name="198">0198</a>    <span class="name">JSONConstant</span><span class="op">,</span><br /> +<a class="lnum" href="#199" name="199">0199</a>    <span class="name">JSONNumber</span><span class="op">,</span><br /> +<a class="lnum" href="#200" name="200">0200</a><span class="op">]</span><br /> +<a class="lnum" href="#201" name="201">0201</a><br /> +<a class="lnum" href="#202" name="202">0202</a><span class="name">JSONScanner</span> <span class="op">=</span> <span class="name">Scanner</span><span class="op">(</span><span class="name">ANYTHING</span><span class="op">)</span><br /> +<a class="lnum" href="#203" name="203">0203</a><br /> +<a class="lnum" href="#204" name="204">0204</a><span class="keyword">class</span> <span class="name">JSONDecoder</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#205" name="205">0205</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#206" name="206">0206</a><span class="string">    Simple JSON <http://json.org> decoder</span><br /> +<a class="lnum" href="#207" name="207">0207</a><span class="string"></span><br /> +<a class="lnum" href="#208" name="208">0208</a><span class="string">    Performs the following translations in decoding by default:</span><br /> +<a class="lnum" href="#209" name="209">0209</a><span class="string">    </span><br /> <a class="lnum" href="#210" name="210">0210</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#211" name="211">0211</a><span class="string">    | number (int)  | int, long         |</span><br /> -<a class="lnum" href="#212" name="212">0212</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#213" name="213">0213</a><span class="string">    | number (real) | float             |</span><br /> +<a class="lnum" href="#211" name="211">0211</a><span class="string">    | JSON          | Python            |</span><br /> +<a class="lnum" href="#212" name="212">0212</a><span class="string">    +===============+===================+</span><br /> +<a class="lnum" href="#213" name="213">0213</a><span class="string">    | object        | dict              |</span><br /> <a class="lnum" href="#214" name="214">0214</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#215" name="215">0215</a><span class="string">    | true          | True              |</span><br /> +<a class="lnum" href="#215" name="215">0215</a><span class="string">    | array         | list              |</span><br /> <a class="lnum" href="#216" name="216">0216</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#217" name="217">0217</a><span class="string">    | false         | False             |</span><br /> +<a class="lnum" href="#217" name="217">0217</a><span class="string">    | string        | unicode           |</span><br /> <a class="lnum" href="#218" name="218">0218</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#219" name="219">0219</a><span class="string">    | null          | None              |</span><br /> +<a class="lnum" href="#219" name="219">0219</a><span class="string">    | number (int)  | int, long         |</span><br /> <a class="lnum" href="#220" name="220">0220</a><span class="string">    +---------------+-------------------+</span><br /> -<a class="lnum" href="#221" name="221">0221</a><span class="string"></span><br /> -<a class="lnum" href="#222" name="222">0222</a><span class="string">    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as</span><br /> -<a class="lnum" href="#223" name="223">0223</a><span class="string">    their corresponding ``float`` values, which is outside the JSON spec.</span><br /> -<a class="lnum" href="#224" name="224">0224</a><span class="string">    """</span><br /> -<a class="lnum" href="#225" name="225">0225</a><br /> -<a class="lnum" href="#226" name="226">0226</a>    <span class="name">_scanner</span> <span class="op">=</span> <span class="name">Scanner</span><span class="op">(</span><span class="name">ANYTHING</span><span class="op">)</span><br /> -<a class="lnum" href="#227" name="227">0227</a>    <span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'__init__'</span><span class="op">,</span> <span class="string">'decode'</span><span class="op">,</span> <span class="string">'raw_decode'</span><span class="op">]</span><br /> -<a class="lnum" href="#228" name="228">0228</a><br /> -<a class="lnum" href="#229" name="229">0229</a>    <span class="keyword">def</span> <span class="name">__init__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#230" name="230">0230</a>        <span class="string">"""</span><br /> -<a class="lnum" href="#231" name="231">0231</a><span class="string">        ``encoding`` determines the encoding used to interpret any ``str``</span><br /> -<a class="lnum" href="#232" name="232">0232</a><span class="string">        objects decoded by this instance (utf-8 by default).  It has no</span><br /> -<a class="lnum" href="#233" name="233">0233</a><span class="string">        effect when decoding ``unicode`` objects.</span><br /> -<a class="lnum" href="#234" name="234">0234</a><span class="string">        </span><br /> -<a class="lnum" href="#235" name="235">0235</a><span class="string">        Note that currently only encodings that are a superset of ASCII work,</span><br /> -<a class="lnum" href="#236" name="236">0236</a><span class="string">        strings of other encodings should be passed in as ``unicode``.</span><br /> -<a class="lnum" href="#237" name="237">0237</a><span class="string"></span><br /> -<a class="lnum" href="#238" name="238">0238</a><span class="string">        ``object_hook``, if specified, will be called with the result</span><br /> -<a class="lnum" href="#239" name="239">0239</a><span class="string">        of every JSON object decoded and its return value will be used in</span><br /> -<a class="lnum" href="#240" name="240">0240</a><span class="string">        place of the given ``dict``.  This can be used to provide custom</span><br /> -<a class="lnum" href="#241" name="241">0241</a><span class="string">        deserializations (e.g. to support JSON-RPC class hinting).</span><br /> -<a class="lnum" href="#242" name="242">0242</a><span class="string">        """</span><br /> -<a class="lnum" href="#243" name="243">0243</a>        <span class="name">self</span><span class="op">.</span><span class="name">encoding</span> <span class="op">=</span> <span class="name">encoding</span><br /> -<a class="lnum" href="#244" name="244">0244</a>        <span class="name">self</span><span class="op">.</span><span class="name">object_hook</span> <span class="op">=</span> <span class="name">object_hook</span><br /> -<a class="lnum" href="#245" name="245">0245</a><br /> -<a class="lnum" href="#246" name="246">0246</a>    <span class="keyword">def</span> <span class="name">decode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#247" name="247">0247</a>        <span class="string">"""</span><br /> -<a class="lnum" href="#248" name="248">0248</a><span class="string">        Return the Python representation of ``s`` (a ``str`` or ``unicode``</span><br /> -<a class="lnum" href="#249" name="249">0249</a><span class="string">        instance containing a JSON document)</span><br /> -<a class="lnum" href="#250" name="250">0250</a><span class="string">        """</span><br /> -<a class="lnum" href="#251" name="251">0251</a>        <span class="name">obj</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">raw_decode</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="number">0</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#252" name="252">0252</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#253" name="253">0253</a>        <span class="keyword">if</span> <span class="name">end</span> <span class="op">!=</span> <span class="name">len</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#254" name="254">0254</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Extra data"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">len</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><br /> -<a class="lnum" href="#255" name="255">0255</a>        <span class="keyword">return</span> <span class="name">obj</span><br /> -<a class="lnum" href="#256" name="256">0256</a><br /> -<a class="lnum" href="#257" name="257">0257</a>    <span class="keyword">def</span> <span class="name">raw_decode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> -<a class="lnum" href="#258" name="258">0258</a>        <span class="string">"""</span><br /> -<a class="lnum" href="#259" name="259">0259</a><span class="string">        Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning</span><br /> -<a class="lnum" href="#260" name="260">0260</a><span class="string">        with a JSON document) and return a 2-tuple of the Python</span><br /> -<a class="lnum" href="#261" name="261">0261</a><span class="string">        representation and the index in ``s`` where the document ended.</span><br /> -<a class="lnum" href="#262" name="262">0262</a><span class="string"></span><br /> -<a class="lnum" href="#263" name="263">0263</a><span class="string">        This can be used to decode a JSON document from a string that may</span><br /> -<a class="lnum" href="#264" name="264">0264</a><span class="string">        have extraneous data at the end.</span><br /> -<a class="lnum" href="#265" name="265">0265</a><span class="string">        """</span><br /> -<a class="lnum" href="#266" name="266">0266</a>        <span class="name">kw</span><span class="op">.</span><span class="name">setdefault</span><span class="op">(</span><span class="string">'context'</span><span class="op">,</span> <span class="name">self</span><span class="op">)</span><br /> -<a class="lnum" href="#267" name="267">0267</a>        <span class="keyword">try</span><span class="op">:</span><br /> -<a class="lnum" href="#268" name="268">0268</a>            <span class="name">obj</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">_scanner</span><span class="op">.</span><span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> -<a class="lnum" href="#269" name="269">0269</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> -<a class="lnum" href="#270" name="270">0270</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"No JSON object could be decoded"</span><span class="op">)</span><br /> -<a class="lnum" href="#271" name="271">0271</a>        <span class="keyword">return</span> <span class="name">obj</span><span class="op">,</span> <span class="name">end</span><br /> +<a class="lnum" href="#221" name="221">0221</a><span class="string">    | number (real) | float             |</span><br /> +<a class="lnum" href="#222" name="222">0222</a><span class="string">    +---------------+-------------------+</span><br /> +<a class="lnum" href="#223" name="223">0223</a><span class="string">    | true          | True              |</span><br /> +<a class="lnum" href="#224" name="224">0224</a><span class="string">    +---------------+-------------------+</span><br /> +<a class="lnum" href="#225" name="225">0225</a><span class="string">    | false         | False             |</span><br /> +<a class="lnum" href="#226" name="226">0226</a><span class="string">    +---------------+-------------------+</span><br /> +<a class="lnum" href="#227" name="227">0227</a><span class="string">    | null          | None              |</span><br /> +<a class="lnum" href="#228" name="228">0228</a><span class="string">    +---------------+-------------------+</span><br /> +<a class="lnum" href="#229" name="229">0229</a><span class="string"></span><br /> +<a class="lnum" href="#230" name="230">0230</a><span class="string">    It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as</span><br /> +<a class="lnum" href="#231" name="231">0231</a><span class="string">    their corresponding ``float`` values, which is outside the JSON spec.</span><br /> +<a class="lnum" href="#232" name="232">0232</a><span class="string">    """</span><br /> +<a class="lnum" href="#233" name="233">0233</a><br /> +<a class="lnum" href="#234" name="234">0234</a>    <span class="name">_scanner</span> <span class="op">=</span> <span class="name">Scanner</span><span class="op">(</span><span class="name">ANYTHING</span><span class="op">)</span><br /> +<a class="lnum" href="#235" name="235">0235</a>    <span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'__init__'</span><span class="op">,</span> <span class="string">'decode'</span><span class="op">,</span> <span class="string">'raw_decode'</span><span class="op">]</span><br /> +<a class="lnum" href="#236" name="236">0236</a><br /> +<a class="lnum" href="#237" name="237">0237</a>    <span class="keyword">def</span> <span class="name">__init__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">encoding</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">object_hook</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_float</span><span class="op">=</span><span class="name">None</span><span class="op">,</span><br /> +<a class="lnum" href="#238" name="238">0238</a>            <span class="name">parse_int</span><span class="op">=</span><span class="name">None</span><span class="op">,</span> <span class="name">parse_constant</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#239" name="239">0239</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#240" name="240">0240</a><span class="string">        ``encoding`` determines the encoding used to interpret any ``str``</span><br /> +<a class="lnum" href="#241" name="241">0241</a><span class="string">        objects decoded by this instance (utf-8 by default).  It has no</span><br /> +<a class="lnum" href="#242" name="242">0242</a><span class="string">        effect when decoding ``unicode`` objects.</span><br /> +<a class="lnum" href="#243" name="243">0243</a><span class="string">        </span><br /> +<a class="lnum" href="#244" name="244">0244</a><span class="string">        Note that currently only encodings that are a superset of ASCII work,</span><br /> +<a class="lnum" href="#245" name="245">0245</a><span class="string">        strings of other encodings should be passed in as ``unicode``.</span><br /> +<a class="lnum" href="#246" name="246">0246</a><span class="string"></span><br /> +<a class="lnum" href="#247" name="247">0247</a><span class="string">        ``object_hook``, if specified, will be called with the result</span><br /> +<a class="lnum" href="#248" name="248">0248</a><span class="string">        of every JSON object decoded and its return value will be used in</span><br /> +<a class="lnum" href="#249" name="249">0249</a><span class="string">        place of the given ``dict``.  This can be used to provide custom</span><br /> +<a class="lnum" href="#250" name="250">0250</a><span class="string">        deserializations (e.g. to support JSON-RPC class hinting).</span><br /> +<a class="lnum" href="#251" name="251">0251</a><span class="string"></span><br /> +<a class="lnum" href="#252" name="252">0252</a><span class="string">        ``parse_float``, if specified, will be called with the string</span><br /> +<a class="lnum" href="#253" name="253">0253</a><span class="string">        of every JSON float to be decoded. By default this is equivalent to</span><br /> +<a class="lnum" href="#254" name="254">0254</a><span class="string">        float(num_str). This can be used to use another datatype or parser</span><br /> +<a class="lnum" href="#255" name="255">0255</a><span class="string">        for JSON floats (e.g. decimal.Decimal).</span><br /> +<a class="lnum" href="#256" name="256">0256</a><span class="string"></span><br /> +<a class="lnum" href="#257" name="257">0257</a><span class="string">        ``parse_int``, if specified, will be called with the string</span><br /> +<a class="lnum" href="#258" name="258">0258</a><span class="string">        of every JSON int to be decoded. By default this is equivalent to</span><br /> +<a class="lnum" href="#259" name="259">0259</a><span class="string">        int(num_str). This can be used to use another datatype or parser</span><br /> +<a class="lnum" href="#260" name="260">0260</a><span class="string">        for JSON integers (e.g. float).</span><br /> +<a class="lnum" href="#261" name="261">0261</a><span class="string"></span><br /> +<a class="lnum" href="#262" name="262">0262</a><span class="string">        ``parse_constant``, if specified, will be called with one of the</span><br /> +<a class="lnum" href="#263" name="263">0263</a><span class="string">        following strings: -Infinity, Infinity, NaN, null, true, false.</span><br /> +<a class="lnum" href="#264" name="264">0264</a><span class="string">        This can be used to raise an exception if invalid JSON numbers</span><br /> +<a class="lnum" href="#265" name="265">0265</a><span class="string">        are encountered.</span><br /> +<a class="lnum" href="#266" name="266">0266</a><span class="string">        """</span><br /> +<a class="lnum" href="#267" name="267">0267</a>        <span class="name">self</span><span class="op">.</span><span class="name">encoding</span> <span class="op">=</span> <span class="name">encoding</span><br /> +<a class="lnum" href="#268" name="268">0268</a>        <span class="name">self</span><span class="op">.</span><span class="name">object_hook</span> <span class="op">=</span> <span class="name">object_hook</span><br /> +<a class="lnum" href="#269" name="269">0269</a>        <span class="name">self</span><span class="op">.</span><span class="name">parse_float</span> <span class="op">=</span> <span class="name">parse_float</span><br /> +<a class="lnum" href="#270" name="270">0270</a>        <span class="name">self</span><span class="op">.</span><span class="name">parse_int</span> <span class="op">=</span> <span class="name">parse_int</span><br /> +<a class="lnum" href="#271" name="271">0271</a>        <span class="name">self</span><span class="op">.</span><span class="name">parse_constant</span> <span class="op">=</span> <span class="name">parse_constant</span><br /> <a class="lnum" href="#272" name="272">0272</a><br /> -<a class="lnum" href="#273" name="273">0273</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'JSONDecoder'</span><span class="op">]</span></code></div></body></html>
\ No newline at end of file +<a class="lnum" href="#273" name="273">0273</a>    <span class="keyword">def</span> <span class="name">decode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">_w</span><span class="op">=</span><span class="name">WHITESPACE</span><span class="op">.</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#274" name="274">0274</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#275" name="275">0275</a><span class="string">        Return the Python representation of ``s`` (a ``str`` or ``unicode``</span><br /> +<a class="lnum" href="#276" name="276">0276</a><span class="string">        instance containing a JSON document)</span><br /> +<a class="lnum" href="#277" name="277">0277</a><span class="string">        """</span><br /> +<a class="lnum" href="#278" name="278">0278</a>        <span class="name">obj</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">raw_decode</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="number">0</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#279" name="279">0279</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">_w</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><span class="op">.</span><span class="name">end</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#280" name="280">0280</a>        <span class="keyword">if</span> <span class="name">end</span> <span class="op">!=</span> <span class="name">len</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#281" name="281">0281</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="name">errmsg</span><span class="op">(</span><span class="string">"Extra data"</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="name">end</span><span class="op">,</span> <span class="name">len</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#282" name="282">0282</a>        <span class="keyword">return</span> <span class="name">obj</span><br /> +<a class="lnum" href="#283" name="283">0283</a><br /> +<a class="lnum" href="#284" name="284">0284</a>    <span class="keyword">def</span> <span class="name">raw_decode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">s</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#285" name="285">0285</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#286" name="286">0286</a><span class="string">        Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning</span><br /> +<a class="lnum" href="#287" name="287">0287</a><span class="string">        with a JSON document) and return a 2-tuple of the Python</span><br /> +<a class="lnum" href="#288" name="288">0288</a><span class="string">        representation and the index in ``s`` where the document ended.</span><br /> +<a class="lnum" href="#289" name="289">0289</a><span class="string"></span><br /> +<a class="lnum" href="#290" name="290">0290</a><span class="string">        This can be used to decode a JSON document from a string that may</span><br /> +<a class="lnum" href="#291" name="291">0291</a><span class="string">        have extraneous data at the end.</span><br /> +<a class="lnum" href="#292" name="292">0292</a><span class="string">        """</span><br /> +<a class="lnum" href="#293" name="293">0293</a>        <span class="name">kw</span><span class="op">.</span><span class="name">setdefault</span><span class="op">(</span><span class="string">'context'</span><span class="op">,</span> <span class="name">self</span><span class="op">)</span><br /> +<a class="lnum" href="#294" name="294">0294</a>        <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#295" name="295">0295</a>            <span class="name">obj</span><span class="op">,</span> <span class="name">end</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">_scanner</span><span class="op">.</span><span class="name">iterscan</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">.</span><span class="name">next</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#296" name="296">0296</a>        <span class="keyword">except</span> <span class="name">StopIteration</span><span class="op">:</span><br /> +<a class="lnum" href="#297" name="297">0297</a>            <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"No JSON object could be decoded"</span><span class="op">)</span><br /> +<a class="lnum" href="#298" name="298">0298</a>        <span class="keyword">return</span> <span class="name">obj</span><span class="op">,</span> <span class="name">end</span><br /> +<a class="lnum" href="#299" name="299">0299</a><br /> +<a class="lnum" href="#300" name="300">0300</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'JSONDecoder'</span><span class="op">]</span></code></div></body></html>
\ No newline at end of file diff --git a/simplejson/__init__.py b/simplejson/__init__.py index f7adfa7..f13e96a 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -65,6 +65,9 @@ Specializing JSON object decoding:: >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', ... object_hook=as_complex) (1+2j) + >>> import decimal + >>> simplejson.loads('1.1', parse_float=decimal.Decimal) + decimal.Decimal(1.1) Extending JSONEncoder:: @@ -217,7 +220,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, _default_decoder = JSONDecoder(encoding=None, object_hook=None) -def load(fp, encoding=None, cls=None, object_hook=None, **kw): +def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None, **kw): """ Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object. @@ -238,9 +242,12 @@ def load(fp, encoding=None, cls=None, object_hook=None, **kw): kwarg. """ return loads(fp.read(), - encoding=encoding, cls=cls, object_hook=object_hook, **kw) + encoding=encoding, cls=cls, object_hook=object_hook, + parse_float=parse_float, parse_int=parse_int, + parse_constant=parse_constant, **kw) -def loads(s, encoding=None, cls=None, object_hook=None, **kw): +def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None, **kw): """ Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON document) to a Python object. @@ -255,20 +262,66 @@ def loads(s, encoding=None, cls=None, object_hook=None, **kw): ``object_hook`` will be used instead of the ``dict``. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting). + ``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. 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. float). + + ``parse_constant``, if specified, will be called with one of the + following strings: -Infinity, Infinity, NaN, null, true, false. + This can be used to raise an exception if invalid JSON numbers + are encountered. + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg. """ - if cls is None and encoding is None and object_hook is None and not kw: + if (cls is None and encoding is None and object_hook is None and + parse_int is None and parse_float is None and + parse_constant is None and not kw): return _default_decoder.decode(s) if cls is None: cls = JSONDecoder if object_hook is not None: kw['object_hook'] = object_hook + if parse_float is not None: + kw['parse_float'] = parse_float + if parse_int is not None: + kw['parse_int'] = parse_int + if parse_constant is not None: + kw['parse_constant'] = parse_constant return cls(encoding=encoding, **kw).decode(s) +# +# Compatibility cruft from other libraries +# + +def decode(s): + """ + demjson, python-cjson API compatibility hook. Use loads(s) instead. + """ + import warnings + warnings.warn("simplejson.loads(s) should be used instead of decode(s)", + DeprecationWarning) + return loads(s) + +def encode(obj): + """ + demjson, python-cjson compatibility hook. Use dumps(s) instead. + """ + import warnings + warnings.warn("simplejson.dumps(s) should be used instead of encode(s)", + DeprecationWarning) + return dumps(obj) + def read(s): """ - json-py API compatibility hook. Use loads(s) instead. + jsonlib, JsonUtils, python-json, json-py API compatibility hook. + Use loads(s) instead. """ import warnings warnings.warn("simplejson.loads(s) should be used instead of read(s)", @@ -277,11 +330,10 @@ def read(s): def write(obj): """ - json-py API compatibility hook. Use dumps(s) instead. + jsonlib, JsonUtils, python-json, json-py API compatibility hook. + Use dumps(s) instead. """ import warnings warnings.warn("simplejson.dumps(s) should be used instead of write(s)", DeprecationWarning) return dumps(obj) - - diff --git a/simplejson/decoder.py b/simplejson/decoder.py index a1b53b2..162483c 100644 --- a/simplejson/decoder.py +++ b/simplejson/decoder.py @@ -44,16 +44,24 @@ _CONSTANTS = { } def JSONConstant(match, context, c=_CONSTANTS): - return c[match.group(0)], None + s = match.group(0) + fn = getattr(context, 'parse_constant', None) + if fn is None: + rval = c[s] + else: + rval = fn(s) + return rval, None pattern('(-?Infinity|NaN|true|false|null)')(JSONConstant) def JSONNumber(match, context): match = JSONNumber.regex.match(match.string, *match.span()) integer, frac, exp = match.groups() if frac or exp: - res = float(integer + (frac or '') + (exp or '')) + fn = getattr(context, 'parse_float', None) or float + res = fn(integer + (frac or '') + (exp or '')) else: - res = int(integer) + fn = getattr(context, 'parse_int', None) or int + res = fn(integer) return res, None pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber) @@ -197,7 +205,7 @@ class JSONDecoder(object): """ Simple JSON <http://json.org> decoder - Performs the following translations in decoding: + Performs the following translations in decoding by default: +---------------+-------------------+ | JSON | Python | @@ -226,7 +234,8 @@ class JSONDecoder(object): _scanner = Scanner(ANYTHING) __all__ = ['__init__', 'decode', 'raw_decode'] - def __init__(self, encoding=None, object_hook=None): + def __init__(self, encoding=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None): """ ``encoding`` determines the encoding used to interpret any ``str`` objects decoded by this instance (utf-8 by default). It has no @@ -239,9 +248,27 @@ class JSONDecoder(object): of every JSON object decoded and its return value will be used in place of the given ``dict``. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting). + + ``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. 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. float). + + ``parse_constant``, if specified, will be called with one of the + following strings: -Infinity, Infinity, NaN, null, true, false. + This can be used to raise an exception if invalid JSON numbers + are encountered. """ self.encoding = encoding self.object_hook = object_hook + self.parse_float = parse_float + self.parse_int = parse_int + self.parse_constant = parse_constant def decode(self, s, _w=WHITESPACE.match): """ |