From 0d80d569fa60b7906867aecd056d4f809ee18413 Mon Sep 17 00:00:00 2001 From: Brian Warner <warner@lothar.com> Date: Tue, 10 Jul 2007 15:22:53 -0700 Subject: [PATCH] import simplejson-1.7.1 into src/simplejson --- src/simplejson/LICENSE.txt | 19 + src/simplejson/PKG-INFO | 29 ++ .../docs/class-simplejson.JSONDecoder.html | 210 ++++++++ .../docs/class-simplejson.JSONEncoder.html | 261 ++++++++++ src/simplejson/docs/class_to_source.js | 22 + src/simplejson/docs/index.html | 455 ++++++++++++++++++ src/simplejson/docs/layout.css | 208 ++++++++ src/simplejson/docs/module-index.html | 111 +++++ .../docs/module-simplejson-index.html | 127 +++++ src/simplejson/docs/module-simplejson.html | 455 ++++++++++++++++++ src/simplejson/docs/pudge.css | 60 +++ src/simplejson/docs/rst.css | 142 ++++++ .../docs/simplejson/__init__.py.html | 357 ++++++++++++++ .../docs/simplejson/decoder.py.html | 345 +++++++++++++ .../docs/simplejson/encoder.py.html | 443 +++++++++++++++++ .../docs/simplejson/jsonfilter.py.html | 112 +++++ .../docs/simplejson/scanner.py.html | 135 ++++++ src/simplejson/ez_setup/README.txt | 15 + src/simplejson/ez_setup/__init__.py | 228 +++++++++ src/simplejson/scripts/bench.sh | 2 + src/simplejson/scripts/make_docs.py | 28 ++ src/simplejson/scripts/prof.py | 9 + src/simplejson/setup.cfg | 5 + src/simplejson/setup.py | 81 ++++ src/simplejson/simplejson.egg-info/PKG-INFO | 29 ++ .../simplejson.egg-info/SOURCES.txt | 46 ++ .../simplejson.egg-info/dependency_links.txt | 1 + .../simplejson.egg-info/entry_points.txt | 3 + .../simplejson.egg-info/top_level.txt | 1 + src/simplejson/simplejson.egg-info/zip-safe | 1 + src/simplejson/simplejson/__init__.py | 287 +++++++++++ src/simplejson/simplejson/_speedups.c | 215 +++++++++ src/simplejson/simplejson/decoder.py | 273 +++++++++++ src/simplejson/simplejson/encoder.py | 371 ++++++++++++++ src/simplejson/simplejson/jsonfilter.py | 40 ++ src/simplejson/simplejson/scanner.py | 63 +++ src/simplejson/simplejson/tests/__init__.py | 0 .../simplejson/tests/test_attacks.py | 6 + src/simplejson/simplejson/tests/test_dump.py | 10 + src/simplejson/simplejson/tests/test_fail.py | 70 +++ .../simplejson/tests/test_indent.py | 41 ++ src/simplejson/simplejson/tests/test_pass1.py | 72 +++ src/simplejson/simplejson/tests/test_pass2.py | 11 + src/simplejson/simplejson/tests/test_pass3.py | 16 + .../simplejson/tests/test_recursion.py | 62 +++ .../simplejson/tests/test_separators.py | 41 ++ .../simplejson/tests/test_unicode.py | 16 + 47 files changed, 5534 insertions(+) create mode 100644 src/simplejson/LICENSE.txt create mode 100644 src/simplejson/PKG-INFO create mode 100644 src/simplejson/docs/class-simplejson.JSONDecoder.html create mode 100644 src/simplejson/docs/class-simplejson.JSONEncoder.html create mode 100644 src/simplejson/docs/class_to_source.js create mode 100644 src/simplejson/docs/index.html create mode 100644 src/simplejson/docs/layout.css create mode 100644 src/simplejson/docs/module-index.html create mode 100644 src/simplejson/docs/module-simplejson-index.html create mode 100644 src/simplejson/docs/module-simplejson.html create mode 100644 src/simplejson/docs/pudge.css create mode 100644 src/simplejson/docs/rst.css create mode 100644 src/simplejson/docs/simplejson/__init__.py.html create mode 100644 src/simplejson/docs/simplejson/decoder.py.html create mode 100644 src/simplejson/docs/simplejson/encoder.py.html create mode 100644 src/simplejson/docs/simplejson/jsonfilter.py.html create mode 100644 src/simplejson/docs/simplejson/scanner.py.html create mode 100644 src/simplejson/ez_setup/README.txt create mode 100644 src/simplejson/ez_setup/__init__.py create mode 100644 src/simplejson/scripts/bench.sh create mode 100644 src/simplejson/scripts/make_docs.py create mode 100644 src/simplejson/scripts/prof.py create mode 100644 src/simplejson/setup.cfg create mode 100644 src/simplejson/setup.py create mode 100644 src/simplejson/simplejson.egg-info/PKG-INFO create mode 100644 src/simplejson/simplejson.egg-info/SOURCES.txt create mode 100644 src/simplejson/simplejson.egg-info/dependency_links.txt create mode 100644 src/simplejson/simplejson.egg-info/entry_points.txt create mode 100644 src/simplejson/simplejson.egg-info/top_level.txt create mode 100644 src/simplejson/simplejson.egg-info/zip-safe create mode 100644 src/simplejson/simplejson/__init__.py create mode 100644 src/simplejson/simplejson/_speedups.c create mode 100644 src/simplejson/simplejson/decoder.py create mode 100644 src/simplejson/simplejson/encoder.py create mode 100644 src/simplejson/simplejson/jsonfilter.py create mode 100644 src/simplejson/simplejson/scanner.py create mode 100644 src/simplejson/simplejson/tests/__init__.py create mode 100644 src/simplejson/simplejson/tests/test_attacks.py create mode 100644 src/simplejson/simplejson/tests/test_dump.py create mode 100644 src/simplejson/simplejson/tests/test_fail.py create mode 100644 src/simplejson/simplejson/tests/test_indent.py create mode 100644 src/simplejson/simplejson/tests/test_pass1.py create mode 100644 src/simplejson/simplejson/tests/test_pass2.py create mode 100644 src/simplejson/simplejson/tests/test_pass3.py create mode 100644 src/simplejson/simplejson/tests/test_recursion.py create mode 100644 src/simplejson/simplejson/tests/test_separators.py create mode 100644 src/simplejson/simplejson/tests/test_unicode.py diff --git a/src/simplejson/LICENSE.txt b/src/simplejson/LICENSE.txt new file mode 100644 index 00000000..ad95f29c --- /dev/null +++ b/src/simplejson/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2006 Bob Ippolito + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/simplejson/PKG-INFO b/src/simplejson/PKG-INFO new file mode 100644 index 00000000..c202c5fa --- /dev/null +++ b/src/simplejson/PKG-INFO @@ -0,0 +1,29 @@ +Metadata-Version: 1.0 +Name: simplejson +Version: 1.7.1 +Summary: Simple, fast, extensible JSON encoder/decoder for Python +Home-page: http://undefined.org/python/#simplejson +Author: Bob Ippolito +Author-email: bob@redivi.com +License: MIT License +Description: + simplejson is a simple, fast, complete, correct and extensible + JSON <http://json.org> encoder and decoder for Python 2.3+. It is + pure Python code with no dependencies, but includes an optional C + extension for a serious speed boost. + + simplejson was formerly known as simple_json, but changed its name to + comply with PEP 8 module naming guidelines. + + The encoder may be subclassed to provide serialization in any kind of + situation, without any special support by the objects to be serialized + (somewhat like pickle). + + The decoder can handle incoming JSON strings of any specified encoding + (UTF-8 by default). + +Platform: any +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: MIT License +Classifier: Programming Language :: Python +Classifier: Topic :: Software Development :: Libraries :: Python Modules diff --git a/src/simplejson/docs/class-simplejson.JSONDecoder.html b/src/simplejson/docs/class-simplejson.JSONDecoder.html new file mode 100644 index 00000000..c5f31ffe --- /dev/null +++ b/src/simplejson/docs/class-simplejson.JSONDecoder.html @@ -0,0 +1,210 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>simplejson.JSONDecoder -- Simple JSON &lt;<a class="reference" href="http://json.org">http://json.org</a>&gt; decoder</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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + + <A HREF="class-simplejson.JSONDecoder.html">details</A> + + <A HREF="class-simplejson.JSONDecoder-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>JSONDecoder</TT> + </H1> + <H4 CLASS="pudge-member-page-subheading"> + Simple JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> decoder + </H4> + <P CLASS="pudge-member-parent-link"> + <SMALL> + The JSONDecoder class is accessible via the + <A HREF="module-simplejson.html"> + <TT>simplejson</TT> + </A> module. + </SMALL> + </P> + <DIV ID="pudge-section-nav"> + <UL> + <LI> + + <SPAN CLASS="pudge-missing-section-link"> + Attributes + </SPAN> + </LI><LI> + <A HREF="#methods" CLASS="pudge-section-link"> + Methods (3) + </A> + + </LI> + + <LI> + <A HREF="simplejson/decoder.py.html?f=196&l=272#196" CLASS="pudge-section-link"> + Source + </A> + </LI> + </UL> + </DIV> + <DIV STYLE="clear: left"></DIV> + <DIV CLASS="rst pudge-module-doc"> + <P>Performs the following translations in decoding:</P> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="44%"> +<COL WIDTH="56%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">JSON</TH> +<TH CLASS="head">Python</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>object</TD> +<TD>dict</TD> +</TR> +<TR><TD>array</TD> +<TD>list</TD> +</TR> +<TR><TD>string</TD> +<TD>unicode</TD> +</TR> +<TR><TD>number (int)</TD> +<TD>int, long</TD> +</TR> +<TR><TD>number (real)</TD> +<TD>float</TD> +</TR> +<TR><TD>true</TD> +<TD>True</TD> +</TR> +<TR><TD>false</TD> +<TD>False</TD> +</TR> +<TR><TD>null</TD> +<TD>None</TD> +</TR> +</TBODY> +</TABLE> +<P>It also understands <TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, and <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT> as +their corresponding <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values, which is outside the JSON spec.</P> + + </DIV> + + <HR> + + + <A NAME="methods"></A> + <H2>Methods</H2> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + <TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> determines the encoding used to interpret any <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> +objects decoded by this instance (utf-8 by default). It has no +effect when decoding <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> objects. + </P> + <P>Note that currently only encodings that are a superset of ASCII work, +strings of other encodings should be passed in as <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT>.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT>, if specified, will be called with the result +of every JSON object decoded and its return value will be used in +place of the given <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> + + + </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 0x13244a0>)</TT> + <A HREF="simplejson/decoder.py.html?f=246&l=256#246" TITLE="View Source">...</A> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Return the Python representation of <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) + </P> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Decode a JSON document from <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> beginning +with a JSON document) and return a 2-tuple of the Python +representation and the index in <TT CLASS="docutils literal"><SPAN CLASS="pre">s</SPAN></TT> where the document ended. + </P> + <P>This can be used to decode a JSON document from a string that may +have extraneous data at the end.</P> + + + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simplejson/decoder.py.html?f=196&l=272#196" TITLE="simplejson/decoder.py:196">the source</A> + for more information. + </SMALL> + </P> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/class-simplejson.JSONEncoder.html b/src/simplejson/docs/class-simplejson.JSONEncoder.html new file mode 100644 index 00000000..1e429613 --- /dev/null +++ b/src/simplejson/docs/class-simplejson.JSONEncoder.html @@ -0,0 +1,261 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>simplejson.JSONEncoder -- Extensible JSON &lt;<a class="reference" href="http://json.org">http://json.org</a>&gt; encoder for Python data structures.</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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + + <A HREF="class-simplejson.JSONEncoder.html">details</A> + + <A HREF="class-simplejson.JSONEncoder-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>JSONEncoder</TT> + </H1> + <H4 CLASS="pudge-member-page-subheading"> + Extensible JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> encoder for Python data structures. + </H4> + <P CLASS="pudge-member-parent-link"> + <SMALL> + The JSONEncoder class is accessible via the + <A HREF="module-simplejson.html"> + <TT>simplejson</TT> + </A> module. + </SMALL> + </P> + <DIV ID="pudge-section-nav"> + <UL> + <LI> + + <SPAN CLASS="pudge-missing-section-link"> + Attributes + </SPAN> + </LI><LI> + <A HREF="#methods" CLASS="pudge-section-link"> + Methods (4) + </A> + + </LI> + + <LI> + <A HREF="simplejson/encoder.py.html?f=80&l=370#80" CLASS="pudge-section-link"> + Source + </A> + </LI> + </UL> + </DIV> + <DIV STYLE="clear: left"></DIV> + <DIV CLASS="rst pudge-module-doc"> + <P>Supports the following objects and types by default:</P> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="56%"> +<COL WIDTH="44%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">Python</TH> +<TH CLASS="head">JSON</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>dict</TD> +<TD>object</TD> +</TR> +<TR><TD>list, tuple</TD> +<TD>array</TD> +</TR> +<TR><TD>str, unicode</TD> +<TD>string</TD> +</TR> +<TR><TD>int, long, float</TD> +<TD>number</TD> +</TR> +<TR><TD>True</TD> +<TD>true</TD> +</TR> +<TR><TD>False</TD> +<TD>false</TD> +</TR> +<TR><TD>None</TD> +<TD>null</TD> +</TR> +</TBODY> +</TABLE> +<P>To extend this to recognize other objects, subclass and implement a +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method with another method that returns a serializable +object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT> if possible, otherwise it should call the superclass +implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).</P> + + </DIV> + + <HR> + + + <A NAME="methods"></A> + <H2>Methods</H2> + <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.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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Constructor for JSONEncoder, with sensible defaults. + </P> + <P>If skipkeys is False, then it is a TypeError to attempt +encoding of keys that are not str, int, long, float or None. If +skipkeys is True, such items are simply skipped.</P> +<P>If ensure_ascii is True, the output is guaranteed to be str +objects with all incoming unicode characters escaped. If +ensure_ascii is false, the output will be unicode object.</P> +<P>If check_circular is True, then lists, dicts, and custom encoded +objects will be checked for circular references during encoding to +prevent an infinite recursion (which would cause an OverflowError). +Otherwise, no such check takes place.</P> +<P>If allow_nan is True, then NaN, Infinity, and -Infinity will be +encoded as such. This behavior is not JSON specification compliant, +but is consistent with most JavaScript based encoders and decoders. +Otherwise, it will be a ValueError to encode such floats.</P> +<P>If sort_keys is True, then the output of dictionaries will be +sorted by key; this is useful for regression tests to ensure +that JSON serializations can be compared on a day-to-day basis.</P> +<P>If indent is a non-negative integer, then JSON array +elements and object members will be pretty-printed with that +indent level. An indent level of 0 will only insert newlines. +None is the most compact representation.</P> +<P>If specified, separators should be a (item_separator, key_separator) +tuple. The default is (', ', ': '). To get the most compact JSON +representation you should specify (',', ':') to eliminate whitespace.</P> +<P>If encoding is not None, then all input strings will be +transformed into unicode using that encoding prior to JSON-encoding. +The default is UTF-8.</P> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Implement this method in a subclass such that it returns +a serializable object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT>, or calls the base implementation +(to raise a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>). + </P> + <P>For example, to support arbitrary iterators, you could +implement default like this:</P> +<PRE CLASS="literal-block"> +def default(self, o): + try: + iterable = iter(o) + except TypeError: + pass + else: + return list(iterable) + return JSONEncoder.default(self, o) +</PRE> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Return a JSON string representation of a Python data structure. + </P> + <PRE CLASS="doctest-block"> +>>> JSONEncoder().encode({"foo": ["bar", "baz"]}) +'{"foo":["bar", "baz"]}' +</PRE> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Encode the given object and yield each string +representation as available. + </P> + <P>For example:</P> +<PRE CLASS="literal-block"> +for chunk in JSONEncoder().iterencode(bigobject): + mysocket.write(chunk) +</PRE> + + + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simplejson/encoder.py.html?f=80&l=370#80" TITLE="simplejson/encoder.py:80">the source</A> + for more information. + </SMALL> + </P> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/class_to_source.js b/src/simplejson/docs/class_to_source.js new file mode 100644 index 00000000..b6dc7651 --- /dev/null +++ b/src/simplejson/docs/class_to_source.js @@ -0,0 +1,22 @@ +(function () { + var href = document.location.href; + if (href.indexOf("file:") == 0 || !document.getElementsByTagName) return; + var _old_onload = window.onload; + window.onload = function () { + if (_old_onload) _old_onload.call(this); + _old_onload = null; + var anchors = document.getElementsByTagName('A'); + var class_re = /\blines-(\d+)-(\d+)\b/; + var hash_re = /#.*$/; + for (var i = 0; i < anchors.length; i++) { + var anchor = anchors[i]; + var found = anchor.className.match(class_re); + if (!found) continue; + href = anchor.href; + var hashidx = href.indexOf("#"); + if (hashidx == -1) hashidx = href.length; + anchor.href = (href.substring(0, hashidx) + "?f=" + found[1] + + "&l=" + found[2] + href.substring(hashidx, href.length)); + } + } +})(); diff --git a/src/simplejson/docs/index.html b/src/simplejson/docs/index.html new file mode 100644 index 00000000..c142f3ff --- /dev/null +++ b/src/simplejson/docs/index.html @@ -0,0 +1,455 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>simplejson -- A simple, fast, extensible JSON encoder and decoder</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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + + <A HREF="module-simplejson.html">details</A> + + <A HREF="module-simplejson-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>simplejson</TT> + </H1> + <H4 CLASS="pudge-member-page-subheading"> + A simple, fast, extensible JSON encoder and decoder + </H4> + + <DIV ID="pudge-section-nav"> + <UL> + <LI> + + <SPAN CLASS="pudge-missing-section-link"> + Attributes + </SPAN> + </LI><LI> + <A HREF="#functions" CLASS="pudge-section-link"> + Functions (4) + </A> + + </LI><LI> + <A HREF="#classes" CLASS="pudge-section-link"> + Classes (2) + </A> + + </LI><LI> + + <SPAN CLASS="pudge-missing-section-link"> + Modules + </SPAN> + </LI> + <LI> + <A HREF="module-simplejson-index.html" CLASS="pudge-section-link"> + Index + </A> + </LI> + <LI> + <A HREF="simplejson/__init__.py.html" CLASS="pudge-section-link"> + Source + </A> + </LI> + </UL> + </DIV> + <DIV STYLE="clear: left"></DIV> + <DIV CLASS="rst pudge-module-doc"> + <P>JSON (JavaScript Object Notation) <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format.</P> +<P>simplejson exposes an API familiar to uses of the standard library +marshal and pickle modules.</P> +<P>Encoding basic Python object hierarchies:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) +'["foo", {"bar": ["baz", null, 1.0, 2]}]' +>>> print simplejson.dumps("\"foo\bar") +"\"foo\bar" +>>> print simplejson.dumps(u'\u1234') +"\u1234" +>>> print simplejson.dumps('\\') +"\\" +>>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) +{"a": 0, "b": 0, "c": 0} +>>> from StringIO import StringIO +>>> io = StringIO() +>>> simplejson.dump(['streaming API'], io) +>>> io.getvalue() +'["streaming API"]' +</PRE> +<P>Compact encoding:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) +'[1,2,3,{"4":5,"6":7}]' +</PRE> +<P>Pretty printing:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) +{ + "4": 5, + "6": 7 +} +</PRE> +<P>Decoding JSON:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') +[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] +>>> simplejson.loads('"\\"foo\\bar"') +u'"foo\x08ar' +>>> from StringIO import StringIO +>>> io = StringIO('["streaming API"]') +>>> simplejson.load(io) +[u'streaming API'] +</PRE> +<P>Specializing JSON object decoding:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> def as_complex(dct): +... if '__complex__' in dct: +... return complex(dct['real'], dct['imag']) +... return dct +... +>>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', +... object_hook=as_complex) +(1+2j) +</PRE> +<P>Extending JSONEncoder:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> class ComplexEncoder(simplejson.JSONEncoder): +... def default(self, obj): +... if isinstance(obj, complex): +... return [obj.real, obj.imag] +... return simplejson.JSONEncoder.default(self, obj) +... +>>> dumps(2 + 1j, cls=ComplexEncoder) +'[2.0, 1.0]' +>>> ComplexEncoder().encode(2 + 1j) +'[2.0, 1.0]' +>>> list(ComplexEncoder().iterencode(2 + 1j)) +['[', '2.0', ', ', '1.0', ']'] +</PRE> +<P>Note that the JSON produced by this module's default settings +is a subset of YAML, so it may be used as a serializer for that as well.</P> + + </DIV> + + <HR> + + + <A NAME="functions"></A> + <H2>Functions</H2> + <DIV CLASS="pudge-member routine "> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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). + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">skipkeys</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">True</SPAN></TT> then <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT> keys that are not basic types +(<TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">int</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">long</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">bool</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT>) +will be skipped instead of raising a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">ensure_ascii</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the some chunks written to <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> +may be <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> instances, subject to normal Python <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> to +<TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> coercion rules. Unless <TT CLASS="docutils literal"><SPAN CLASS="pre">fp.write()</SPAN></TT> explicitly +understands <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> (as in <TT CLASS="docutils literal"><SPAN CLASS="pre">codecs.getwriter()</SPAN></TT>) this is likely +to cause an error.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">check_circular</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the circular reference check +for container types will be skipped and a circular reference will +result in an <TT CLASS="docutils literal"><SPAN CLASS="pre">OverflowError</SPAN></TT> (or worse).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">allow_nan</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then it will be a <TT CLASS="docutils literal"><SPAN CLASS="pre">ValueError</SPAN></TT> to +serialize out of range <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values (<TT CLASS="docutils literal"><SPAN CLASS="pre">nan</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">inf</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-inf</SPAN></TT>) +in strict compliance of the JSON specification, instead of using the +JavaScript equivalents (<TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT>).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">indent</SPAN></TT> is a non-negative integer, then JSON array elements and object +members will be pretty-printed with that indent level. An indent level +of 0 will only insert newlines. <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT> is the most compact representation.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">separators</SPAN></TT> is an <TT CLASS="docutils literal"><SPAN CLASS="pre">(item_separator,</SPAN> <SPAN CLASS="pre">dict_separator)</SPAN></TT> tuple +then it will be used instead of the default <TT CLASS="docutils literal"><SPAN CLASS="pre">(',</SPAN> <SPAN CLASS="pre">',</SPAN> <SPAN CLASS="pre">':</SPAN> <SPAN CLASS="pre">')</SPAN></TT> separators. +<TT CLASS="docutils literal"><SPAN CLASS="pre">(',',</SPAN> <SPAN CLASS="pre">':')</SPAN></TT> is the most compact JSON representation.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> is the character encoding for str instances, default is UTF-8.</P> +<P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONEncoder</SPAN></TT> subclass (e.g. one that overrides the +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method to serialize additional types), specify it with +the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine "> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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>. + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">skipkeys</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">True</SPAN></TT> then <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT> keys that are not basic types +(<TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">int</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">long</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">bool</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT>) +will be skipped instead of raising a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">ensure_ascii</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the return value will be a +<TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> instance subject to normal Python <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> to <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> +coercion rules instead of being escaped to an ASCII <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">check_circular</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the circular reference check +for container types will be skipped and a circular reference will +result in an <TT CLASS="docutils literal"><SPAN CLASS="pre">OverflowError</SPAN></TT> (or worse).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">allow_nan</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then it will be a <TT CLASS="docutils literal"><SPAN CLASS="pre">ValueError</SPAN></TT> to +serialize out of range <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values (<TT CLASS="docutils literal"><SPAN CLASS="pre">nan</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">inf</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-inf</SPAN></TT>) in +strict compliance of the JSON specification, instead of using the +JavaScript equivalents (<TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT>).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">indent</SPAN></TT> is a non-negative integer, then JSON array elements and +object members will be pretty-printed with that indent level. An indent +level of 0 will only insert newlines. <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT> is the most compact +representation.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">separators</SPAN></TT> is an <TT CLASS="docutils literal"><SPAN CLASS="pre">(item_separator,</SPAN> <SPAN CLASS="pre">dict_separator)</SPAN></TT> tuple +then it will be used instead of the default <TT CLASS="docutils literal"><SPAN CLASS="pre">(',</SPAN> <SPAN CLASS="pre">',</SPAN> <SPAN CLASS="pre">':</SPAN> <SPAN CLASS="pre">')</SPAN></TT> separators. +<TT CLASS="docutils literal"><SPAN CLASS="pre">(',',</SPAN> <SPAN CLASS="pre">':')</SPAN></TT> is the most compact JSON representation.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> is the character encoding for str instances, default is UTF-8.</P> +<P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONEncoder</SPAN></TT> subclass (e.g. one that overrides the +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method to serialize additional types), specify it with +the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> + + + </DIV> + </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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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. + </P> + <P>If the contents of <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> is encoded with an ASCII based encoding other +than utf-8 (e.g. latin-1), then an appropriate <TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> name must +be specified. Encodings that are not ASCII based (such as UCS-2) are +not allowed, and should be wrapped with +<TT CLASS="docutils literal"><SPAN CLASS="pre">codecs.getreader(fp)(encoding)</SPAN></TT>, or simply decoded to a <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> +object and passed to <TT CLASS="docutils literal"><SPAN CLASS="pre">loads()</SPAN></TT></P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> is an optional function that will be called with the +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>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> + + + </DIV> + </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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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. + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">s</SPAN></TT> is a <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> instance and is encoded with an ASCII based encoding +other than utf-8 (e.g. latin-1) then an appropriate <TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> name +must be specified. Encodings that are not ASCII based (such as UCS-2) +are not allowed and should be decoded to <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> first.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> is an optional function that will be called with the +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>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> + + + </DIV> + </DIV> + + + <A NAME="classes"></A> + <H2>Classes</H2> + <DIV CLASS="pudge-member class alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Extensible JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> encoder for Python data structures. + </P> + <P>Supports the following objects and types by default:</P> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="56%"> +<COL WIDTH="44%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">Python</TH> +<TH CLASS="head">JSON</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>dict</TD> +<TD>object</TD> +</TR> +<TR><TD>list, tuple</TD> +<TD>array</TD> +</TR> +<TR><TD>str, unicode</TD> +<TD>string</TD> +</TR> +<TR><TD>int, long, float</TD> +<TD>number</TD> +</TR> +<TR><TD>True</TD> +<TD>true</TD> +</TR> +<TR><TD>False</TD> +<TD>false</TD> +</TR> +<TR><TD>None</TD> +<TD>null</TD> +</TR> +</TBODY> +</TABLE> +<P>To extend this to recognize other objects, subclass and implement a +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method with another method that returns a serializable +object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT> if possible, otherwise it should call the superclass +implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).</P> + + + + <P CLASS="note"> + This class contains <A HREF="class-simplejson.JSONEncoder.html#members"> + 5 members</A>. + </P> + </DIV> + </DIV><DIV CLASS="pudge-member class alias"> + <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> + </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> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="44%"> +<COL WIDTH="56%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">JSON</TH> +<TH CLASS="head">Python</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>object</TD> +<TD>dict</TD> +</TR> +<TR><TD>array</TD> +<TD>list</TD> +</TR> +<TR><TD>string</TD> +<TD>unicode</TD> +</TR> +<TR><TD>number (int)</TD> +<TD>int, long</TD> +</TR> +<TR><TD>number (real)</TD> +<TD>float</TD> +</TR> +<TR><TD>true</TD> +<TD>True</TD> +</TR> +<TR><TD>false</TD> +<TD>False</TD> +</TR> +<TR><TD>null</TD> +<TD>None</TD> +</TR> +</TBODY> +</TABLE> +<P>It also understands <TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, and <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT> as +their corresponding <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values, which is outside the JSON spec.</P> + + + + <P CLASS="note"> + This class contains <A HREF="class-simplejson.JSONDecoder.html#members"> + 4 members</A>. + </P> + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simplejson/__init__.py.html" TITLE="simplejson/__init__.py:0">the source</A> + for more information. + </SMALL> + </P> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/layout.css b/src/simplejson/docs/layout.css new file mode 100644 index 00000000..a45c58b3 --- /dev/null +++ b/src/simplejson/docs/layout.css @@ -0,0 +1,208 @@ +@import url("pudge.css"); + +/* Basic Layout +---------------------------------- */ + +body { + margin-left: 1em; + margin-right: 1em; + max-width: 50em; +} +body { + font-size: .80em; + color: #111; +} +div#main-content { + margin-left: 1em; + margin-right: 1em; + max-width: 47em; +} + +/* Top Navigation +----------------------------------- */ + +div#top-nav { + background: #373; + padding: .3em .3em; + margin: 0; + margin-bottom: 1.5em; + font-size: 90%; +} +div#top-nav #doc-title { + font-size: 140%; + font-weight: bold; + margin: 0; + padding-top: .32em; + padding-right: 1em; + padding-left: .3em; + color: #9c9; + float: left; +} +div#top-nav a { + color: #6a6; + text-decoration: none; +} +div#top-nav .online-navigation a:hover, +div#top-nav h1 a +{ + color: #9c9; +} + +/* Footer +----------------------------------- */ + +div#footer { + text-align: right; + margin-top: 1.5em; + border-top: 1px solid #373; + padding-top: .5em; + font-size: 80%; + color: #666; +} +div#footer a { + color: #373; + text-decoration: none; +} +div#footer p { + margin: .2em 1em; +} + +/* Basic Style +----------------------------------- */ +h1, h2, h3, h4 { + margin: 1em auto; + font-family: 'Trebuchet MS', 'Verdana', Sans-serif; + color: #555; + font-weight: normal; +} +h1 { font-size: 200% } +h2 { font-size: 170% } +h3 { font-size: 150% } +h4 { font-size: 120% } +a:link { color: #060; font-weight: bold } +a:visited { color: #060; text-decoration: none } +hr { margin: auto 12px } +pre { color: #036 } + +dl dt { + font-style: italic; + margin-top: .5em; + font-weight: bold; + color: #555; +} +hr { + color: #373; + background-color: #373; + height: 1px; + border: 0; + width: 100%; + margin: 2em 0; +} + +/* Pudge Elements +--------------------------------- */ + +h1.pudge-member-page-heading { + font-size: 300%; + margin-top: .4em; + margin-bottom: .4em; +} +h4.pudge-member-page-subheading { + font-size: 150%; + font-style: italic; + margin-top: -1.3em; + margin-left: 2em; + color: #999; +} +p.pudge-member-blurb { + font-style: italic; + font-weight: bold; + font-size: 120%; + margin-top: 0.2em; + color: #6a6; +} +div.pudge-module-doc { + max-width: 45em; +} +div.pudge-section { + margin-left: 2em; + max-width: 45em; +} +p.pudge-member-blurb { + font-style: italic; + font-weight: bold; + font-size: 120%; +} + +/* Section Navigation +----------------------------------- */ + +div#pudge-section-nav +{ + margin: 1em 0 1.5em 0; + padding: 0; + height: 20px; +} + +div#pudge-section-nav ul { + border: 0; + margin: 0; + padding: 0; + list-style-type: none; + text-align: center; + border-right: 1px solid #aaa; +} +div#pudge-section-nav ul li +{ + display: block; + float: left; + text-align: center; + padding: 0; + margin: 0; +} + +div#pudge-section-nav ul li .pudge-section-link, +div#pudge-section-nav ul li .pudge-missing-section-link +{ + background: #aaa; + width: 11em; + height: 1.8em; + border: 0; + border-right: 3px solid #fff; + padding: 0; + margin: 0 0 10px 0; + color: #ddd; + text-decoration: none; + display: block; + text-align: center; + font: normal 10px/18px "Lucida Grande", "Lucida Sans Unicode", verdana, lucida, sans-serif; + font-weight: bold; + cursor: hand; +} + +div#pudge-section-nav ul li a:hover +{ + color: #fff; + background: #393; +} + +div#pudge-section-nav ul li .pudge-section-link +{ + background: #373; + color: #9c9; +} + +/* Module Lists +----------------------------------- */ +dl.pudge-module-list dt { + font-style: normal; + font-size: 110%; +} +dl.pudge-module-list dd { + color: #555; +} + +/* misc */ +pre, tt { + font-size: 120%; +} diff --git a/src/simplejson/docs/module-index.html b/src/simplejson/docs/module-index.html new file mode 100644 index 00000000..5e92fca1 --- /dev/null +++ b/src/simplejson/docs/module-index.html @@ -0,0 +1,111 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>simplejson 1.7.1 -- 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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + simplejson 1.7.1 + </H1> + <H4 CLASS="pudge-member-page-subheading"> + Module Reference + </H4> + + <H2>Packages and Modules</H2> + + <DL CLASS="pudge-module-list"> + + <DT><TT><A HREF="module-simplejson.html" CLASS="pudge-obj-link"> + simplejson + </A></TT> + </DT> + <DD> + A simple, fast, extensible JSON encoder and decoder + </DD> + + <DT><TT><A HREF="module-simplejson.encoder.html" CLASS="pudge-obj-link"> + simplejson.encoder + </A></TT> + </DT> + <DD> + Implementation of JSONEncoder + </DD> + + <DT><TT><A HREF="module-simplejson.jsonfilter.html" CLASS="pudge-obj-link"> + simplejson.jsonfilter + </A></TT> + </DT> + + + <DT><TT><A HREF="module-simplejson.tests.html" CLASS="pudge-obj-link"> + simplejson.tests + </A></TT> + </DT> + + + <DT><TT><A HREF="module-simplejson.decoder.html" CLASS="pudge-obj-link"> + simplejson.decoder + </A></TT> + </DT> + <DD> + 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> + </DT> + + + </DL> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/module-simplejson-index.html b/src/simplejson/docs/module-simplejson-index.html new file mode 100644 index 00000000..935e0269 --- /dev/null +++ b/src/simplejson/docs/module-simplejson-index.html @@ -0,0 +1,127 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>Index of simplejson module</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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + + <A HREF="module-simplejson.html">details</A> + + <A HREF="module-simplejson-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1>Index of the <TT>simplejson</TT> module</H1> + + <UL CLASS="pudge-module-index"> + <LI CLASS="module"> + <SPAN CLASS="prefix">m</SPAN> + <A HREF="module-simplejson.html"> + <TT>simplejson</TT> + </A> + <A HREF="simplejson/__init__.py.html">...</A> + <SMALL> + - <SPAN CLASS="rst">A simple, fast, extensible JSON encoder and decoder</SPAN> + </SMALL> + <UL> + + + + + + <LI CLASS="routine"> + <SPAN CLASS="prefix">f</SPAN> + <A HREF="module-simplejson.html#load"> + <TT>load</TT> + </A> + <A HREF="simplejson/__init__.py.html?f=220&l=242#220">...</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> + </SMALL> + + </LI><LI CLASS="routine"> + <SPAN CLASS="prefix">f</SPAN> + <A HREF="module-simplejson.html#dump"> + <TT>dump</TT> + </A> + <A HREF="simplejson/__init__.py.html?f=108&l=164#108">...</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> + </SMALL> + + </LI><LI CLASS="routine"> + <SPAN CLASS="prefix">f</SPAN> + <A HREF="module-simplejson.html#dumps"> + <TT>dumps</TT> + </A> + <A HREF="simplejson/__init__.py.html?f=166&l=217#166">...</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> + + </LI><LI CLASS="routine"> + <SPAN CLASS="prefix">f</SPAN> + <A HREF="module-simplejson.html#loads"> + <TT>loads</TT> + </A> + <A HREF="simplejson/__init__.py.html?f=243&l=268#243">...</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> + </SMALL> + + </LI> + + + + </UL> + </LI> + </UL> + + + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/module-simplejson.html b/src/simplejson/docs/module-simplejson.html new file mode 100644 index 00000000..c142f3ff --- /dev/null +++ b/src/simplejson/docs/module-simplejson.html @@ -0,0 +1,455 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> +<HTML> + + <HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type"> + <TITLE>simplejson -- A simple, fast, extensible JSON encoder and decoder</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.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + + <BR> + + <A HREF="module-simplejson.html" TITLE="simplejson reference"> + simplejson + </A> + + + + <A HREF="module-simplejson.html">details</A> + + <A HREF="module-simplejson-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>simplejson</TT> + </H1> + <H4 CLASS="pudge-member-page-subheading"> + A simple, fast, extensible JSON encoder and decoder + </H4> + + <DIV ID="pudge-section-nav"> + <UL> + <LI> + + <SPAN CLASS="pudge-missing-section-link"> + Attributes + </SPAN> + </LI><LI> + <A HREF="#functions" CLASS="pudge-section-link"> + Functions (4) + </A> + + </LI><LI> + <A HREF="#classes" CLASS="pudge-section-link"> + Classes (2) + </A> + + </LI><LI> + + <SPAN CLASS="pudge-missing-section-link"> + Modules + </SPAN> + </LI> + <LI> + <A HREF="module-simplejson-index.html" CLASS="pudge-section-link"> + Index + </A> + </LI> + <LI> + <A HREF="simplejson/__init__.py.html" CLASS="pudge-section-link"> + Source + </A> + </LI> + </UL> + </DIV> + <DIV STYLE="clear: left"></DIV> + <DIV CLASS="rst pudge-module-doc"> + <P>JSON (JavaScript Object Notation) <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format.</P> +<P>simplejson exposes an API familiar to uses of the standard library +marshal and pickle modules.</P> +<P>Encoding basic Python object hierarchies:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) +'["foo", {"bar": ["baz", null, 1.0, 2]}]' +>>> print simplejson.dumps("\"foo\bar") +"\"foo\bar" +>>> print simplejson.dumps(u'\u1234') +"\u1234" +>>> print simplejson.dumps('\\') +"\\" +>>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) +{"a": 0, "b": 0, "c": 0} +>>> from StringIO import StringIO +>>> io = StringIO() +>>> simplejson.dump(['streaming API'], io) +>>> io.getvalue() +'["streaming API"]' +</PRE> +<P>Compact encoding:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) +'[1,2,3,{"4":5,"6":7}]' +</PRE> +<P>Pretty printing:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) +{ + "4": 5, + "6": 7 +} +</PRE> +<P>Decoding JSON:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') +[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] +>>> simplejson.loads('"\\"foo\\bar"') +u'"foo\x08ar' +>>> from StringIO import StringIO +>>> io = StringIO('["streaming API"]') +>>> simplejson.load(io) +[u'streaming API'] +</PRE> +<P>Specializing JSON object decoding:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> def as_complex(dct): +... if '__complex__' in dct: +... return complex(dct['real'], dct['imag']) +... return dct +... +>>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', +... object_hook=as_complex) +(1+2j) +</PRE> +<P>Extending JSONEncoder:</P> +<PRE CLASS="literal-block"> +>>> import simplejson +>>> class ComplexEncoder(simplejson.JSONEncoder): +... def default(self, obj): +... if isinstance(obj, complex): +... return [obj.real, obj.imag] +... return simplejson.JSONEncoder.default(self, obj) +... +>>> dumps(2 + 1j, cls=ComplexEncoder) +'[2.0, 1.0]' +>>> ComplexEncoder().encode(2 + 1j) +'[2.0, 1.0]' +>>> list(ComplexEncoder().iterencode(2 + 1j)) +['[', '2.0', ', ', '1.0', ']'] +</PRE> +<P>Note that the JSON produced by this module's default settings +is a subset of YAML, so it may be used as a serializer for that as well.</P> + + </DIV> + + <HR> + + + <A NAME="functions"></A> + <H2>Functions</H2> + <DIV CLASS="pudge-member routine "> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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). + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">skipkeys</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">True</SPAN></TT> then <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT> keys that are not basic types +(<TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">int</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">long</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">bool</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT>) +will be skipped instead of raising a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">ensure_ascii</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the some chunks written to <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> +may be <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> instances, subject to normal Python <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> to +<TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> coercion rules. Unless <TT CLASS="docutils literal"><SPAN CLASS="pre">fp.write()</SPAN></TT> explicitly +understands <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> (as in <TT CLASS="docutils literal"><SPAN CLASS="pre">codecs.getwriter()</SPAN></TT>) this is likely +to cause an error.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">check_circular</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the circular reference check +for container types will be skipped and a circular reference will +result in an <TT CLASS="docutils literal"><SPAN CLASS="pre">OverflowError</SPAN></TT> (or worse).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">allow_nan</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then it will be a <TT CLASS="docutils literal"><SPAN CLASS="pre">ValueError</SPAN></TT> to +serialize out of range <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values (<TT CLASS="docutils literal"><SPAN CLASS="pre">nan</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">inf</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-inf</SPAN></TT>) +in strict compliance of the JSON specification, instead of using the +JavaScript equivalents (<TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT>).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">indent</SPAN></TT> is a non-negative integer, then JSON array elements and object +members will be pretty-printed with that indent level. An indent level +of 0 will only insert newlines. <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT> is the most compact representation.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">separators</SPAN></TT> is an <TT CLASS="docutils literal"><SPAN CLASS="pre">(item_separator,</SPAN> <SPAN CLASS="pre">dict_separator)</SPAN></TT> tuple +then it will be used instead of the default <TT CLASS="docutils literal"><SPAN CLASS="pre">(',</SPAN> <SPAN CLASS="pre">',</SPAN> <SPAN CLASS="pre">':</SPAN> <SPAN CLASS="pre">')</SPAN></TT> separators. +<TT CLASS="docutils literal"><SPAN CLASS="pre">(',',</SPAN> <SPAN CLASS="pre">':')</SPAN></TT> is the most compact JSON representation.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> is the character encoding for str instances, default is UTF-8.</P> +<P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONEncoder</SPAN></TT> subclass (e.g. one that overrides the +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method to serialize additional types), specify it with +the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> + + + </DIV> + </DIV><DIV CLASS="pudge-member routine "> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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>. + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">skipkeys</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">True</SPAN></TT> then <TT CLASS="docutils literal"><SPAN CLASS="pre">dict</SPAN></TT> keys that are not basic types +(<TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">int</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">long</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">bool</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT>) +will be skipped instead of raising a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">ensure_ascii</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the return value will be a +<TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> instance subject to normal Python <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> to <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> +coercion rules instead of being escaped to an ASCII <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT>.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">check_circular</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then the circular reference check +for container types will be skipped and a circular reference will +result in an <TT CLASS="docutils literal"><SPAN CLASS="pre">OverflowError</SPAN></TT> (or worse).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">allow_nan</SPAN></TT> is <TT CLASS="docutils literal"><SPAN CLASS="pre">False</SPAN></TT>, then it will be a <TT CLASS="docutils literal"><SPAN CLASS="pre">ValueError</SPAN></TT> to +serialize out of range <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values (<TT CLASS="docutils literal"><SPAN CLASS="pre">nan</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">inf</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-inf</SPAN></TT>) in +strict compliance of the JSON specification, instead of using the +JavaScript equivalents (<TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT>).</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">indent</SPAN></TT> is a non-negative integer, then JSON array elements and +object members will be pretty-printed with that indent level. An indent +level of 0 will only insert newlines. <TT CLASS="docutils literal"><SPAN CLASS="pre">None</SPAN></TT> is the most compact +representation.</P> +<P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">separators</SPAN></TT> is an <TT CLASS="docutils literal"><SPAN CLASS="pre">(item_separator,</SPAN> <SPAN CLASS="pre">dict_separator)</SPAN></TT> tuple +then it will be used instead of the default <TT CLASS="docutils literal"><SPAN CLASS="pre">(',</SPAN> <SPAN CLASS="pre">',</SPAN> <SPAN CLASS="pre">':</SPAN> <SPAN CLASS="pre">')</SPAN></TT> separators. +<TT CLASS="docutils literal"><SPAN CLASS="pre">(',',</SPAN> <SPAN CLASS="pre">':')</SPAN></TT> is the most compact JSON representation.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> is the character encoding for str instances, default is UTF-8.</P> +<P>To use a custom <TT CLASS="docutils literal"><SPAN CLASS="pre">JSONEncoder</SPAN></TT> subclass (e.g. one that overrides the +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method to serialize additional types), specify it with +the <TT CLASS="docutils literal"><SPAN CLASS="pre">cls</SPAN></TT> kwarg.</P> + + + </DIV> + </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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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. + </P> + <P>If the contents of <TT CLASS="docutils literal"><SPAN CLASS="pre">fp</SPAN></TT> is encoded with an ASCII based encoding other +than utf-8 (e.g. latin-1), then an appropriate <TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> name must +be specified. Encodings that are not ASCII based (such as UCS-2) are +not allowed, and should be wrapped with +<TT CLASS="docutils literal"><SPAN CLASS="pre">codecs.getreader(fp)(encoding)</SPAN></TT>, or simply decoded to a <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> +object and passed to <TT CLASS="docutils literal"><SPAN CLASS="pre">loads()</SPAN></TT></P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> is an optional function that will be called with the +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>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> + + + </DIV> + </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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + 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. + </P> + <P>If <TT CLASS="docutils literal"><SPAN CLASS="pre">s</SPAN></TT> is a <TT CLASS="docutils literal"><SPAN CLASS="pre">str</SPAN></TT> instance and is encoded with an ASCII based encoding +other than utf-8 (e.g. latin-1) then an appropriate <TT CLASS="docutils literal"><SPAN CLASS="pre">encoding</SPAN></TT> name +must be specified. Encodings that are not ASCII based (such as UCS-2) +are not allowed and should be decoded to <TT CLASS="docutils literal"><SPAN CLASS="pre">unicode</SPAN></TT> first.</P> +<P><TT CLASS="docutils literal"><SPAN CLASS="pre">object_hook</SPAN></TT> is an optional function that will be called with the +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>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> + + + </DIV> + </DIV> + + + <A NAME="classes"></A> + <H2>Classes</H2> + <DIV CLASS="pudge-member class alias"> + <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> + </H4> + <DIV CLASS="pudge-section rst"> + + <P CLASS="pudge-member-blurb"> + Extensible JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> encoder for Python data structures. + </P> + <P>Supports the following objects and types by default:</P> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="56%"> +<COL WIDTH="44%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">Python</TH> +<TH CLASS="head">JSON</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>dict</TD> +<TD>object</TD> +</TR> +<TR><TD>list, tuple</TD> +<TD>array</TD> +</TR> +<TR><TD>str, unicode</TD> +<TD>string</TD> +</TR> +<TR><TD>int, long, float</TD> +<TD>number</TD> +</TR> +<TR><TD>True</TD> +<TD>true</TD> +</TR> +<TR><TD>False</TD> +<TD>false</TD> +</TR> +<TR><TD>None</TD> +<TD>null</TD> +</TR> +</TBODY> +</TABLE> +<P>To extend this to recognize other objects, subclass and implement a +<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method with another method that returns a serializable +object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT> if possible, otherwise it should call the superclass +implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).</P> + + + + <P CLASS="note"> + This class contains <A HREF="class-simplejson.JSONEncoder.html#members"> + 5 members</A>. + </P> + </DIV> + </DIV><DIV CLASS="pudge-member class alias"> + <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> + </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> +<TABLE BORDER="1" CLASS="docutils"> +<COLGROUP> +<COL WIDTH="44%"> +<COL WIDTH="56%"> +</COLGROUP> +<THEAD VALIGN="bottom"> +<TR><TH CLASS="head">JSON</TH> +<TH CLASS="head">Python</TH> +</TR> +</THEAD> +<TBODY VALIGN="top"> +<TR><TD>object</TD> +<TD>dict</TD> +</TR> +<TR><TD>array</TD> +<TD>list</TD> +</TR> +<TR><TD>string</TD> +<TD>unicode</TD> +</TR> +<TR><TD>number (int)</TD> +<TD>int, long</TD> +</TR> +<TR><TD>number (real)</TD> +<TD>float</TD> +</TR> +<TR><TD>true</TD> +<TD>True</TD> +</TR> +<TR><TD>false</TD> +<TD>False</TD> +</TR> +<TR><TD>null</TD> +<TD>None</TD> +</TR> +</TBODY> +</TABLE> +<P>It also understands <TT CLASS="docutils literal"><SPAN CLASS="pre">NaN</SPAN></TT>, <TT CLASS="docutils literal"><SPAN CLASS="pre">Infinity</SPAN></TT>, and <TT CLASS="docutils literal"><SPAN CLASS="pre">-Infinity</SPAN></TT> as +their corresponding <TT CLASS="docutils literal"><SPAN CLASS="pre">float</SPAN></TT> values, which is outside the JSON spec.</P> + + + + <P CLASS="note"> + This class contains <A HREF="class-simplejson.JSONDecoder.html#members"> + 4 members</A>. + </P> + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simplejson/__init__.py.html" TITLE="simplejson/__init__.py:0">the source</A> + for more information. + </SMALL> + </P> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1.3</A> + </P> + + + </DIV> + </DIV> + </BODY> + +</HTML> \ No newline at end of file diff --git a/src/simplejson/docs/pudge.css b/src/simplejson/docs/pudge.css new file mode 100644 index 00000000..b3702171 --- /dev/null +++ b/src/simplejson/docs/pudge.css @@ -0,0 +1,60 @@ +/* Layout +----------------------------------- */ + +@import url("rst.css"); + +/* Pudge Elements +----------------------------------- */ +.note { font-size: 90% } +h4.pudge-member-name { + font-size: 110%; + margin-bottom: 0; +} +h4.pudge-member-name a.obj-link { + font-weight: bold; + text-decoration: none; +} +h4.pudge-member-name .prefix { + font-style: oblique; + padding-right: 6px; + font-weight: bold; + color: #c9c; +} +h1.pudge-member-page-heading { + font-size: 250%; +} +h4.pudge-member-page-subheading { + font-size: 150%; + font-style: italic; +} +h4.pudge-member-page-subheading p { + display: inline; +} +div.pudge-member { + margin-top: 1.5em; + margin-bottom: 1.5em; +} +ul.pudge-module-index { + margin-left: 0; + padding-left: 0; +} +ul.pudge-module-index ul { + padding-left: 1.5em; + margin-left: 0; +} +ul.pudge-module-index li { + list-style-type: none; +} +ul.pudge-module-index .prefix { + font-style: oblique; + padding-right: 6px; + font-size: 90%; + font-weight: bold; + color: purple; +} +ul.pudge-module-index a { + text-decoration: none; +} +div.pudge-section { + margin-left: 2em; +} \ No newline at end of file diff --git a/src/simplejson/docs/rst.css b/src/simplejson/docs/rst.css new file mode 100644 index 00000000..f48c7232 --- /dev/null +++ b/src/simplejson/docs/rst.css @@ -0,0 +1,142 @@ +/* Headings + ------------------------------- */ + +.rst h1 { font-size: 110% } +.rst h2 { font-size: 100% } + +/*.rst-doc h1 { font-size: 200% } +.rst-doc h2 { font-size: 140% } +.rst-doc h3 { font-size: 110% }*/ +.rst-doc h1.title { font-size: 220% } +.rst-doc h1 a, .rst-doc h2 a, .rst-doc h3 a { + color: inherit ; + font-weight: inherit; + text-decoration: inherit; +} + +/* Blockquotes + ------------------------------- */ + +.rst blockquote, +.rst-doc blockquote { + font-style: italic; + font-family: Georgia, serif; + max-width: 30em; +} + +/* Document Info + ------------------------------- */ +.rst-doc table.docinfo { + margin: 2em 0 + width: 100%; +} +.rst-doc table.docinfo th { + text-align: left ; + padding-right: 2em; + color: #555; +} +.rst-doc table.docinfo td { + text-align: left +} + +/* Field Lists + ------------------------------- */ + +.rst table.field-list, +.rst-doc table.field-list { + border: 0; + margin-left: 0; +} +.rst table.field-list ul, +.rst-doc table.field-list ul { + margin-left: 0; +} +.rst-doc table.field-list ul { + border: 0; + margin-left: 2em; + background: #fff; + color: #119; +} + +/* Tables + ------------------------------- */ + +.rst table.docutils, +.rst-doc table.docutils { + border: 0; +} +.rst table.docutils th, +.rst-doc table.docutils th { + border: 0; + background: #777; + color: #fff; + padding: 3px; +} +.rst table.docutils td, +.rst-doc table.docutils td { + border: 0; + border-bottom: 1px solid #ccc; + padding-bottom: 2px; +} + +/* Contents and Back References + ------------------------------- */ + +.rst-doc div.contents { + margin: 2em inherit; +} +.rst-doc div.contents ul { + margin-left: 0; + padding-left: 2em; + line-height: 150%; +} +.rst-doc div.contents ul li { + font-weight: bold; + list-style-type: none; +} +.rst-doc div.contents p.topic-title { + font-size: 160%; + font-weight: normal; + color: #555; + margin-top: .5em; +} +.rst-doc .contents .reference, +.rst-doc .toc-backref { + text-decoration: none; +} + +/* Admonitions + ------------------------------- */ + +.rst-doc div.admonition, +.rst-doc div.warning, +.rst-doc div.note { + margin: 2.5em 6em; + padding: .6em 2.5em; + background: #ddf; + border: 2px solid #ccc; + font-family: Georgia, serif; + color: #333; +} +.rst-doc div.warning { + background: #ff8; + border-color: #fe2; +} +.rst-doc div .admonition-title { + font-weight: bold; + font-style: italic; + color: #223; + font-size: 110%; + font-family: sans-serif; +} + +/* Misc + ------------------------------- */ + +tt.literal { + color: #333; +} +.footnote-reference { + vertical-align: super; + font-size: 20%; +} \ No newline at end of file diff --git a/src/simplejson/docs/simplejson/__init__.py.html b/src/simplejson/docs/simplejson/__init__.py.html new file mode 100644 index 00000000..fb67bda2 --- /dev/null +++ b/src/simplejson/docs/simplejson/__init__.py.html @@ -0,0 +1,357 @@ +<html><head><title>/Users/bob/src/simplejson/simplejson/__init__.py</title> + <script type="text/javascript"><!-- + +function show_line_range() { + var href = document.location.href; + if (href.indexOf('?') == -1) { + return; + } + var qs = href.substring(href.indexOf('?')+1); + if (qs.indexOf('#') >= 0) { + qs = qs.substring(0, qs.indexOf('#')); + } + var first = qs.match(/f=(\d+)/)[1]; + var last = qs.match(/l=(\d+)/)[1]; + if (! first || ! last) { + return; + } + var anchors = document.getElementsByTagName('A'); + var container = document.createElement('DIV'); + container.className = 'highlighted'; + var children = []; + var start = null; + var parent = null; + var highlight = false; + for (var i = 0; i < anchors.length; i++) { + var el = anchors[i]; + if (el.getAttribute('name') == first) { + start = el.previousSibling; + parent = el.parentNode; + highlight = true; + } + if (el.getAttribute('name') == last) { + break; + } + if (highlight) { + children[children.length] = el; + el = el.nextSibling; + while (el && el.tagName != 'A') { + children[children.length] = el; + el = el.nextSibling; + } + } + } + for (i=0; i<children.length; i++) { + container.appendChild(children[i]); + } + if (start) { + start.parentNode.insertBefore(container, start.nextSibling); + } else { + parent.insertBefore(container, parent.childNodes[0]); + } +} + + // --></script> + <style> + div.python { + color: #333 + } + div.python a.lnum { + color: #555; + background-color: #eee; + border-right: 1px solid #999; + padding-right: 2px; + margin-right: 4px; + } + div.python span.comment { color: #933 } + div.python span.keyword { color: #a3e; font-weight: bold } + div.python span.op { color: #c96 } + div.python span.string { color: #6a6 } + div.python span.name { } + div.python span.text { color: #333 } + div.highlighted { background-color: #ff9; border: 1px solid #009 } + </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="string">r"""</span><br /> +<a class="lnum" href="#2" name="2">0002</a><span class="string">A simple, fast, extensible JSON encoder and decoder</span><br /> +<a class="lnum" href="#3" name="3">0003</a><span class="string"></span><br /> +<a class="lnum" href="#4" name="4">0004</a><span class="string">JSON (JavaScript Object Notation) <http://json.org> is a subset of</span><br /> +<a class="lnum" href="#5" name="5">0005</a><span class="string">JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data</span><br /> +<a class="lnum" href="#6" name="6">0006</a><span class="string">interchange format.</span><br /> +<a class="lnum" href="#7" name="7">0007</a><span class="string"></span><br /> +<a class="lnum" href="#8" name="8">0008</a><span class="string">simplejson exposes an API familiar to uses of the standard library</span><br /> +<a class="lnum" href="#9" name="9">0009</a><span class="string">marshal and pickle modules.</span><br /> +<a class="lnum" href="#10" name="10">0010</a><span class="string"></span><br /> +<a class="lnum" href="#11" name="11">0011</a><span class="string">Encoding basic Python object hierarchies::</span><br /> +<a class="lnum" href="#12" name="12">0012</a><span class="string">    </span><br /> +<a class="lnum" href="#13" name="13">0013</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#14" name="14">0014</a><span class="string">    >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])</span><br /> +<a class="lnum" href="#15" name="15">0015</a><span class="string">    '["foo", {"bar": ["baz", null, 1.0, 2]}]'</span><br /> +<a class="lnum" href="#16" name="16">0016</a><span class="string">    >>> print simplejson.dumps("\"foo\bar")</span><br /> +<a class="lnum" href="#17" name="17">0017</a><span class="string">    "\"foo\bar"</span><br /> +<a class="lnum" href="#18" name="18">0018</a><span class="string">    >>> print simplejson.dumps(u'\u1234')</span><br /> +<a class="lnum" href="#19" name="19">0019</a><span class="string">    "\u1234"</span><br /> +<a class="lnum" href="#20" name="20">0020</a><span class="string">    >>> print simplejson.dumps('\\')</span><br /> +<a class="lnum" href="#21" name="21">0021</a><span class="string">    "\\"</span><br /> +<a class="lnum" href="#22" name="22">0022</a><span class="string">    >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)</span><br /> +<a class="lnum" href="#23" name="23">0023</a><span class="string">    {"a": 0, "b": 0, "c": 0}</span><br /> +<a class="lnum" href="#24" name="24">0024</a><span class="string">    >>> from StringIO import StringIO</span><br /> +<a class="lnum" href="#25" name="25">0025</a><span class="string">    >>> io = StringIO()</span><br /> +<a class="lnum" href="#26" name="26">0026</a><span class="string">    >>> simplejson.dump(['streaming API'], io)</span><br /> +<a class="lnum" href="#27" name="27">0027</a><span class="string">    >>> io.getvalue()</span><br /> +<a class="lnum" href="#28" name="28">0028</a><span class="string">    '["streaming API"]'</span><br /> +<a class="lnum" href="#29" name="29">0029</a><span class="string"></span><br /> +<a class="lnum" href="#30" name="30">0030</a><span class="string">Compact encoding::</span><br /> +<a class="lnum" href="#31" name="31">0031</a><span class="string"></span><br /> +<a class="lnum" href="#32" name="32">0032</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#33" name="33">0033</a><span class="string">    >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))</span><br /> +<a class="lnum" href="#34" name="34">0034</a><span class="string">    '[1,2,3,{"4":5,"6":7}]'</span><br /> +<a class="lnum" href="#35" name="35">0035</a><span class="string"></span><br /> +<a class="lnum" href="#36" name="36">0036</a><span class="string">Pretty printing::</span><br /> +<a class="lnum" href="#37" name="37">0037</a><span class="string"></span><br /> +<a class="lnum" href="#38" name="38">0038</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#39" name="39">0039</a><span class="string">    >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)</span><br /> +<a class="lnum" href="#40" name="40">0040</a><span class="string">    {</span><br /> +<a class="lnum" href="#41" name="41">0041</a><span class="string">        "4": 5, </span><br /> +<a class="lnum" href="#42" name="42">0042</a><span class="string">        "6": 7</span><br /> +<a class="lnum" href="#43" name="43">0043</a><span class="string">    }</span><br /> +<a class="lnum" href="#44" name="44">0044</a><span class="string"></span><br /> +<a class="lnum" href="#45" name="45">0045</a><span class="string">Decoding JSON::</span><br /> +<a class="lnum" href="#46" name="46">0046</a><span class="string">    </span><br /> +<a class="lnum" href="#47" name="47">0047</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#48" name="48">0048</a><span class="string">    >>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')</span><br /> +<a class="lnum" href="#49" name="49">0049</a><span class="string">    [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]</span><br /> +<a class="lnum" href="#50" name="50">0050</a><span class="string">    >>> simplejson.loads('"\\"foo\\bar"')</span><br /> +<a class="lnum" href="#51" name="51">0051</a><span class="string">    u'"foo\x08ar'</span><br /> +<a class="lnum" href="#52" name="52">0052</a><span class="string">    >>> from StringIO import StringIO</span><br /> +<a class="lnum" href="#53" name="53">0053</a><span class="string">    >>> io = StringIO('["streaming API"]')</span><br /> +<a class="lnum" href="#54" name="54">0054</a><span class="string">    >>> simplejson.load(io)</span><br /> +<a class="lnum" href="#55" name="55">0055</a><span class="string">    [u'streaming API']</span><br /> +<a class="lnum" href="#56" name="56">0056</a><span class="string"></span><br /> +<a class="lnum" href="#57" name="57">0057</a><span class="string">Specializing JSON object decoding::</span><br /> +<a class="lnum" href="#58" name="58">0058</a><span class="string"></span><br /> +<a class="lnum" href="#59" name="59">0059</a><span class="string">    >>> import simplejson</span><br /> +<a class="lnum" href="#60" name="60">0060</a><span class="string">    >>> def as_complex(dct):</span><br /> +<a class="lnum" href="#61" name="61">0061</a><span class="string">    ...     if '__complex__' in dct:</span><br /> +<a class="lnum" href="#62" name="62">0062</a><span class="string">    ...         return complex(dct['real'], dct['imag'])</span><br /> +<a class="lnum" href="#63" name="63">0063</a><span class="string">    ...     return dct</span><br /> +<a class="lnum" href="#64" name="64">0064</a><span class="string">    ... </span><br /> +<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.1'</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="#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 diff --git a/src/simplejson/docs/simplejson/decoder.py.html b/src/simplejson/docs/simplejson/decoder.py.html new file mode 100644 index 00000000..d252e691 --- /dev/null +++ b/src/simplejson/docs/simplejson/decoder.py.html @@ -0,0 +1,345 @@ +<html><head><title>/Users/bob/src/simplejson/simplejson/decoder.py</title> + <script type="text/javascript"><!-- + +function show_line_range() { + var href = document.location.href; + if (href.indexOf('?') == -1) { + return; + } + var qs = href.substring(href.indexOf('?')+1); + if (qs.indexOf('#') >= 0) { + qs = qs.substring(0, qs.indexOf('#')); + } + var first = qs.match(/f=(\d+)/)[1]; + var last = qs.match(/l=(\d+)/)[1]; + if (! first || ! last) { + return; + } + var anchors = document.getElementsByTagName('A'); + var container = document.createElement('DIV'); + container.className = 'highlighted'; + var children = []; + var start = null; + var parent = null; + var highlight = false; + for (var i = 0; i < anchors.length; i++) { + var el = anchors[i]; + if (el.getAttribute('name') == first) { + start = el.previousSibling; + parent = el.parentNode; + highlight = true; + } + if (el.getAttribute('name') == last) { + break; + } + if (highlight) { + children[children.length] = el; + el = el.nextSibling; + while (el && el.tagName != 'A') { + children[children.length] = el; + el = el.nextSibling; + } + } + } + for (i=0; i<children.length; i++) { + container.appendChild(children[i]); + } + if (start) { + start.parentNode.insertBefore(container, start.nextSibling); + } else { + parent.insertBefore(container, parent.childNodes[0]); + } +} + + // --></script> + <style> + div.python { + color: #333 + } + div.python a.lnum { + color: #555; + background-color: #eee; + border-right: 1px solid #999; + padding-right: 2px; + margin-right: 4px; + } + div.python span.comment { color: #933 } + div.python span.keyword { color: #a3e; font-weight: bold } + div.python span.op { color: #c96 } + div.python span.string { color: #6a6 } + div.python span.name { } + div.python span.text { color: #333 } + div.highlighted { background-color: #ff9; border: 1px solid #009 } + </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="string">"""</span><br /> +<a class="lnum" href="#2" name="2">0002</a><span class="string">Implementation of JSONDecoder</span><br /> +<a class="lnum" href="#3" name="3">0003</a><span class="string">"""</span><br /> +<a class="lnum" href="#4" name="4">0004</a><span class="keyword">import</span> <span class="name">re</span><br /> +<a class="lnum" href="#5" name="5">0005</a><br /> +<a class="lnum" href="#6" name="6">0006</a><span class="keyword">from</span> <span class="name">simplejson</span><span class="op">.</span><span class="name">scanner</span> <span class="keyword">import</span> <span class="name">Scanner</span><span class="op">,</span> <span class="name">pattern</span><br /> +<a class="lnum" href="#7" name="7">0007</a><br /> +<a class="lnum" href="#8" name="8">0008</a><span class="name">FLAGS</span> <span class="op">=</span> <span class="name">re</span><span class="op">.</span><span class="name">VERBOSE</span> <span class="op">|</span> <span class="name">re</span><span class="op">.</span><span class="name">MULTILINE</span> <span class="op">|</span> <span class="name">re</span><span class="op">.</span><span class="name">DOTALL</span><br /> +<a class="lnum" href="#9" name="9">0009</a><br /> +<a class="lnum" href="#10" name="10">0010</a><span class="keyword">def</span> <span class="name">_floatconstants</span><span class="op">(</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#11" name="11">0011</a>    <span class="keyword">import</span> <span class="name">struct</span><br /> +<a class="lnum" href="#12" name="12">0012</a>    <span class="keyword">import</span> <span class="name">sys</span><br /> +<a class="lnum" href="#13" name="13">0013</a>    <span class="name">_BYTES</span> <span class="op">=</span> <span class="string">'7FF80000000000007FF0000000000000'</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="string">'hex'</span><span class="op">)</span><br /> +<a class="lnum" href="#14" name="14">0014</a>    <span class="keyword">if</span> <span class="name">sys</span><span class="op">.</span><span class="name">byteorder</span> <span class="op">!=</span> <span class="string">'big'</span><span class="op">:</span><br /> +<a class="lnum" href="#15" name="15">0015</a>        <span class="name">_BYTES</span> <span class="op">=</span> <span class="name">_BYTES</span><span class="op">[</span><span class="op">:</span><span class="number">8</span><span class="op">]</span><span class="op">[</span><span class="op">:</span><span class="op">:</span><span class="op">-</span><span class="number">1</span><span class="op">]</span> <span class="op">+</span> <span class="name">_BYTES</span><span class="op">[</span><span class="number">8</span><span class="op">:</span><span class="op">]</span><span class="op">[</span><span class="op">:</span><span class="op">:</span><span class="op">-</span><span class="number">1</span><span class="op">]</span><br /> +<a class="lnum" href="#16" name="16">0016</a>    <span class="name">nan</span><span class="op">,</span> <span class="name">inf</span> <span class="op">=</span> <span class="name">struct</span><span class="op">.</span><span class="name">unpack</span><span class="op">(</span><span class="string">'dd'</span><span class="op">,</span> <span class="name">_BYTES</span><span class="op">)</span><br /> +<a class="lnum" href="#17" name="17">0017</a>    <span class="keyword">return</span> <span class="name">nan</span><span class="op">,</span> <span class="name">inf</span><span class="op">,</span> <span class="op">-</span><span class="name">inf</span><br /> +<a class="lnum" href="#18" name="18">0018</a><br /> +<a class="lnum" href="#19" name="19">0019</a><span class="name">NaN</span><span class="op">,</span> <span class="name">PosInf</span><span class="op">,</span> <span class="name">NegInf</span> <span class="op">=</span> <span class="name">_floatconstants</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#20" name="20">0020</a><br /> +<a class="lnum" href="#21" name="21">0021</a><span class="keyword">def</span> <span class="name">linecol</span><span class="op">(</span><span class="name">doc</span><span class="op">,</span> <span class="name">pos</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#22" name="22">0022</a>    <span class="name">lineno</span> <span class="op">=</span> <span class="name">doc</span><span class="op">.</span><span class="name">count</span><span class="op">(</span><span class="string">'\n'</span><span class="op">,</span> <span class="number">0</span><span class="op">,</span> <span class="name">pos</span><span class="op">)</span> <span class="op">+</span> <span class="number">1</span><br /> +<a class="lnum" href="#23" name="23">0023</a>    <span class="keyword">if</span> <span class="name">lineno</span> <span class="op">==</span> <span class="number">1</span><span class="op">:</span><br /> +<a class="lnum" href="#24" name="24">0024</a>        <span class="name">colno</span> <span class="op">=</span> <span class="name">pos</span><br /> +<a class="lnum" href="#25" name="25">0025</a>    <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#26" name="26">0026</a>        <span class="name">colno</span> <span class="op">=</span> <span class="name">pos</span> <span class="op">-</span> <span class="name">doc</span><span class="op">.</span><span class="name">rindex</span><span class="op">(</span><span class="string">'\n'</span><span class="op">,</span> <span class="number">0</span><span class="op">,</span> <span class="name">pos</span><span class="op">)</span><br /> +<a class="lnum" href="#27" name="27">0027</a>    <span class="keyword">return</span> <span class="name">lineno</span><span class="op">,</span> <span class="name">colno</span><br /> +<a class="lnum" href="#28" name="28">0028</a><br /> +<a class="lnum" href="#29" name="29">0029</a><span class="keyword">def</span> <span class="name">errmsg</span><span class="op">(</span><span class="name">msg</span><span class="op">,</span> <span class="name">doc</span><span class="op">,</span> <span class="name">pos</span><span class="op">,</span> <span class="name">end</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#30" name="30">0030</a>    <span class="name">lineno</span><span class="op">,</span> <span class="name">colno</span> <span class="op">=</span> <span class="name">linecol</span><span class="op">(</span><span class="name">doc</span><span class="op">,</span> <span class="name">pos</span><span class="op">)</span><br /> +<a class="lnum" href="#31" name="31">0031</a>    <span class="keyword">if</span> <span class="name">end</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#32" name="32">0032</a>        <span class="keyword">return</span> <span class="string">'%s: line %d column %d (char %d)'</span> <span class="op">%</span> <span class="op">(</span><span class="name">msg</span><span class="op">,</span> <span class="name">lineno</span><span class="op">,</span> <span class="name">colno</span><span class="op">,</span> <span class="name">pos</span><span class="op">)</span><br /> +<a class="lnum" href="#33" name="33">0033</a>    <span class="name">endlineno</span><span class="op">,</span> <span class="name">endcolno</span> <span class="op">=</span> <span class="name">linecol</span><span class="op">(</span><span class="name">doc</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><br /> +<a class="lnum" href="#34" name="34">0034</a>    <span class="keyword">return</span> <span class="string">'%s: line %d column %d - line %d column %d (char %d - %d)'</span> <span class="op">%</span> <span class="op">(</span><br /> +<a class="lnum" href="#35" name="35">0035</a>        <span class="name">msg</span><span class="op">,</span> <span class="name">lineno</span><span class="op">,</span> <span class="name">colno</span><span class="op">,</span> <span class="name">endlineno</span><span class="op">,</span> <span class="name">endcolno</span><span class="op">,</span> <span class="name">pos</span><span class="op">,</span> <span class="name">end</span><span class="op">)</span><br /> +<a class="lnum" href="#36" name="36">0036</a><br /> +<a class="lnum" href="#37" name="37">0037</a><span class="name">_CONSTANTS</span> <span class="op">=</span> <span class="op">{</span><br /> +<a class="lnum" href="#38" name="38">0038</a>    <span class="string">'-Infinity'</span><span class="op">:</span> <span class="name">NegInf</span><span class="op">,</span><br /> +<a class="lnum" href="#39" name="39">0039</a>    <span class="string">'Infinity'</span><span class="op">:</span> <span class="name">PosInf</span><span class="op">,</span><br /> +<a class="lnum" href="#40" name="40">0040</a>    <span class="string">'NaN'</span><span class="op">:</span> <span class="name">NaN</span><span class="op">,</span><br /> +<a class="lnum" href="#41" name="41">0041</a>    <span class="string">'true'</span><span class="op">:</span> <span class="name">True</span><span class="op">,</span><br /> +<a class="lnum" href="#42" name="42">0042</a>    <span class="string">'false'</span><span class="op">:</span> <span class="name">False</span><span class="op">,</span><br /> +<a class="lnum" href="#43" name="43">0043</a>    <span class="string">'null'</span><span class="op">:</span> <span class="name">None</span><span class="op">,</span><br /> +<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="#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="#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="#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="#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="#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="#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="#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="#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="#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 diff --git a/src/simplejson/docs/simplejson/encoder.py.html b/src/simplejson/docs/simplejson/encoder.py.html new file mode 100644 index 00000000..8c814ab6 --- /dev/null +++ b/src/simplejson/docs/simplejson/encoder.py.html @@ -0,0 +1,443 @@ +<html><head><title>/Users/bob/src/simplejson/simplejson/encoder.py</title> + <script type="text/javascript"><!-- + +function show_line_range() { + var href = document.location.href; + if (href.indexOf('?') == -1) { + return; + } + var qs = href.substring(href.indexOf('?')+1); + if (qs.indexOf('#') >= 0) { + qs = qs.substring(0, qs.indexOf('#')); + } + var first = qs.match(/f=(\d+)/)[1]; + var last = qs.match(/l=(\d+)/)[1]; + if (! first || ! last) { + return; + } + var anchors = document.getElementsByTagName('A'); + var container = document.createElement('DIV'); + container.className = 'highlighted'; + var children = []; + var start = null; + var parent = null; + var highlight = false; + for (var i = 0; i < anchors.length; i++) { + var el = anchors[i]; + if (el.getAttribute('name') == first) { + start = el.previousSibling; + parent = el.parentNode; + highlight = true; + } + if (el.getAttribute('name') == last) { + break; + } + if (highlight) { + children[children.length] = el; + el = el.nextSibling; + while (el && el.tagName != 'A') { + children[children.length] = el; + el = el.nextSibling; + } + } + } + for (i=0; i<children.length; i++) { + container.appendChild(children[i]); + } + if (start) { + start.parentNode.insertBefore(container, start.nextSibling); + } else { + parent.insertBefore(container, parent.childNodes[0]); + } +} + + // --></script> + <style> + div.python { + color: #333 + } + div.python a.lnum { + color: #555; + background-color: #eee; + border-right: 1px solid #999; + padding-right: 2px; + margin-right: 4px; + } + div.python span.comment { color: #933 } + div.python span.keyword { color: #a3e; font-weight: bold } + div.python span.op { color: #c96 } + div.python span.string { color: #6a6 } + div.python span.name { } + div.python span.text { color: #333 } + div.highlighted { background-color: #ff9; border: 1px solid #009 } + </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="string">"""</span><br /> +<a class="lnum" href="#2" name="2">0002</a><span class="string">Implementation of JSONEncoder</span><br /> +<a class="lnum" href="#3" name="3">0003</a><span class="string">"""</span><br /> +<a class="lnum" href="#4" name="4">0004</a><span class="keyword">import</span> <span class="name">re</span><br /> +<a class="lnum" href="#5" name="5">0005</a><span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#6" name="6">0006</a>    <span class="keyword">from</span> <span class="name">simplejson</span> <span class="keyword">import</span> <span class="name">_speedups</span><br /> +<a class="lnum" href="#7" name="7">0007</a><span class="keyword">except</span> <span class="name">ImportError</span><span class="op">:</span><br /> +<a class="lnum" href="#8" name="8">0008</a>    <span class="name">_speedups</span> <span class="op">=</span> <span class="name">None</span><br /> +<a class="lnum" href="#9" name="9">0009</a><br /> +<a class="lnum" href="#10" name="10">0010</a><span class="name">ESCAPE</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'[\x00-\x19\\"\b\f\n\r\t]'</span><span class="op">)</span><br /> +<a class="lnum" href="#11" name="11">0011</a><span class="name">ESCAPE_ASCII</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><br /> +<a class="lnum" href="#12" name="12">0012</a><span class="name">ESCAPE_DCT</span> <span class="op">=</span> <span class="op">{</span><br /> +<a class="lnum" href="#13" name="13">0013</a>    <span class="comment"># escape all forward slashes to prevent </script> attack</span><br /> +<a class="lnum" href="#14" name="14">0014</a>    <span class="string">'/'</span><span class="op">:</span> <span class="string">'\\/'</span><span class="op">,</span><br /> +<a class="lnum" href="#15" name="15">0015</a>    <span class="string">'\\'</span><span class="op">:</span> <span class="string">'\\\\'</span><span class="op">,</span><br /> +<a class="lnum" href="#16" name="16">0016</a>    <span class="string">'"'</span><span class="op">:</span> <span class="string">'\\"'</span><span class="op">,</span><br /> +<a class="lnum" href="#17" name="17">0017</a>    <span class="string">'\b'</span><span class="op">:</span> <span class="string">'\\b'</span><span class="op">,</span><br /> +<a class="lnum" href="#18" name="18">0018</a>    <span class="string">'\f'</span><span class="op">:</span> <span class="string">'\\f'</span><span class="op">,</span><br /> +<a class="lnum" href="#19" name="19">0019</a>    <span class="string">'\n'</span><span class="op">:</span> <span class="string">'\\n'</span><span class="op">,</span><br /> +<a class="lnum" href="#20" name="20">0020</a>    <span class="string">'\r'</span><span class="op">:</span> <span class="string">'\\r'</span><span class="op">,</span><br /> +<a class="lnum" href="#21" name="21">0021</a>    <span class="string">'\t'</span><span class="op">:</span> <span class="string">'\\t'</span><span class="op">,</span><br /> +<a class="lnum" href="#22" name="22">0022</a><span class="op">}</span><br /> +<a class="lnum" href="#23" name="23">0023</a><span class="keyword">for</span> <span class="name">i</span> <span class="keyword">in</span> <span class="name">range</span><span class="op">(</span><span class="number">0x20</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#24" name="24">0024</a>    <span class="name">ESCAPE_DCT</span><span class="op">.</span><span class="name">setdefault</span><span class="op">(</span><span class="name">chr</span><span class="op">(</span><span class="name">i</span><span class="op">)</span><span class="op">,</span> <span class="string">'\\u%04x'</span> <span class="op">%</span> <span class="op">(</span><span class="name">i</span><span class="op">,</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#25" name="25">0025</a><br /> +<a class="lnum" href="#26" name="26">0026</a><span class="comment"># assume this produces an infinity on all machines (probably not guaranteed)</span><br /> +<a class="lnum" href="#27" name="27">0027</a><span class="comment"></span><span class="name">INFINITY</span> <span class="op">=</span> <span class="name">float</span><span class="op">(</span><span class="string">'1e66666'</span><span class="op">)</span><br /> +<a class="lnum" href="#28" name="28">0028</a><br /> +<a class="lnum" href="#29" name="29">0029</a><span class="keyword">def</span> <span class="name">floatstr</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#30" name="30">0030</a>    <span class="comment"># Check for specials.  Note that this type of test is processor- and/or</span><br /> +<a class="lnum" href="#31" name="31">0031</a><span class="comment"></span>    <span class="comment"># platform-specific, so do tests which don't depend on the internals.</span><br /> +<a class="lnum" href="#32" name="32">0032</a><span class="comment"></span><br /> +<a class="lnum" href="#33" name="33">0033</a>    <span class="keyword">if</span> <span class="name">o</span> <span class="op">!=</span> <span class="name">o</span><span class="op">:</span><br /> +<a class="lnum" href="#34" name="34">0034</a>        <span class="name">text</span> <span class="op">=</span> <span class="string">'NaN'</span><br /> +<a class="lnum" href="#35" name="35">0035</a>    <span class="keyword">elif</span> <span class="name">o</span> <span class="op">==</span> <span class="name">INFINITY</span><span class="op">:</span><br /> +<a class="lnum" href="#36" name="36">0036</a>        <span class="name">text</span> <span class="op">=</span> <span class="string">'Infinity'</span><br /> +<a class="lnum" href="#37" name="37">0037</a>    <span class="keyword">elif</span> <span class="name">o</span> <span class="op">==</span> <span class="op">-</span><span class="name">INFINITY</span><span class="op">:</span><br /> +<a class="lnum" href="#38" name="38">0038</a>        <span class="name">text</span> <span class="op">=</span> <span class="string">'-Infinity'</span><br /> +<a class="lnum" href="#39" name="39">0039</a>    <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#40" name="40">0040</a>        <span class="keyword">return</span> <span class="name">str</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#41" name="41">0041</a><br /> +<a class="lnum" href="#42" name="42">0042</a>    <span class="keyword">if</span> <span class="keyword">not</span> <span class="name">allow_nan</span><span class="op">:</span><br /> +<a class="lnum" href="#43" name="43">0043</a>        <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"Out of range float values are not JSON compliant: %r"</span><br /> +<a class="lnum" href="#44" name="44">0044</a>            <span class="op">%</span> <span class="op">(</span><span class="name">o</span><span class="op">,</span><span class="op">)</span><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">return</span> <span class="name">text</span><br /> +<a class="lnum" href="#47" name="47">0047</a><br /> +<a class="lnum" href="#48" name="48">0048</a><br /> +<a class="lnum" href="#49" name="49">0049</a><span class="keyword">def</span> <span class="name">encode_basestring</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#50" name="50">0050</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#51" name="51">0051</a><span class="string">    Return a JSON representation of a Python string</span><br /> +<a class="lnum" href="#52" name="52">0052</a><span class="string">    """</span><br /> +<a class="lnum" href="#53" name="53">0053</a>    <span class="keyword">def</span> <span class="name">replace</span><span class="op">(</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#54" name="54">0054</a>        <span class="keyword">return</span> <span class="name">ESCAPE_DCT</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><br /> +<a class="lnum" href="#55" name="55">0055</a>    <span class="keyword">return</span> <span class="string">'"'</span> <span class="op">+</span> <span class="name">ESCAPE</span><span class="op">.</span><span class="name">sub</span><span class="op">(</span><span class="name">replace</span><span class="op">,</span> <span class="name">s</span><span class="op">)</span> <span class="op">+</span> <span class="string">'"'</span><br /> +<a class="lnum" href="#56" name="56">0056</a><br /> +<a class="lnum" href="#57" name="57">0057</a><span class="keyword">def</span> <span class="name">encode_basestring_ascii</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#58" name="58">0058</a>    <span class="keyword">def</span> <span class="name">replace</span><span class="op">(</span><span class="name">match</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#59" name="59">0059</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="#60" name="60">0060</a>        <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#61" name="61">0061</a>            <span class="keyword">return</span> <span class="name">ESCAPE_DCT</span><span class="op">[</span><span class="name">s</span><span class="op">]</span><br /> +<a class="lnum" href="#62" name="62">0062</a>        <span class="keyword">except</span> <span class="name">KeyError</span><span class="op">:</span><br /> +<a class="lnum" href="#63" name="63">0063</a>            <span class="name">n</span> <span class="op">=</span> <span class="name">ord</span><span class="op">(</span><span class="name">s</span><span class="op">)</span><br /> +<a class="lnum" href="#64" name="64">0064</a>            <span class="keyword">if</span> <span class="name">n</span> <span class="op"><</span> <span class="number">0x10000</span><span class="op">:</span><br /> +<a class="lnum" href="#65" name="65">0065</a>                <span class="keyword">return</span> <span class="string">'\\u%04x'</span> <span class="op">%</span> <span class="op">(</span><span class="name">n</span><span class="op">,</span><span class="op">)</span><br /> +<a class="lnum" href="#66" name="66">0066</a>            <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#67" name="67">0067</a>                <span class="comment"># surrogate pair</span><br /> +<a class="lnum" href="#68" name="68">0068</a><span class="comment"></span>                <span class="name">n</span> <span class="op">-=</span> <span class="number">0x10000</span><br /> +<a class="lnum" href="#69" name="69">0069</a>                <span class="name">s1</span> <span class="op">=</span> <span class="number">0xd800</span> <span class="op">|</span> <span class="op">(</span><span class="op">(</span><span class="name">n</span> <span class="op">>></span> <span class="number">10</span><span class="op">)</span> <span class="op">&</span> <span class="number">0x3ff</span><span class="op">)</span><br /> +<a class="lnum" href="#70" name="70">0070</a>                <span class="name">s2</span> <span class="op">=</span> <span class="number">0xdc00</span> <span class="op">|</span> <span class="op">(</span><span class="name">n</span> <span class="op">&</span> <span class="number">0x3ff</span><span class="op">)</span><br /> +<a class="lnum" href="#71" name="71">0071</a>                <span class="keyword">return</span> <span class="string">'\\u%04x\\u%04x'</span> <span class="op">%</span> <span class="op">(</span><span class="name">s1</span><span class="op">,</span> <span class="name">s2</span><span class="op">)</span><br /> +<a class="lnum" href="#72" name="72">0072</a>    <span class="keyword">return</span> <span class="string">'"'</span> <span class="op">+</span> <span class="name">str</span><span class="op">(</span><span class="name">ESCAPE_ASCII</span><span class="op">.</span><span class="name">sub</span><span class="op">(</span><span class="name">replace</span><span class="op">,</span> <span class="name">s</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span> <span class="string">'"'</span><br /> +<a class="lnum" href="#73" name="73">0073</a><br /> +<a class="lnum" href="#74" name="74">0074</a><span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#75" name="75">0075</a>    <span class="name">encode_basestring_ascii</span> <span class="op">=</span> <span class="name">_speedups</span><span class="op">.</span><span class="name">encode_basestring_ascii</span><br /> +<a class="lnum" href="#76" name="76">0076</a>    <span class="name">_need_utf8</span> <span class="op">=</span> <span class="name">True</span><br /> +<a class="lnum" href="#77" name="77">0077</a><span class="keyword">except</span> <span class="name">AttributeError</span><span class="op">:</span><br /> +<a class="lnum" href="#78" name="78">0078</a>    <span class="name">_need_utf8</span> <span class="op">=</span> <span class="name">False</span><br /> +<a class="lnum" href="#79" name="79">0079</a><br /> +<a class="lnum" href="#80" name="80">0080</a><span class="keyword">class</span> <span class="name">JSONEncoder</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#81" name="81">0081</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#82" name="82">0082</a><span class="string">    Extensible JSON <http://json.org> encoder for Python data structures.</span><br /> +<a class="lnum" href="#83" name="83">0083</a><span class="string"></span><br /> +<a class="lnum" href="#84" name="84">0084</a><span class="string">    Supports the following objects and types by default:</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">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#87" name="87">0087</a><span class="string">    | Python            | JSON          |</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">    | dict              | object        |</span><br /> +<a class="lnum" href="#90" name="90">0090</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#91" name="91">0091</a><span class="string">    | list, tuple       | array         |</span><br /> +<a class="lnum" href="#92" name="92">0092</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#93" name="93">0093</a><span class="string">    | str, unicode      | string        |</span><br /> +<a class="lnum" href="#94" name="94">0094</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#95" name="95">0095</a><span class="string">    | int, long, float  | number        |</span><br /> +<a class="lnum" href="#96" name="96">0096</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#97" name="97">0097</a><span class="string">    | True              | true          |</span><br /> +<a class="lnum" href="#98" name="98">0098</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#99" name="99">0099</a><span class="string">    | False             | false         |</span><br /> +<a class="lnum" href="#100" name="100">0100</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#101" name="101">0101</a><span class="string">    | None              | null          |</span><br /> +<a class="lnum" href="#102" name="102">0102</a><span class="string">    +-------------------+---------------+</span><br /> +<a class="lnum" href="#103" name="103">0103</a><span class="string"></span><br /> +<a class="lnum" href="#104" name="104">0104</a><span class="string">    To extend this to recognize other objects, subclass and implement a</span><br /> +<a class="lnum" href="#105" name="105">0105</a><span class="string">    ``.default()`` method with another method that returns a serializable</span><br /> +<a class="lnum" href="#106" name="106">0106</a><span class="string">    object for ``o`` if possible, otherwise it should call the superclass</span><br /> +<a class="lnum" href="#107" name="107">0107</a><span class="string">    implementation (to raise ``TypeError``).</span><br /> +<a class="lnum" href="#108" name="108">0108</a><span class="string">    """</span><br /> +<a class="lnum" href="#109" name="109">0109</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">'default'</span><span class="op">,</span> <span class="string">'encode'</span><span class="op">,</span> <span class="string">'iterencode'</span><span class="op">]</span><br /> +<a class="lnum" href="#110" name="110">0110</a>    <span class="name">item_separator</span> <span class="op">=</span> <span class="string">', '</span><br /> +<a class="lnum" href="#111" name="111">0111</a>    <span class="name">key_separator</span> <span class="op">=</span> <span class="string">': '</span><br /> +<a class="lnum" href="#112" name="112">0112</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">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><br /> +<a class="lnum" href="#113" name="113">0113</a>            <span class="name">check_circular</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">=</span><span class="name">True</span><span class="op">,</span> <span class="name">sort_keys</span><span class="op">=</span><span class="name">False</span><span class="op">,</span><br /> +<a class="lnum" href="#114" name="114">0114</a>            <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> <span class="name">encoding</span><span class="op">=</span><span class="string">'utf-8'</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#115" name="115">0115</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#116" name="116">0116</a><span class="string">        Constructor for JSONEncoder, with sensible defaults.</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 False, then it is a TypeError to attempt</span><br /> +<a class="lnum" href="#119" name="119">0119</a><span class="string">        encoding of keys that are not str, int, long, float or None.  If</span><br /> +<a class="lnum" href="#120" name="120">0120</a><span class="string">        skipkeys is True, such items are simply skipped.</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 True, the output is guaranteed to be str</span><br /> +<a class="lnum" href="#123" name="123">0123</a><span class="string">        objects with all incoming unicode characters escaped.  If</span><br /> +<a class="lnum" href="#124" name="124">0124</a><span class="string">        ensure_ascii is false, the output will be unicode object.</span><br /> +<a class="lnum" href="#125" name="125">0125</a><span class="string"></span><br /> +<a class="lnum" href="#126" name="126">0126</a><span class="string">        If check_circular is True, then lists, dicts, and custom encoded</span><br /> +<a class="lnum" href="#127" name="127">0127</a><span class="string">        objects will be checked for circular references during encoding to</span><br /> +<a class="lnum" href="#128" name="128">0128</a><span class="string">        prevent an infinite recursion (which would cause an OverflowError).</span><br /> +<a class="lnum" href="#129" name="129">0129</a><span class="string">        Otherwise, no such check takes place.</span><br /> +<a class="lnum" href="#130" name="130">0130</a><span class="string"></span><br /> +<a class="lnum" href="#131" name="131">0131</a><span class="string">        If allow_nan is True, then NaN, Infinity, and -Infinity will be</span><br /> +<a class="lnum" href="#132" name="132">0132</a><span class="string">        encoded as such.  This behavior is not JSON specification compliant,</span><br /> +<a class="lnum" href="#133" name="133">0133</a><span class="string">        but is consistent with most JavaScript based encoders and decoders.</span><br /> +<a class="lnum" href="#134" name="134">0134</a><span class="string">        Otherwise, it will be a ValueError to encode such floats.</span><br /> +<a class="lnum" href="#135" name="135">0135</a><span class="string"></span><br /> +<a class="lnum" href="#136" name="136">0136</a><span class="string">        If sort_keys is True, then the output of dictionaries will be</span><br /> +<a class="lnum" href="#137" name="137">0137</a><span class="string">        sorted by key; this is useful for regression tests to ensure</span><br /> +<a class="lnum" href="#138" name="138">0138</a><span class="string">        that JSON serializations can be compared on a day-to-day basis.</span><br /> +<a class="lnum" href="#139" name="139">0139</a><span class="string"></span><br /> +<a class="lnum" href="#140" name="140">0140</a><span class="string">        If indent is a non-negative integer, then JSON array</span><br /> +<a class="lnum" href="#141" name="141">0141</a><span class="string">        elements and object members will be pretty-printed with that</span><br /> +<a class="lnum" href="#142" name="142">0142</a><span class="string">        indent level.  An indent level of 0 will only insert newlines.</span><br /> +<a class="lnum" href="#143" name="143">0143</a><span class="string">        None is the most compact 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">        If specified, separators should be a (item_separator, key_separator)</span><br /> +<a class="lnum" href="#146" name="146">0146</a><span class="string">        tuple. The default is (', ', ': '). To get the most compact JSON</span><br /> +<a class="lnum" href="#147" name="147">0147</a><span class="string">        representation you should specify (',', ':') to eliminate whitespace.</span><br /> +<a class="lnum" href="#148" name="148">0148</a><span class="string"></span><br /> +<a class="lnum" href="#149" name="149">0149</a><span class="string">        If encoding is not None, then all input strings will be</span><br /> +<a class="lnum" href="#150" name="150">0150</a><span class="string">        transformed into unicode using that encoding prior to JSON-encoding. </span><br /> +<a class="lnum" href="#151" name="151">0151</a><span class="string">        The default is UTF-8.</span><br /> +<a class="lnum" href="#152" name="152">0152</a><span class="string">        """</span><br /> +<a class="lnum" href="#153" name="153">0153</a><br /> +<a class="lnum" href="#154" name="154">0154</a>        <span class="name">self</span><span class="op">.</span><span class="name">skipkeys</span> <span class="op">=</span> <span class="name">skipkeys</span><br /> +<a class="lnum" href="#155" name="155">0155</a>        <span class="name">self</span><span class="op">.</span><span class="name">ensure_ascii</span> <span class="op">=</span> <span class="name">ensure_ascii</span><br /> +<a class="lnum" href="#156" name="156">0156</a>        <span class="name">self</span><span class="op">.</span><span class="name">check_circular</span> <span class="op">=</span> <span class="name">check_circular</span><br /> +<a class="lnum" href="#157" name="157">0157</a>        <span class="name">self</span><span class="op">.</span><span class="name">allow_nan</span> <span class="op">=</span> <span class="name">allow_nan</span><br /> +<a class="lnum" href="#158" name="158">0158</a>        <span class="name">self</span><span class="op">.</span><span class="name">sort_keys</span> <span class="op">=</span> <span class="name">sort_keys</span><br /> +<a class="lnum" href="#159" name="159">0159</a>        <span class="name">self</span><span class="op">.</span><span class="name">indent</span> <span class="op">=</span> <span class="name">indent</span><br /> +<a class="lnum" href="#160" name="160">0160</a>        <span class="name">self</span><span class="op">.</span><span class="name">current_indent_level</span> <span class="op">=</span> <span class="number">0</span><br /> +<a class="lnum" href="#161" name="161">0161</a>        <span class="keyword">if</span> <span class="name">separators</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="#162" name="162">0162</a>            <span class="name">self</span><span class="op">.</span><span class="name">item_separator</span><span class="op">,</span> <span class="name">self</span><span class="op">.</span><span class="name">key_separator</span> <span class="op">=</span> <span class="name">separators</span><br /> +<a class="lnum" href="#163" name="163">0163</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="#164" name="164">0164</a><br /> +<a class="lnum" href="#165" name="165">0165</a>    <span class="keyword">def</span> <span class="name">_newline_indent</span><span class="op">(</span><span class="name">self</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#166" name="166">0166</a>        <span class="keyword">return</span> <span class="string">'\n'</span> <span class="op">+</span> <span class="op">(</span><span class="string">' '</span> <span class="op">*</span> <span class="op">(</span><span class="name">self</span><span class="op">.</span><span class="name">indent</span> <span class="op">*</span> <span class="name">self</span><span class="op">.</span><span class="name">current_indent_level</span><span class="op">)</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">_iterencode_list</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">lst</span><span class="op">,</span> <span class="name">markers</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#169" name="169">0169</a>        <span class="keyword">if</span> <span class="keyword">not</span> <span class="name">lst</span><span class="op">:</span><br /> +<a class="lnum" href="#170" name="170">0170</a>            <span class="keyword">yield</span> <span class="string">'[]'</span><br /> +<a class="lnum" href="#171" name="171">0171</a>            <span class="keyword">return</span><br /> +<a class="lnum" href="#172" name="172">0172</a>        <span class="keyword">if</span> <span class="name">markers</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="#173" name="173">0173</a>            <span class="name">markerid</span> <span class="op">=</span> <span class="name">id</span><span class="op">(</span><span class="name">lst</span><span class="op">)</span><br /> +<a class="lnum" href="#174" name="174">0174</a>            <span class="keyword">if</span> <span class="name">markerid</span> <span class="keyword">in</span> <span class="name">markers</span><span class="op">:</span><br /> +<a class="lnum" href="#175" name="175">0175</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"Circular reference detected"</span><span class="op">)</span><br /> +<a class="lnum" href="#176" name="176">0176</a>            <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span> <span class="op">=</span> <span class="name">lst</span><br /> +<a class="lnum" href="#177" name="177">0177</a>        <span class="keyword">yield</span> <span class="string">'['</span><br /> +<a class="lnum" href="#178" name="178">0178</a>        <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">indent</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="#179" name="179">0179</a>            <span class="name">self</span><span class="op">.</span><span class="name">current_indent_level</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#180" name="180">0180</a>            <span class="name">newline_indent</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">_newline_indent</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#181" name="181">0181</a>            <span class="name">separator</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">item_separator</span> <span class="op">+</span> <span class="name">newline_indent</span><br /> +<a class="lnum" href="#182" name="182">0182</a>            <span class="keyword">yield</span> <span class="name">newline_indent</span><br /> +<a class="lnum" href="#183" name="183">0183</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#184" name="184">0184</a>            <span class="name">newline_indent</span> <span class="op">=</span> <span class="name">None</span><br /> +<a class="lnum" href="#185" name="185">0185</a>            <span class="name">separator</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">item_separator</span><br /> +<a class="lnum" href="#186" name="186">0186</a>        <span class="name">first</span> <span class="op">=</span> <span class="name">True</span><br /> +<a class="lnum" href="#187" name="187">0187</a>        <span class="keyword">for</span> <span class="name">value</span> <span class="keyword">in</span> <span class="name">lst</span><span class="op">:</span><br /> +<a class="lnum" href="#188" name="188">0188</a>            <span class="keyword">if</span> <span class="name">first</span><span class="op">:</span><br /> +<a class="lnum" href="#189" name="189">0189</a>                <span class="name">first</span> <span class="op">=</span> <span class="name">False</span><br /> +<a class="lnum" href="#190" name="190">0190</a>            <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#191" name="191">0191</a>                <span class="keyword">yield</span> <span class="name">separator</span><br /> +<a class="lnum" href="#192" name="192">0192</a>            <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode</span><span class="op">(</span><span class="name">value</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#193" name="193">0193</a>                <span class="keyword">yield</span> <span class="name">chunk</span><br /> +<a class="lnum" href="#194" name="194">0194</a>        <span class="keyword">if</span> <span class="name">newline_indent</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="#195" name="195">0195</a>            <span class="name">self</span><span class="op">.</span><span class="name">current_indent_level</span> <span class="op">-=</span> <span class="number">1</span><br /> +<a class="lnum" href="#196" name="196">0196</a>            <span class="keyword">yield</span> <span class="name">self</span><span class="op">.</span><span class="name">_newline_indent</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#197" name="197">0197</a>        <span class="keyword">yield</span> <span class="string">']'</span><br /> +<a class="lnum" href="#198" name="198">0198</a>        <span class="keyword">if</span> <span class="name">markers</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="#199" name="199">0199</a>            <span class="keyword">del</span> <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span><br /> +<a class="lnum" href="#200" name="200">0200</a><br /> +<a class="lnum" href="#201" name="201">0201</a>    <span class="keyword">def</span> <span class="name">_iterencode_dict</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">dct</span><span class="op">,</span> <span class="name">markers</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#202" name="202">0202</a>        <span class="keyword">if</span> <span class="keyword">not</span> <span class="name">dct</span><span class="op">:</span><br /> +<a class="lnum" href="#203" name="203">0203</a>            <span class="keyword">yield</span> <span class="string">'{}'</span><br /> +<a class="lnum" href="#204" name="204">0204</a>            <span class="keyword">return</span><br /> +<a class="lnum" href="#205" name="205">0205</a>        <span class="keyword">if</span> <span class="name">markers</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="#206" name="206">0206</a>            <span class="name">markerid</span> <span class="op">=</span> <span class="name">id</span><span class="op">(</span><span class="name">dct</span><span class="op">)</span><br /> +<a class="lnum" href="#207" name="207">0207</a>            <span class="keyword">if</span> <span class="name">markerid</span> <span class="keyword">in</span> <span class="name">markers</span><span class="op">:</span><br /> +<a class="lnum" href="#208" name="208">0208</a>                <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"Circular reference detected"</span><span class="op">)</span><br /> +<a class="lnum" href="#209" name="209">0209</a>            <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span> <span class="op">=</span> <span class="name">dct</span><br /> +<a class="lnum" href="#210" name="210">0210</a>        <span class="keyword">yield</span> <span class="string">'{'</span><br /> +<a class="lnum" href="#211" name="211">0211</a>        <span class="name">key_separator</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">key_separator</span><br /> +<a class="lnum" href="#212" name="212">0212</a>        <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">indent</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="#213" name="213">0213</a>            <span class="name">self</span><span class="op">.</span><span class="name">current_indent_level</span> <span class="op">+=</span> <span class="number">1</span><br /> +<a class="lnum" href="#214" name="214">0214</a>            <span class="name">newline_indent</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">_newline_indent</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#215" name="215">0215</a>            <span class="name">item_separator</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">item_separator</span> <span class="op">+</span> <span class="name">newline_indent</span><br /> +<a class="lnum" href="#216" name="216">0216</a>            <span class="keyword">yield</span> <span class="name">newline_indent</span><br /> +<a class="lnum" href="#217" name="217">0217</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#218" name="218">0218</a>            <span class="name">newline_indent</span> <span class="op">=</span> <span class="name">None</span><br /> +<a class="lnum" href="#219" name="219">0219</a>            <span class="name">item_separator</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">item_separator</span><br /> +<a class="lnum" href="#220" name="220">0220</a>        <span class="name">first</span> <span class="op">=</span> <span class="name">True</span><br /> +<a class="lnum" href="#221" name="221">0221</a>        <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">ensure_ascii</span><span class="op">:</span><br /> +<a class="lnum" href="#222" name="222">0222</a>            <span class="name">encoder</span> <span class="op">=</span> <span class="name">encode_basestring_ascii</span><br /> +<a class="lnum" href="#223" name="223">0223</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#224" name="224">0224</a>            <span class="name">encoder</span> <span class="op">=</span> <span class="name">encode_basestring</span><br /> +<a class="lnum" href="#225" name="225">0225</a>        <span class="name">allow_nan</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">allow_nan</span><br /> +<a class="lnum" href="#226" name="226">0226</a>        <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">sort_keys</span><span class="op">:</span><br /> +<a class="lnum" href="#227" name="227">0227</a>            <span class="name">keys</span> <span class="op">=</span> <span class="name">dct</span><span class="op">.</span><span class="name">keys</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#228" name="228">0228</a>            <span class="name">keys</span><span class="op">.</span><span class="name">sort</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#229" name="229">0229</a>            <span class="name">items</span> <span class="op">=</span> <span class="op">[</span><span class="op">(</span><span class="name">k</span><span class="op">,</span> <span class="name">dct</span><span class="op">[</span><span class="name">k</span><span class="op">]</span><span class="op">)</span> <span class="keyword">for</span> <span class="name">k</span> <span class="keyword">in</span> <span class="name">keys</span><span class="op">]</span><br /> +<a class="lnum" href="#230" name="230">0230</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#231" name="231">0231</a>            <span class="name">items</span> <span class="op">=</span> <span class="name">dct</span><span class="op">.</span><span class="name">iteritems</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#232" name="232">0232</a>        <span class="name">_encoding</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">encoding</span><br /> +<a class="lnum" href="#233" name="233">0233</a>        <span class="name">_do_decode</span> <span class="op">=</span> <span class="op">(</span><span class="name">_encoding</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><br /> +<a class="lnum" href="#234" name="234">0234</a>            <span class="keyword">and</span> <span class="keyword">not</span> <span class="op">(</span><span class="name">_need_utf8</span> <span class="keyword">and</span> <span class="name">_encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#235" name="235">0235</a>        <span class="keyword">for</span> <span class="name">key</span><span class="op">,</span> <span class="name">value</span> <span class="keyword">in</span> <span class="name">items</span><span class="op">:</span><br /> +<a class="lnum" href="#236" name="236">0236</a>            <span class="keyword">if</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">key</span><span class="op">,</span> <span class="name">str</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#237" name="237">0237</a>                <span class="keyword">if</span> <span class="name">_do_decode</span><span class="op">:</span><br /> +<a class="lnum" href="#238" name="238">0238</a>                    <span class="name">key</span> <span class="op">=</span> <span class="name">key</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">_encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#239" name="239">0239</a>            <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">key</span><span class="op">,</span> <span class="name">basestring</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#240" name="240">0240</a>                <span class="keyword">pass</span><br /> +<a class="lnum" href="#241" name="241">0241</a>            <span class="comment"># JavaScript is weakly typed for these, so it makes sense to</span><br /> +<a class="lnum" href="#242" name="242">0242</a><span class="comment"></span>            <span class="comment"># also allow them.  Many encoders seem to do something like this.</span><br /> +<a class="lnum" href="#243" name="243">0243</a><span class="comment"></span>            <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">key</span><span class="op">,</span> <span class="name">float</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#244" name="244">0244</a>                <span class="name">key</span> <span class="op">=</span> <span class="name">floatstr</span><span class="op">(</span><span class="name">key</span><span class="op">,</span> <span class="name">allow_nan</span><span class="op">)</span><br /> +<a class="lnum" href="#245" name="245">0245</a>            <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">key</span><span class="op">,</span> <span class="op">(</span><span class="name">int</span><span class="op">,</span> <span class="name">long</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#246" name="246">0246</a>                <span class="name">key</span> <span class="op">=</span> <span class="name">str</span><span class="op">(</span><span class="name">key</span><span class="op">)</span><br /> +<a class="lnum" href="#247" name="247">0247</a>            <span class="keyword">elif</span> <span class="name">key</span> <span class="keyword">is</span> <span class="name">True</span><span class="op">:</span><br /> +<a class="lnum" href="#248" name="248">0248</a>                <span class="name">key</span> <span class="op">=</span> <span class="string">'true'</span><br /> +<a class="lnum" href="#249" name="249">0249</a>            <span class="keyword">elif</span> <span class="name">key</span> <span class="keyword">is</span> <span class="name">False</span><span class="op">:</span><br /> +<a class="lnum" href="#250" name="250">0250</a>                <span class="name">key</span> <span class="op">=</span> <span class="string">'false'</span><br /> +<a class="lnum" href="#251" name="251">0251</a>            <span class="keyword">elif</span> <span class="name">key</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#252" name="252">0252</a>                <span class="name">key</span> <span class="op">=</span> <span class="string">'null'</span><br /> +<a class="lnum" href="#253" name="253">0253</a>            <span class="keyword">elif</span> <span class="name">self</span><span class="op">.</span><span class="name">skipkeys</span><span class="op">:</span><br /> +<a class="lnum" href="#254" name="254">0254</a>                <span class="keyword">continue</span><br /> +<a class="lnum" href="#255" name="255">0255</a>            <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#256" name="256">0256</a>                <span class="keyword">raise</span> <span class="name">TypeError</span><span class="op">(</span><span class="string">"key %r is not a string"</span> <span class="op">%</span> <span class="op">(</span><span class="name">key</span><span class="op">,</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#257" name="257">0257</a>            <span class="keyword">if</span> <span class="name">first</span><span class="op">:</span><br /> +<a class="lnum" href="#258" name="258">0258</a>                <span class="name">first</span> <span class="op">=</span> <span class="name">False</span><br /> +<a class="lnum" href="#259" name="259">0259</a>            <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#260" name="260">0260</a>                <span class="keyword">yield</span> <span class="name">item_separator</span><br /> +<a class="lnum" href="#261" name="261">0261</a>            <span class="keyword">yield</span> <span class="name">encoder</span><span class="op">(</span><span class="name">key</span><span class="op">)</span><br /> +<a class="lnum" href="#262" name="262">0262</a>            <span class="keyword">yield</span> <span class="name">key_separator</span><br /> +<a class="lnum" href="#263" name="263">0263</a>            <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode</span><span class="op">(</span><span class="name">value</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#264" name="264">0264</a>                <span class="keyword">yield</span> <span class="name">chunk</span><br /> +<a class="lnum" href="#265" name="265">0265</a>        <span class="keyword">if</span> <span class="name">newline_indent</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">self</span><span class="op">.</span><span class="name">current_indent_level</span> <span class="op">-=</span> <span class="number">1</span><br /> +<a class="lnum" href="#267" name="267">0267</a>            <span class="keyword">yield</span> <span class="name">self</span><span class="op">.</span><span class="name">_newline_indent</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#268" name="268">0268</a>        <span class="keyword">yield</span> <span class="string">'}'</span><br /> +<a class="lnum" href="#269" name="269">0269</a>        <span class="keyword">if</span> <span class="name">markers</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="#270" name="270">0270</a>            <span class="keyword">del</span> <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span><br /> +<a class="lnum" href="#271" name="271">0271</a><br /> +<a class="lnum" href="#272" name="272">0272</a>    <span class="keyword">def</span> <span class="name">_iterencode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#273" name="273">0273</a>        <span class="keyword">if</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">basestring</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#274" name="274">0274</a>            <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">ensure_ascii</span><span class="op">:</span><br /> +<a class="lnum" href="#275" name="275">0275</a>                <span class="name">encoder</span> <span class="op">=</span> <span class="name">encode_basestring_ascii</span><br /> +<a class="lnum" href="#276" name="276">0276</a>            <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#277" name="277">0277</a>                <span class="name">encoder</span> <span class="op">=</span> <span class="name">encode_basestring</span><br /> +<a class="lnum" href="#278" name="278">0278</a>            <span class="name">_encoding</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">encoding</span><br /> +<a class="lnum" href="#279" name="279">0279</a>            <span class="keyword">if</span> <span class="op">(</span><span class="name">_encoding</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">str</span><span class="op">)</span><br /> +<a class="lnum" href="#280" name="280">0280</a>                    <span class="keyword">and</span> <span class="keyword">not</span> <span class="op">(</span><span class="name">_need_utf8</span> <span class="keyword">and</span> <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="op">:</span><br /> +<a class="lnum" href="#281" name="281">0281</a>                <span class="name">o</span> <span class="op">=</span> <span class="name">o</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">_encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#282" name="282">0282</a>            <span class="keyword">yield</span> <span class="name">encoder</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#283" name="283">0283</a>        <span class="keyword">elif</span> <span class="name">o</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#284" name="284">0284</a>            <span class="keyword">yield</span> <span class="string">'null'</span><br /> +<a class="lnum" href="#285" name="285">0285</a>        <span class="keyword">elif</span> <span class="name">o</span> <span class="keyword">is</span> <span class="name">True</span><span class="op">:</span><br /> +<a class="lnum" href="#286" name="286">0286</a>            <span class="keyword">yield</span> <span class="string">'true'</span><br /> +<a class="lnum" href="#287" name="287">0287</a>        <span class="keyword">elif</span> <span class="name">o</span> <span class="keyword">is</span> <span class="name">False</span><span class="op">:</span><br /> +<a class="lnum" href="#288" name="288">0288</a>            <span class="keyword">yield</span> <span class="string">'false'</span><br /> +<a class="lnum" href="#289" name="289">0289</a>        <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="op">(</span><span class="name">int</span><span class="op">,</span> <span class="name">long</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#290" name="290">0290</a>            <span class="keyword">yield</span> <span class="name">str</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#291" name="291">0291</a>        <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">float</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#292" name="292">0292</a>            <span class="keyword">yield</span> <span class="name">floatstr</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">self</span><span class="op">.</span><span class="name">allow_nan</span><span class="op">)</span><br /> +<a class="lnum" href="#293" name="293">0293</a>        <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="op">(</span><span class="name">list</span><span class="op">,</span> <span class="name">tuple</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#294" name="294">0294</a>            <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode_list</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#295" name="295">0295</a>                <span class="keyword">yield</span> <span class="name">chunk</span><br /> +<a class="lnum" href="#296" name="296">0296</a>        <span class="keyword">elif</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">dict</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#297" name="297">0297</a>            <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode_dict</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#298" name="298">0298</a>                <span class="keyword">yield</span> <span class="name">chunk</span><br /> +<a class="lnum" href="#299" name="299">0299</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#300" name="300">0300</a>            <span class="keyword">if</span> <span class="name">markers</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="#301" name="301">0301</a>                <span class="name">markerid</span> <span class="op">=</span> <span class="name">id</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#302" name="302">0302</a>                <span class="keyword">if</span> <span class="name">markerid</span> <span class="keyword">in</span> <span class="name">markers</span><span class="op">:</span><br /> +<a class="lnum" href="#303" name="303">0303</a>                    <span class="keyword">raise</span> <span class="name">ValueError</span><span class="op">(</span><span class="string">"Circular reference detected"</span><span class="op">)</span><br /> +<a class="lnum" href="#304" name="304">0304</a>                <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span> <span class="op">=</span> <span class="name">o</span><br /> +<a class="lnum" href="#305" name="305">0305</a>            <span class="keyword">for</span> <span class="name">chunk</span> <span class="keyword">in</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode_default</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#306" name="306">0306</a>                <span class="keyword">yield</span> <span class="name">chunk</span><br /> +<a class="lnum" href="#307" name="307">0307</a>            <span class="keyword">if</span> <span class="name">markers</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="#308" name="308">0308</a>                <span class="keyword">del</span> <span class="name">markers</span><span class="op">[</span><span class="name">markerid</span><span class="op">]</span><br /> +<a class="lnum" href="#309" name="309">0309</a><br /> +<a class="lnum" href="#310" name="310">0310</a>    <span class="keyword">def</span> <span class="name">_iterencode_default</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#311" name="311">0311</a>        <span class="name">newobj</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">default</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#312" name="312">0312</a>        <span class="keyword">return</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode</span><span class="op">(</span><span class="name">newobj</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><br /> +<a class="lnum" href="#313" name="313">0313</a><br /> +<a class="lnum" href="#314" name="314">0314</a>    <span class="keyword">def</span> <span class="name">default</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">o</span><span class="op">)</span><span class="op">:</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="string">        Implement this method in a subclass such that it returns</span><br /> +<a class="lnum" href="#317" name="317">0317</a><span class="string">        a serializable object for ``o``, or calls the base implementation</span><br /> +<a class="lnum" href="#318" name="318">0318</a><span class="string">        (to raise a ``TypeError``).</span><br /> +<a class="lnum" href="#319" name="319">0319</a><span class="string"></span><br /> +<a class="lnum" href="#320" name="320">0320</a><span class="string">        For example, to support arbitrary iterators, you could</span><br /> +<a class="lnum" href="#321" name="321">0321</a><span class="string">        implement default like this::</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">            def default(self, o):</span><br /> +<a class="lnum" href="#324" name="324">0324</a><span class="string">                try:</span><br /> +<a class="lnum" href="#325" name="325">0325</a><span class="string">                    iterable = iter(o)</span><br /> +<a class="lnum" href="#326" name="326">0326</a><span class="string">                except TypeError:</span><br /> +<a class="lnum" href="#327" name="327">0327</a><span class="string">                    pass</span><br /> +<a class="lnum" href="#328" name="328">0328</a><span class="string">                else:</span><br /> +<a class="lnum" href="#329" name="329">0329</a><span class="string">                    return list(iterable)</span><br /> +<a class="lnum" href="#330" name="330">0330</a><span class="string">                return JSONEncoder.default(self, o)</span><br /> +<a class="lnum" href="#331" name="331">0331</a><span class="string">        """</span><br /> +<a class="lnum" href="#332" name="332">0332</a>        <span class="keyword">raise</span> <span class="name">TypeError</span><span class="op">(</span><span class="string">"%r is not JSON serializable"</span> <span class="op">%</span> <span class="op">(</span><span class="name">o</span><span class="op">,</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#333" name="333">0333</a><br /> +<a class="lnum" href="#334" name="334">0334</a>    <span class="keyword">def</span> <span class="name">encode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">o</span><span class="op">)</span><span class="op">:</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="string">        Return a JSON string representation of a Python data structure.</span><br /> +<a class="lnum" href="#337" name="337">0337</a><span class="string"></span><br /> +<a class="lnum" href="#338" name="338">0338</a><span class="string">        >>> JSONEncoder().encode({"foo": ["bar", "baz"]})</span><br /> +<a class="lnum" href="#339" name="339">0339</a><span class="string">        '{"foo":["bar", "baz"]}'</span><br /> +<a class="lnum" href="#340" name="340">0340</a><span class="string">        """</span><br /> +<a class="lnum" href="#341" name="341">0341</a>        <span class="comment"># This is for extremely simple cases and benchmarks...</span><br /> +<a class="lnum" href="#342" name="342">0342</a><span class="comment"></span>        <span class="keyword">if</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">basestring</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#343" name="343">0343</a>            <span class="keyword">if</span> <span class="name">isinstance</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">str</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#344" name="344">0344</a>                <span class="name">_encoding</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">encoding</span><br /> +<a class="lnum" href="#345" name="345">0345</a>                <span class="keyword">if</span> <span class="op">(</span><span class="name">_encoding</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span><br /> +<a class="lnum" href="#346" name="346">0346</a>                        <span class="keyword">and</span> <span class="keyword">not</span> <span class="op">(</span><span class="name">_encoding</span> <span class="op">==</span> <span class="string">'utf-8'</span> <span class="keyword">and</span> <span class="name">_need_utf8</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#347" name="347">0347</a>                    <span class="name">o</span> <span class="op">=</span> <span class="name">o</span><span class="op">.</span><span class="name">decode</span><span class="op">(</span><span class="name">_encoding</span><span class="op">)</span><br /> +<a class="lnum" href="#348" name="348">0348</a>            <span class="keyword">return</span> <span class="name">encode_basestring_ascii</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><br /> +<a class="lnum" href="#349" name="349">0349</a>        <span class="comment"># This doesn't pass the iterator directly to ''.join() because it</span><br /> +<a class="lnum" href="#350" name="350">0350</a><span class="comment"></span>        <span class="comment"># sucks at reporting exceptions.  It's going to do this internally</span><br /> +<a class="lnum" href="#351" name="351">0351</a><span class="comment"></span>        <span class="comment"># anyway because it uses PySequence_Fast or similar.</span><br /> +<a class="lnum" href="#352" name="352">0352</a><span class="comment"></span>        <span class="name">chunks</span> <span class="op">=</span> <span class="name">list</span><span class="op">(</span><span class="name">self</span><span class="op">.</span><span class="name">iterencode</span><span class="op">(</span><span class="name">o</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#353" name="353">0353</a>        <span class="keyword">return</span> <span class="string">''</span><span class="op">.</span><span class="name">join</span><span class="op">(</span><span class="name">chunks</span><span class="op">)</span><br /> +<a class="lnum" href="#354" name="354">0354</a><br /> +<a class="lnum" href="#355" name="355">0355</a>    <span class="keyword">def</span> <span class="name">iterencode</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">o</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#356" name="356">0356</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#357" name="357">0357</a><span class="string">        Encode the given object and yield each string</span><br /> +<a class="lnum" href="#358" name="358">0358</a><span class="string">        representation as available.</span><br /> +<a class="lnum" href="#359" name="359">0359</a><span class="string">        </span><br /> +<a class="lnum" href="#360" name="360">0360</a><span class="string">        For example::</span><br /> +<a class="lnum" href="#361" name="361">0361</a><span class="string">            </span><br /> +<a class="lnum" href="#362" name="362">0362</a><span class="string">            for chunk in JSONEncoder().iterencode(bigobject):</span><br /> +<a class="lnum" href="#363" name="363">0363</a><span class="string">                mysocket.write(chunk)</span><br /> +<a class="lnum" href="#364" name="364">0364</a><span class="string">        """</span><br /> +<a class="lnum" href="#365" name="365">0365</a>        <span class="keyword">if</span> <span class="name">self</span><span class="op">.</span><span class="name">check_circular</span><span class="op">:</span><br /> +<a class="lnum" href="#366" name="366">0366</a>            <span class="name">markers</span> <span class="op">=</span> <span class="op">{</span><span class="op">}</span><br /> +<a class="lnum" href="#367" name="367">0367</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#368" name="368">0368</a>            <span class="name">markers</span> <span class="op">=</span> <span class="name">None</span><br /> +<a class="lnum" href="#369" name="369">0369</a>        <span class="keyword">return</span> <span class="name">self</span><span class="op">.</span><span class="name">_iterencode</span><span class="op">(</span><span class="name">o</span><span class="op">,</span> <span class="name">markers</span><span class="op">)</span><br /> +<a class="lnum" href="#370" name="370">0370</a><br /> +<a class="lnum" href="#371" name="371">0371</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'JSONEncoder'</span><span class="op">]</span></code></div></body></html> \ No newline at end of file diff --git a/src/simplejson/docs/simplejson/jsonfilter.py.html b/src/simplejson/docs/simplejson/jsonfilter.py.html new file mode 100644 index 00000000..874290bf --- /dev/null +++ b/src/simplejson/docs/simplejson/jsonfilter.py.html @@ -0,0 +1,112 @@ +<html><head><title>/Users/bob/src/simplejson/simplejson/jsonfilter.py</title> + <script type="text/javascript"><!-- + +function show_line_range() { + var href = document.location.href; + if (href.indexOf('?') == -1) { + return; + } + var qs = href.substring(href.indexOf('?')+1); + if (qs.indexOf('#') >= 0) { + qs = qs.substring(0, qs.indexOf('#')); + } + var first = qs.match(/f=(\d+)/)[1]; + var last = qs.match(/l=(\d+)/)[1]; + if (! first || ! last) { + return; + } + var anchors = document.getElementsByTagName('A'); + var container = document.createElement('DIV'); + container.className = 'highlighted'; + var children = []; + var start = null; + var parent = null; + var highlight = false; + for (var i = 0; i < anchors.length; i++) { + var el = anchors[i]; + if (el.getAttribute('name') == first) { + start = el.previousSibling; + parent = el.parentNode; + highlight = true; + } + if (el.getAttribute('name') == last) { + break; + } + if (highlight) { + children[children.length] = el; + el = el.nextSibling; + while (el && el.tagName != 'A') { + children[children.length] = el; + el = el.nextSibling; + } + } + } + for (i=0; i<children.length; i++) { + container.appendChild(children[i]); + } + if (start) { + start.parentNode.insertBefore(container, start.nextSibling); + } else { + parent.insertBefore(container, parent.childNodes[0]); + } +} + + // --></script> + <style> + div.python { + color: #333 + } + div.python a.lnum { + color: #555; + background-color: #eee; + border-right: 1px solid #999; + padding-right: 2px; + margin-right: 4px; + } + div.python span.comment { color: #933 } + div.python span.keyword { color: #a3e; font-weight: bold } + div.python span.op { color: #c96 } + div.python span.string { color: #6a6 } + div.python span.name { } + div.python span.text { color: #333 } + div.highlighted { background-color: #ff9; border: 1px solid #009 } + </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="keyword">import</span> <span class="name">simplejson</span><br /> +<a class="lnum" href="#2" name="2">0002</a><span class="keyword">import</span> <span class="name">cgi</span><br /> +<a class="lnum" href="#3" name="3">0003</a><br /> +<a class="lnum" href="#4" name="4">0004</a><span class="keyword">class</span> <span class="name">JSONFilter</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#5" name="5">0005</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">app</span><span class="op">,</span> <span class="name">mime_type</span><span class="op">=</span><span class="string">'text/x-json'</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#6" name="6">0006</a>        <span class="name">self</span><span class="op">.</span><span class="name">app</span> <span class="op">=</span> <span class="name">app</span><br /> +<a class="lnum" href="#7" name="7">0007</a>        <span class="name">self</span><span class="op">.</span><span class="name">mime_type</span> <span class="op">=</span> <span class="name">mime_type</span><br /> +<a class="lnum" href="#8" name="8">0008</a><br /> +<a class="lnum" href="#9" name="9">0009</a>    <span class="keyword">def</span> <span class="name">__call__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">environ</span><span class="op">,</span> <span class="name">start_response</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#10" name="10">0010</a>        <span class="comment"># Read JSON POST input to jsonfilter.json if matching mime type</span><br /> +<a class="lnum" href="#11" name="11">0011</a><span class="comment"></span>        <span class="name">response</span> <span class="op">=</span> <span class="op">{</span><span class="string">'status'</span><span class="op">:</span> <span class="string">'200 OK'</span><span class="op">,</span> <span class="string">'headers'</span><span class="op">:</span> <span class="op">[</span><span class="op">]</span><span class="op">}</span><br /> +<a class="lnum" href="#12" name="12">0012</a>        <span class="keyword">def</span> <span class="name">json_start_response</span><span class="op">(</span><span class="name">status</span><span class="op">,</span> <span class="name">headers</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#13" name="13">0013</a>            <span class="name">response</span><span class="op">[</span><span class="string">'status'</span><span class="op">]</span> <span class="op">=</span> <span class="name">status</span><br /> +<a class="lnum" href="#14" name="14">0014</a>            <span class="name">response</span><span class="op">[</span><span class="string">'headers'</span><span class="op">]</span><span class="op">.</span><span class="name">extend</span><span class="op">(</span><span class="name">headers</span><span class="op">)</span><br /> +<a class="lnum" href="#15" name="15">0015</a>        <span class="name">environ</span><span class="op">[</span><span class="string">'jsonfilter.mime_type'</span><span class="op">]</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">mime_type</span><br /> +<a class="lnum" href="#16" name="16">0016</a>        <span class="keyword">if</span> <span class="name">environ</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'REQUEST_METHOD'</span><span class="op">,</span> <span class="string">''</span><span class="op">)</span> <span class="op">==</span> <span class="string">'POST'</span><span class="op">:</span><br /> +<a class="lnum" href="#17" name="17">0017</a>            <span class="keyword">if</span> <span class="name">environ</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'CONTENT_TYPE'</span><span class="op">,</span> <span class="string">''</span><span class="op">)</span> <span class="op">==</span> <span class="name">self</span><span class="op">.</span><span class="name">mime_type</span><span class="op">:</span><br /> +<a class="lnum" href="#18" name="18">0018</a>                <span class="name">args</span> <span class="op">=</span> <span class="op">[</span><span class="name">_</span> <span class="keyword">for</span> <span class="name">_</span> <span class="keyword">in</span> <span class="op">[</span><span class="name">environ</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'CONTENT_LENGTH'</span><span class="op">)</span><span class="op">]</span> <span class="keyword">if</span> <span class="name">_</span><span class="op">]</span><br /> +<a class="lnum" href="#19" name="19">0019</a>                <span class="name">data</span> <span class="op">=</span> <span class="name">environ</span><span class="op">[</span><span class="string">'wsgi.input'</span><span class="op">]</span><span class="op">.</span><span class="name">read</span><span class="op">(</span><span class="op">*</span><span class="name">map</span><span class="op">(</span><span class="name">int</span><span class="op">,</span> <span class="name">args</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#20" name="20">0020</a>                <span class="name">environ</span><span class="op">[</span><span class="string">'jsonfilter.json'</span><span class="op">]</span> <span class="op">=</span> <span class="name">simplejson</span><span class="op">.</span><span class="name">loads</span><span class="op">(</span><span class="name">data</span><span class="op">)</span><br /> +<a class="lnum" href="#21" name="21">0021</a>        <span class="name">res</span> <span class="op">=</span> <span class="name">simplejson</span><span class="op">.</span><span class="name">dumps</span><span class="op">(</span><span class="name">self</span><span class="op">.</span><span class="name">app</span><span class="op">(</span><span class="name">environ</span><span class="op">,</span> <span class="name">json_start_response</span><span class="op">)</span><span class="op">)</span><br /> +<a class="lnum" href="#22" name="22">0022</a>        <span class="name">jsonp</span> <span class="op">=</span> <span class="name">cgi</span><span class="op">.</span><span class="name">parse_qs</span><span class="op">(</span><span class="name">environ</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'QUERY_STRING'</span><span class="op">,</span> <span class="string">''</span><span class="op">)</span><span class="op">)</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'jsonp'</span><span class="op">)</span><br /> +<a class="lnum" href="#23" name="23">0023</a>        <span class="keyword">if</span> <span class="name">jsonp</span><span class="op">:</span><br /> +<a class="lnum" href="#24" name="24">0024</a>            <span class="name">content_type</span> <span class="op">=</span> <span class="string">'text/javascript'</span><br /> +<a class="lnum" href="#25" name="25">0025</a>            <span class="name">res</span> <span class="op">=</span> <span class="string">''</span><span class="op">.</span><span class="name">join</span><span class="op">(</span><span class="name">jsonp</span> <span class="op">+</span> <span class="op">[</span><span class="string">'('</span><span class="op">,</span> <span class="name">res</span><span class="op">,</span> <span class="string">')'</span><span class="op">]</span><span class="op">)</span><br /> +<a class="lnum" href="#26" name="26">0026</a>        <span class="keyword">elif</span> <span class="string">'Opera'</span> <span class="keyword">in</span> <span class="name">environ</span><span class="op">.</span><span class="name">get</span><span class="op">(</span><span class="string">'HTTP_USER_AGENT'</span><span class="op">,</span> <span class="string">''</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#27" name="27">0027</a>            <span class="comment"># Opera has bunk XMLHttpRequest support for most mime types</span><br /> +<a class="lnum" href="#28" name="28">0028</a><span class="comment"></span>            <span class="name">content_type</span> <span class="op">=</span> <span class="string">'text/plain'</span><br /> +<a class="lnum" href="#29" name="29">0029</a>        <span class="keyword">else</span><span class="op">:</span><br /> +<a class="lnum" href="#30" name="30">0030</a>            <span class="name">content_type</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">mime_type</span><br /> +<a class="lnum" href="#31" name="31">0031</a>        <span class="name">headers</span> <span class="op">=</span> <span class="op">[</span><br /> +<a class="lnum" href="#32" name="32">0032</a>            <span class="op">(</span><span class="string">'Content-type'</span><span class="op">,</span> <span class="name">content_type</span><span class="op">)</span><span class="op">,</span><br /> +<a class="lnum" href="#33" name="33">0033</a>            <span class="op">(</span><span class="string">'Content-length'</span><span class="op">,</span> <span class="name">len</span><span class="op">(</span><span class="name">res</span><span class="op">)</span><span class="op">)</span><span class="op">,</span><br /> +<a class="lnum" href="#34" name="34">0034</a>        <span class="op">]</span><br /> +<a class="lnum" href="#35" name="35">0035</a>        <span class="name">headers</span><span class="op">.</span><span class="name">extend</span><span class="op">(</span><span class="name">response</span><span class="op">[</span><span class="string">'headers'</span><span class="op">]</span><span class="op">)</span><br /> +<a class="lnum" href="#36" name="36">0036</a>        <span class="name">start_response</span><span class="op">(</span><span class="name">response</span><span class="op">[</span><span class="string">'status'</span><span class="op">]</span><span class="op">,</span> <span class="name">headers</span><span class="op">)</span><br /> +<a class="lnum" href="#37" name="37">0037</a>        <span class="keyword">return</span> <span class="op">[</span><span class="name">res</span><span class="op">]</span><br /> +<a class="lnum" href="#38" name="38">0038</a><br /> +<a class="lnum" href="#39" name="39">0039</a><span class="keyword">def</span> <span class="name">factory</span><span class="op">(</span><span class="name">app</span><span class="op">,</span> <span class="name">global_conf</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="#40" name="40">0040</a>    <span class="keyword">return</span> <span class="name">JSONFilter</span><span class="op">(</span><span class="name">app</span><span class="op">,</span> <span class="op">**</span><span class="name">kw</span><span class="op">)</span></code></div></body></html> \ No newline at end of file diff --git a/src/simplejson/docs/simplejson/scanner.py.html b/src/simplejson/docs/simplejson/scanner.py.html new file mode 100644 index 00000000..031da08d --- /dev/null +++ b/src/simplejson/docs/simplejson/scanner.py.html @@ -0,0 +1,135 @@ +<html><head><title>/Users/bob/src/simplejson/simplejson/scanner.py</title> + <script type="text/javascript"><!-- + +function show_line_range() { + var href = document.location.href; + if (href.indexOf('?') == -1) { + return; + } + var qs = href.substring(href.indexOf('?')+1); + if (qs.indexOf('#') >= 0) { + qs = qs.substring(0, qs.indexOf('#')); + } + var first = qs.match(/f=(\d+)/)[1]; + var last = qs.match(/l=(\d+)/)[1]; + if (! first || ! last) { + return; + } + var anchors = document.getElementsByTagName('A'); + var container = document.createElement('DIV'); + container.className = 'highlighted'; + var children = []; + var start = null; + var parent = null; + var highlight = false; + for (var i = 0; i < anchors.length; i++) { + var el = anchors[i]; + if (el.getAttribute('name') == first) { + start = el.previousSibling; + parent = el.parentNode; + highlight = true; + } + if (el.getAttribute('name') == last) { + break; + } + if (highlight) { + children[children.length] = el; + el = el.nextSibling; + while (el && el.tagName != 'A') { + children[children.length] = el; + el = el.nextSibling; + } + } + } + for (i=0; i<children.length; i++) { + container.appendChild(children[i]); + } + if (start) { + start.parentNode.insertBefore(container, start.nextSibling); + } else { + parent.insertBefore(container, parent.childNodes[0]); + } +} + + // --></script> + <style> + div.python { + color: #333 + } + div.python a.lnum { + color: #555; + background-color: #eee; + border-right: 1px solid #999; + padding-right: 2px; + margin-right: 4px; + } + div.python span.comment { color: #933 } + div.python span.keyword { color: #a3e; font-weight: bold } + div.python span.op { color: #c96 } + div.python span.string { color: #6a6 } + div.python span.name { } + div.python span.text { color: #333 } + div.highlighted { background-color: #ff9; border: 1px solid #009 } + </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="string">"""</span><br /> +<a class="lnum" href="#2" name="2">0002</a><span class="string">Iterator based sre token scanner</span><br /> +<a class="lnum" href="#3" name="3">0003</a><span class="string">"""</span><br /> +<a class="lnum" href="#4" name="4">0004</a><span class="keyword">import</span> <span class="name">sre_parse</span><span class="op">,</span> <span class="name">sre_compile</span><span class="op">,</span> <span class="name">sre_constants</span><br /> +<a class="lnum" href="#5" name="5">0005</a><span class="keyword">from</span> <span class="name">sre_constants</span> <span class="keyword">import</span> <span class="name">BRANCH</span><span class="op">,</span> <span class="name">SUBPATTERN</span><br /> +<a class="lnum" href="#6" name="6">0006</a><span class="keyword">from</span> <span class="name">re</span> <span class="keyword">import</span> <span class="name">VERBOSE</span><span class="op">,</span> <span class="name">MULTILINE</span><span class="op">,</span> <span class="name">DOTALL</span><br /> +<a class="lnum" href="#7" name="7">0007</a><span class="keyword">import</span> <span class="name">re</span><br /> +<a class="lnum" href="#8" name="8">0008</a><br /> +<a class="lnum" href="#9" name="9">0009</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><span class="string">'Scanner'</span><span class="op">,</span> <span class="string">'pattern'</span><span class="op">]</span><br /> +<a class="lnum" href="#10" name="10">0010</a><br /> +<a class="lnum" href="#11" name="11">0011</a><span class="name">FLAGS</span> <span class="op">=</span> <span class="op">(</span><span class="name">VERBOSE</span> <span class="op">|</span> <span class="name">MULTILINE</span> <span class="op">|</span> <span class="name">DOTALL</span><span class="op">)</span><br /> +<a class="lnum" href="#12" name="12">0012</a><span class="keyword">class</span> <span class="name">Scanner</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#13" name="13">0013</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">lexicon</span><span class="op">,</span> <span class="name">flags</span><span class="op">=</span><span class="name">FLAGS</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#14" name="14">0014</a>        <span class="name">self</span><span class="op">.</span><span class="name">actions</span> <span class="op">=</span> <span class="op">[</span><span class="name">None</span><span class="op">]</span><br /> +<a class="lnum" href="#15" name="15">0015</a>        <span class="comment"># combine phrases into a compound pattern</span><br /> +<a class="lnum" href="#16" name="16">0016</a><span class="comment"></span>        <span class="name">s</span> <span class="op">=</span> <span class="name">sre_parse</span><span class="op">.</span><span class="name">Pattern</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#17" name="17">0017</a>        <span class="name">s</span><span class="op">.</span><span class="name">flags</span> <span class="op">=</span> <span class="name">flags</span><br /> +<a class="lnum" href="#18" name="18">0018</a>        <span class="name">p</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><br /> +<a class="lnum" href="#19" name="19">0019</a>        <span class="keyword">for</span> <span class="name">idx</span><span class="op">,</span> <span class="name">token</span> <span class="keyword">in</span> <span class="name">enumerate</span><span class="op">(</span><span class="name">lexicon</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#20" name="20">0020</a>            <span class="name">phrase</span> <span class="op">=</span> <span class="name">token</span><span class="op">.</span><span class="name">pattern</span><br /> +<a class="lnum" href="#21" name="21">0021</a>            <span class="keyword">try</span><span class="op">:</span><br /> +<a class="lnum" href="#22" name="22">0022</a>                <span class="name">subpattern</span> <span class="op">=</span> <span class="name">sre_parse</span><span class="op">.</span><span class="name">SubPattern</span><span class="op">(</span><span class="name">s</span><span class="op">,</span><br /> +<a class="lnum" href="#23" name="23">0023</a>                    <span class="op">[</span><span class="op">(</span><span class="name">SUBPATTERN</span><span class="op">,</span> <span class="op">(</span><span class="name">idx</span> <span class="op">+</span> <span class="number">1</span><span class="op">,</span> <span class="name">sre_parse</span><span class="op">.</span><span class="name">parse</span><span class="op">(</span><span class="name">phrase</span><span class="op">,</span> <span class="name">flags</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><span class="op">]</span><span class="op">)</span><br /> +<a class="lnum" href="#24" name="24">0024</a>            <span class="keyword">except</span> <span class="name">sre_constants</span><span class="op">.</span><span class="name">error</span><span class="op">:</span><br /> +<a class="lnum" href="#25" name="25">0025</a>                <span class="keyword">raise</span><br /> +<a class="lnum" href="#26" name="26">0026</a>            <span class="name">p</span><span class="op">.</span><span class="name">append</span><span class="op">(</span><span class="name">subpattern</span><span class="op">)</span><br /> +<a class="lnum" href="#27" name="27">0027</a>            <span class="name">self</span><span class="op">.</span><span class="name">actions</span><span class="op">.</span><span class="name">append</span><span class="op">(</span><span class="name">token</span><span class="op">)</span><br /> +<a class="lnum" href="#28" name="28">0028</a><br /> +<a class="lnum" href="#29" name="29">0029</a>        <span class="name">p</span> <span class="op">=</span> <span class="name">sre_parse</span><span class="op">.</span><span class="name">SubPattern</span><span class="op">(</span><span class="name">s</span><span class="op">,</span> <span class="op">[</span><span class="op">(</span><span class="name">BRANCH</span><span class="op">,</span> <span class="op">(</span><span class="name">None</span><span class="op">,</span> <span class="name">p</span><span class="op">)</span><span class="op">)</span><span class="op">]</span><span class="op">)</span><br /> +<a class="lnum" href="#30" name="30">0030</a>        <span class="name">self</span><span class="op">.</span><span class="name">scanner</span> <span class="op">=</span> <span class="name">sre_compile</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="name">p</span><span class="op">)</span><br /> +<a class="lnum" href="#31" name="31">0031</a><br /> +<a class="lnum" href="#32" name="32">0032</a><br /> +<a class="lnum" href="#33" name="33">0033</a>    <span class="keyword">def</span> <span class="name">iterscan</span><span class="op">(</span><span class="name">self</span><span class="op">,</span> <span class="name">string</span><span class="op">,</span> <span class="name">idx</span><span class="op">=</span><span class="number">0</span><span class="op">,</span> <span class="name">context</span><span class="op">=</span><span class="name">None</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#34" name="34">0034</a>        <span class="string">"""</span><br /> +<a class="lnum" href="#35" name="35">0035</a><span class="string">        Yield match, end_idx for each match</span><br /> +<a class="lnum" href="#36" name="36">0036</a><span class="string">        """</span><br /> +<a class="lnum" href="#37" name="37">0037</a>        <span class="name">match</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">scanner</span><span class="op">(</span><span class="name">string</span><span class="op">,</span> <span class="name">idx</span><span class="op">)</span><span class="op">.</span><span class="name">match</span><br /> +<a class="lnum" href="#38" name="38">0038</a>        <span class="name">actions</span> <span class="op">=</span> <span class="name">self</span><span class="op">.</span><span class="name">actions</span><br /> +<a class="lnum" href="#39" name="39">0039</a>        <span class="name">lastend</span> <span class="op">=</span> <span class="name">idx</span><br /> +<a class="lnum" href="#40" name="40">0040</a>        <span class="name">end</span> <span class="op">=</span> <span class="name">len</span><span class="op">(</span><span class="name">string</span><span class="op">)</span><br /> +<a class="lnum" href="#41" name="41">0041</a>        <span class="keyword">while</span> <span class="name">True</span><span class="op">:</span><br /> +<a class="lnum" href="#42" name="42">0042</a>            <span class="name">m</span> <span class="op">=</span> <span class="name">match</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#43" name="43">0043</a>            <span class="keyword">if</span> <span class="name">m</span> <span class="keyword">is</span> <span class="name">None</span><span class="op">:</span><br /> +<a class="lnum" href="#44" name="44">0044</a>                <span class="keyword">break</span><br /> +<a class="lnum" href="#45" name="45">0045</a>            <span class="name">matchbegin</span><span class="op">,</span> <span class="name">matchend</span> <span class="op">=</span> <span class="name">m</span><span class="op">.</span><span class="name">span</span><span class="op">(</span><span class="op">)</span><br /> +<a class="lnum" href="#46" name="46">0046</a>            <span class="keyword">if</span> <span class="name">lastend</span> <span class="op">==</span> <span class="name">matchend</span><span class="op">:</span><br /> +<a class="lnum" href="#47" name="47">0047</a>                <span class="keyword">break</span><br /> +<a class="lnum" href="#48" name="48">0048</a>            <span class="name">action</span> <span class="op">=</span> <span class="name">actions</span><span class="op">[</span><span class="name">m</span><span class="op">.</span><span class="name">lastindex</span><span class="op">]</span><br /> +<a class="lnum" href="#49" name="49">0049</a>            <span class="keyword">if</span> <span class="name">action</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="#50" name="50">0050</a>                <span class="name">rval</span><span class="op">,</span> <span class="name">next_pos</span> <span class="op">=</span> <span class="name">action</span><span class="op">(</span><span class="name">m</span><span class="op">,</span> <span class="name">context</span><span class="op">)</span><br /> +<a class="lnum" href="#51" name="51">0051</a>                <span class="keyword">if</span> <span class="name">next_pos</span> <span class="keyword">is</span> <span class="keyword">not</span> <span class="name">None</span> <span class="keyword">and</span> <span class="name">next_pos</span> <span class="op">!=</span> <span class="name">matchend</span><span class="op">:</span><br /> +<a class="lnum" href="#52" name="52">0052</a>                    <span class="comment"># "fast forward" the scanner</span><br /> +<a class="lnum" href="#53" name="53">0053</a><span class="comment"></span>                    <span class="name">matchend</span> <span class="op">=</span> <span class="name">next_pos</span><br /> +<a class="lnum" href="#54" name="54">0054</a>                    <span class="name">match</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">scanner</span><span class="op">(</span><span class="name">string</span><span class="op">,</span> <span class="name">matchend</span><span class="op">)</span><span class="op">.</span><span class="name">match</span><br /> +<a class="lnum" href="#55" name="55">0055</a>                <span class="keyword">yield</span> <span class="name">rval</span><span class="op">,</span> <span class="name">matchend</span><br /> +<a class="lnum" href="#56" name="56">0056</a>            <span class="name">lastend</span> <span class="op">=</span> <span class="name">matchend</span><br /> +<a class="lnum" href="#57" name="57">0057</a><br /> +<a class="lnum" href="#58" name="58">0058</a><span class="keyword">def</span> <span class="name">pattern</span><span class="op">(</span><span class="name">pattern</span><span class="op">,</span> <span class="name">flags</span><span class="op">=</span><span class="name">FLAGS</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#59" name="59">0059</a>    <span class="keyword">def</span> <span class="name">decorator</span><span class="op">(</span><span class="name">fn</span><span class="op">)</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">pattern</span> <span class="op">=</span> <span class="name">pattern</span><br /> +<a class="lnum" href="#61" name="61">0061</a>        <span class="name">fn</span><span class="op">.</span><span class="name">regex</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="name">pattern</span><span class="op">,</span> <span class="name">flags</span><span class="op">)</span><br /> +<a class="lnum" href="#62" name="62">0062</a>        <span class="keyword">return</span> <span class="name">fn</span><br /> +<a class="lnum" href="#63" name="63">0063</a>    <span class="keyword">return</span> <span class="name">decorator</span></code></div></body></html> \ No newline at end of file diff --git a/src/simplejson/ez_setup/README.txt b/src/simplejson/ez_setup/README.txt new file mode 100644 index 00000000..9287f5a6 --- /dev/null +++ b/src/simplejson/ez_setup/README.txt @@ -0,0 +1,15 @@ +This directory exists so that Subversion-based projects can share a single +copy of the ``ez_setup`` bootstrap module for ``setuptools``, and have it +automatically updated in their projects when ``setuptools`` is updated. + +For your convenience, you may use the following svn:externals definition:: + + ez_setup svn://svn.eby-sarna.com/svnroot/ez_setup + +You can set this by executing this command in your project directory:: + + svn propedit svn:externals . + +And then adding the line shown above to the file that comes up for editing. +Then, whenever you update your project, ``ez_setup`` will be updated as well. + diff --git a/src/simplejson/ez_setup/__init__.py b/src/simplejson/ez_setup/__init__.py new file mode 100644 index 00000000..38c09c62 --- /dev/null +++ b/src/simplejson/ez_setup/__init__.py @@ -0,0 +1,228 @@ +#!python +"""Bootstrap setuptools installation + +If you want to use setuptools in your package's setup.py, just include this +file in the same directory with it, and add this to the top of your setup.py:: + + from ez_setup import use_setuptools + use_setuptools() + +If you want to require a specific version of setuptools, set a download +mirror, or use an alternate download directory, you can do so by supplying +the appropriate options to ``use_setuptools()``. + +This file can also be run as a script to install or upgrade setuptools. +""" +import sys +DEFAULT_VERSION = "0.6c5" +DEFAULT_URL = "http://cheeseshop.python.org/packages/%s/s/setuptools/" % sys.version[:3] + +md5_data = { + 'setuptools-0.6b1-py2.3.egg': '8822caf901250d848b996b7f25c6e6ca', + 'setuptools-0.6b1-py2.4.egg': 'b79a8a403e4502fbb85ee3f1941735cb', + 'setuptools-0.6b2-py2.3.egg': '5657759d8a6d8fc44070a9d07272d99b', + 'setuptools-0.6b2-py2.4.egg': '4996a8d169d2be661fa32a6e52e4f82a', + 'setuptools-0.6b3-py2.3.egg': 'bb31c0fc7399a63579975cad9f5a0618', + 'setuptools-0.6b3-py2.4.egg': '38a8c6b3d6ecd22247f179f7da669fac', + 'setuptools-0.6b4-py2.3.egg': '62045a24ed4e1ebc77fe039aa4e6f7e5', + 'setuptools-0.6b4-py2.4.egg': '4cb2a185d228dacffb2d17f103b3b1c4', + 'setuptools-0.6c1-py2.3.egg': 'b3f2b5539d65cb7f74ad79127f1a908c', + 'setuptools-0.6c1-py2.4.egg': 'b45adeda0667d2d2ffe14009364f2a4b', + 'setuptools-0.6c2-py2.3.egg': 'f0064bf6aa2b7d0f3ba0b43f20817c27', + 'setuptools-0.6c2-py2.4.egg': '616192eec35f47e8ea16cd6a122b7277', + 'setuptools-0.6c3-py2.3.egg': 'f181fa125dfe85a259c9cd6f1d7b78fa', + 'setuptools-0.6c3-py2.4.egg': 'e0ed74682c998bfb73bf803a50e7b71e', + 'setuptools-0.6c3-py2.5.egg': 'abef16fdd61955514841c7c6bd98965e', + 'setuptools-0.6c4-py2.3.egg': 'b0b9131acab32022bfac7f44c5d7971f', + 'setuptools-0.6c4-py2.4.egg': '2a1f9656d4fbf3c97bf946c0a124e6e2', + 'setuptools-0.6c4-py2.5.egg': '8f5a052e32cdb9c72bcf4b5526f28afc', + 'setuptools-0.6c5-py2.3.egg': 'ee9fd80965da04f2f3e6b3576e9d8167', + 'setuptools-0.6c5-py2.4.egg': 'afe2adf1c01701ee841761f5bcd8aa64', + 'setuptools-0.6c5-py2.5.egg': 'a8d3f61494ccaa8714dfed37bccd3d5d', +} + +import sys, os + +def _validate_md5(egg_name, data): + if egg_name in md5_data: + from md5 import md5 + digest = md5(data).hexdigest() + if digest != md5_data[egg_name]: + print >>sys.stderr, ( + "md5 validation of %s failed! (Possible download problem?)" + % egg_name + ) + sys.exit(2) + return data + + +def use_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + download_delay=15 +): + """Automatically find/download setuptools and make it available on sys.path + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end with + a '/'). `to_dir` is the directory where setuptools will be downloaded, if + it is not already available. If `download_delay` is specified, it should + be the number of seconds that will be paused before initiating a download, + should one be required. If an older version of setuptools is installed, + this routine will print a message to ``sys.stderr`` and raise SystemExit in + an attempt to abort the calling script. + """ + try: + import setuptools + if setuptools.__version__ == '0.0.1': + print >>sys.stderr, ( + "You have an obsolete version of setuptools installed. Please\n" + "remove it from your system entirely before rerunning this script." + ) + sys.exit(2) + except ImportError: + egg = download_setuptools(version, download_base, to_dir, download_delay) + sys.path.insert(0, egg) + import setuptools; setuptools.bootstrap_install_from = egg + + import pkg_resources + try: + pkg_resources.require("setuptools>="+version) + + except pkg_resources.VersionConflict, e: + # XXX could we install in a subprocess here? + print >>sys.stderr, ( + "The required version of setuptools (>=%s) is not available, and\n" + "can't be installed while this script is running. Please install\n" + " a more recent version first.\n\n(Currently using %r)" + ) % (version, e.args[0]) + sys.exit(2) + +def download_setuptools( + version=DEFAULT_VERSION, download_base=DEFAULT_URL, to_dir=os.curdir, + delay = 15 +): + """Download setuptools from a specified location and return its filename + + `version` should be a valid setuptools version number that is available + as an egg for download under the `download_base` URL (which should end + with a '/'). `to_dir` is the directory where the egg will be downloaded. + `delay` is the number of seconds to pause before an actual download attempt. + """ + import urllib2, shutil + egg_name = "setuptools-%s-py%s.egg" % (version,sys.version[:3]) + url = download_base + egg_name + saveto = os.path.join(to_dir, egg_name) + src = dst = None + if not os.path.exists(saveto): # Avoid repeated downloads + try: + from distutils import log + if delay: + log.warn(""" +--------------------------------------------------------------------------- +This script requires setuptools version %s to run (even to display +help). I will attempt to download it for you (from +%s), but +you may need to enable firewall access for this script first. +I will start the download in %d seconds. + +(Note: if this machine does not have network access, please obtain the file + + %s + +and place it in this directory before rerunning this script.) +---------------------------------------------------------------------------""", + version, download_base, delay, url + ); from time import sleep; sleep(delay) + log.warn("Downloading %s", url) + src = urllib2.urlopen(url) + # Read/write all in one block, so we don't create a corrupt file + # if the download is interrupted. + data = _validate_md5(egg_name, src.read()) + dst = open(saveto,"wb"); dst.write(data) + finally: + if src: src.close() + if dst: dst.close() + return os.path.realpath(saveto) + +def main(argv, version=DEFAULT_VERSION): + """Install or upgrade setuptools and EasyInstall""" + + try: + import setuptools + except ImportError: + egg = None + try: + egg = download_setuptools(version, delay=0) + sys.path.insert(0,egg) + from setuptools.command.easy_install import main + return main(list(argv)+[egg]) # we're done here + finally: + if egg and os.path.exists(egg): + os.unlink(egg) + else: + if setuptools.__version__ == '0.0.1': + # tell the user to uninstall obsolete version + use_setuptools(version) + + req = "setuptools>="+version + import pkg_resources + try: + pkg_resources.require(req) + except pkg_resources.VersionConflict: + try: + from setuptools.command.easy_install import main + except ImportError: + from easy_install import main + main(list(argv)+[download_setuptools(delay=0)]) + sys.exit(0) # try to force an exit + else: + if argv: + from setuptools.command.easy_install import main + main(argv) + else: + print "Setuptools version",version,"or greater has been installed." + print '(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)' + + + +def update_md5(filenames): + """Update our built-in md5 registry""" + + import re + from md5 import md5 + + for name in filenames: + base = os.path.basename(name) + f = open(name,'rb') + md5_data[base] = md5(f.read()).hexdigest() + f.close() + + data = [" %r: %r,\n" % it for it in md5_data.items()] + data.sort() + repl = "".join(data) + + import inspect + srcfile = inspect.getsourcefile(sys.modules[__name__]) + f = open(srcfile, 'rb'); src = f.read(); f.close() + + match = re.search("\nmd5_data = {\n([^}]+)}", src) + if not match: + print >>sys.stderr, "Internal error!" + sys.exit(2) + + src = src[:match.start(1)] + repl + src[match.end(1):] + f = open(srcfile,'w') + f.write(src) + f.close() + + +if __name__=='__main__': + if len(sys.argv)>2 and sys.argv[1]=='--md5update': + update_md5(sys.argv[2:]) + else: + main(sys.argv[1:]) + + + + + diff --git a/src/simplejson/scripts/bench.sh b/src/simplejson/scripts/bench.sh new file mode 100644 index 00000000..e30e2467 --- /dev/null +++ b/src/simplejson/scripts/bench.sh @@ -0,0 +1,2 @@ +#!/bin/sh +/usr/bin/env python -mtimeit -s 'from simplejson.tests.test_pass1 import test_parse' 'test_parse()' diff --git a/src/simplejson/scripts/make_docs.py b/src/simplejson/scripts/make_docs.py new file mode 100644 index 00000000..2c59032b --- /dev/null +++ b/src/simplejson/scripts/make_docs.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +import os +import subprocess +import shutil + +PROJECT='simplejson' + +def _get_version(): + from pkg_resources import PathMetadata, Distribution + egg_info = PROJECT + '.egg-info' + base_dir = os.path.dirname(egg_info) + metadata = PathMetadata(base_dir, egg_info) + dist_name = os.path.splitext(os.path.basename(egg_info))[0] + dist = Distribution(base_dir, project_name=dist_name, metadata=metadata) + return dist.version +VERSION = _get_version() + +PUDGE = '/Library/Frameworks/Python.framework/Versions/2.4/bin/pudge' +#PUDGE = 'pudge' + +res = subprocess.call([ + PUDGE, '-v', '-d', 'docs', '-m', PROJECT, + '-l', '%s %s' % (PROJECT, VERSION), + '--theme=green' +]) +if not res: + shutil.copyfile('docs/module-simplejson.html', 'docs/index.html') +raise SystemExit(res) diff --git a/src/simplejson/scripts/prof.py b/src/simplejson/scripts/prof.py new file mode 100644 index 00000000..45c7a85e --- /dev/null +++ b/src/simplejson/scripts/prof.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python2.4 +from pkg_resources import require +require('simplejson') + +import profile + +from simplejson.tests.test_pass1 import test_parse + +profile.run("for x in xrange(10): test_parse()") diff --git a/src/simplejson/setup.cfg b/src/simplejson/setup.cfg new file mode 100644 index 00000000..861a9f55 --- /dev/null +++ b/src/simplejson/setup.cfg @@ -0,0 +1,5 @@ +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/src/simplejson/setup.py b/src/simplejson/setup.py new file mode 100644 index 00000000..83d160c0 --- /dev/null +++ b/src/simplejson/setup.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python + +import ez_setup +ez_setup.use_setuptools() + +from setuptools import setup, find_packages, Extension, Feature +from distutils.command.build_ext import build_ext +from distutils.errors import CCompilerError + +VERSION = '1.7.1' +DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" +LONG_DESCRIPTION = """ +simplejson is a simple, fast, complete, correct and extensible +JSON <http://json.org> encoder and decoder for Python 2.3+. It is +pure Python code with no dependencies, but includes an optional C +extension for a serious speed boost. + +simplejson was formerly known as simple_json, but changed its name to +comply with PEP 8 module naming guidelines. + +The encoder may be subclassed to provide serialization in any kind of +situation, without any special support by the objects to be serialized +(somewhat like pickle). + +The decoder can handle incoming JSON strings of any specified encoding +(UTF-8 by default). +""" + +CLASSIFIERS = filter(None, map(str.strip, +""" +Intended Audience :: Developers +License :: OSI Approved :: MIT License +Programming Language :: Python +Topic :: Software Development :: Libraries :: Python Modules +""".splitlines())) + + +BUILD_EXT_WARNING=""" +WARNING: The C extension could not be compiled, speedups are not enabled. + +Above is the output showing how the compilation failed. +""" + +class ve_build_ext(build_ext): + # This class allows C extension building to fail. + def build_extension(self, ext): + try: + build_ext.build_extension(self, ext) + except CCompilerError, x: + print ('*'*70+'\n') + print BUILD_EXT_WARNING + print ('*'*70+'\n') + +speedups = Feature( + "options C speed-enhancement modules", + standard=True, + ext_modules = [ + Extension("simplejson._speedups", ["simplejson/_speedups.c"]), + ], +) + +setup( + name="simplejson", + version=VERSION, + description=DESCRIPTION, + long_description=LONG_DESCRIPTION, + classifiers=CLASSIFIERS, + author="Bob Ippolito", + author_email="bob@redivi.com", + url="http://undefined.org/python/#simplejson", + license="MIT License", + packages=find_packages(exclude=['ez_setup']), + platforms=['any'], + test_suite="nose.collector", + zip_safe=True, + entry_points={ + 'paste.filter_app_factory': ['json = simplejson.jsonfilter:factory'], + }, + features={'speedups': speedups}, + cmdclass={'build_ext': ve_build_ext}, +) diff --git a/src/simplejson/simplejson.egg-info/PKG-INFO b/src/simplejson/simplejson.egg-info/PKG-INFO new file mode 100644 index 00000000..c202c5fa --- /dev/null +++ b/src/simplejson/simplejson.egg-info/PKG-INFO @@ -0,0 +1,29 @@ +Metadata-Version: 1.0 +Name: simplejson +Version: 1.7.1 +Summary: Simple, fast, extensible JSON encoder/decoder for Python +Home-page: http://undefined.org/python/#simplejson +Author: Bob Ippolito +Author-email: bob@redivi.com +License: MIT License +Description: + simplejson is a simple, fast, complete, correct and extensible + JSON <http://json.org> encoder and decoder for Python 2.3+. It is + pure Python code with no dependencies, but includes an optional C + extension for a serious speed boost. + + simplejson was formerly known as simple_json, but changed its name to + comply with PEP 8 module naming guidelines. + + The encoder may be subclassed to provide serialization in any kind of + situation, without any special support by the objects to be serialized + (somewhat like pickle). + + The decoder can handle incoming JSON strings of any specified encoding + (UTF-8 by default). + +Platform: any +Classifier: Intended Audience :: Developers +Classifier: License :: OSI Approved :: MIT License +Classifier: Programming Language :: Python +Classifier: Topic :: Software Development :: Libraries :: Python Modules diff --git a/src/simplejson/simplejson.egg-info/SOURCES.txt b/src/simplejson/simplejson.egg-info/SOURCES.txt new file mode 100644 index 00000000..7e0139f0 --- /dev/null +++ b/src/simplejson/simplejson.egg-info/SOURCES.txt @@ -0,0 +1,46 @@ +LICENSE.txt +setup.cfg +setup.py +docs/class-simplejson.JSONDecoder.html +docs/class-simplejson.JSONEncoder.html +docs/class_to_source.js +docs/index.html +docs/layout.css +docs/module-index.html +docs/module-simplejson-index.html +docs/module-simplejson.html +docs/pudge.css +docs/rst.css +docs/simplejson/__init__.py.html +docs/simplejson/decoder.py.html +docs/simplejson/encoder.py.html +docs/simplejson/jsonfilter.py.html +docs/simplejson/scanner.py.html +ez_setup/README.txt +ez_setup/__init__.py +scripts/bench.sh +scripts/make_docs.py +scripts/prof.py +simplejson/__init__.py +simplejson/_speedups.c +simplejson/decoder.py +simplejson/encoder.py +simplejson/jsonfilter.py +simplejson/scanner.py +simplejson.egg-info/PKG-INFO +simplejson.egg-info/SOURCES.txt +simplejson.egg-info/dependency_links.txt +simplejson.egg-info/entry_points.txt +simplejson.egg-info/top_level.txt +simplejson.egg-info/zip-safe +simplejson/tests/__init__.py +simplejson/tests/test_attacks.py +simplejson/tests/test_dump.py +simplejson/tests/test_fail.py +simplejson/tests/test_indent.py +simplejson/tests/test_pass1.py +simplejson/tests/test_pass2.py +simplejson/tests/test_pass3.py +simplejson/tests/test_recursion.py +simplejson/tests/test_separators.py +simplejson/tests/test_unicode.py diff --git a/src/simplejson/simplejson.egg-info/dependency_links.txt b/src/simplejson/simplejson.egg-info/dependency_links.txt new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/simplejson/simplejson.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/src/simplejson/simplejson.egg-info/entry_points.txt b/src/simplejson/simplejson.egg-info/entry_points.txt new file mode 100644 index 00000000..e08f296b --- /dev/null +++ b/src/simplejson/simplejson.egg-info/entry_points.txt @@ -0,0 +1,3 @@ +[paste.filter_app_factory] +json = simplejson.jsonfilter:factory + diff --git a/src/simplejson/simplejson.egg-info/top_level.txt b/src/simplejson/simplejson.egg-info/top_level.txt new file mode 100644 index 00000000..322630ee --- /dev/null +++ b/src/simplejson/simplejson.egg-info/top_level.txt @@ -0,0 +1 @@ +simplejson diff --git a/src/simplejson/simplejson.egg-info/zip-safe b/src/simplejson/simplejson.egg-info/zip-safe new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/simplejson/simplejson.egg-info/zip-safe @@ -0,0 +1 @@ + diff --git a/src/simplejson/simplejson/__init__.py b/src/simplejson/simplejson/__init__.py new file mode 100644 index 00000000..8dae51a7 --- /dev/null +++ b/src/simplejson/simplejson/__init__.py @@ -0,0 +1,287 @@ +r""" +A simple, fast, extensible JSON encoder and decoder + +JSON (JavaScript Object Notation) <http://json.org> is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format. + +simplejson exposes an API familiar to uses of the standard library +marshal and pickle modules. + +Encoding basic Python object hierarchies:: + + >>> import simplejson + >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) + '["foo", {"bar": ["baz", null, 1.0, 2]}]' + >>> print simplejson.dumps("\"foo\bar") + "\"foo\bar" + >>> print simplejson.dumps(u'\u1234') + "\u1234" + >>> print simplejson.dumps('\\') + "\\" + >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) + {"a": 0, "b": 0, "c": 0} + >>> from StringIO import StringIO + >>> io = StringIO() + >>> simplejson.dump(['streaming API'], io) + >>> io.getvalue() + '["streaming API"]' + +Compact encoding:: + + >>> import simplejson + >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) + '[1,2,3,{"4":5,"6":7}]' + +Pretty printing:: + + >>> import simplejson + >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) + { + "4": 5, + "6": 7 + } + +Decoding JSON:: + + >>> import simplejson + >>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') + [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] + >>> simplejson.loads('"\\"foo\\bar"') + u'"foo\x08ar' + >>> from StringIO import StringIO + >>> io = StringIO('["streaming API"]') + >>> simplejson.load(io) + [u'streaming API'] + +Specializing JSON object decoding:: + + >>> import simplejson + >>> def as_complex(dct): + ... if '__complex__' in dct: + ... return complex(dct['real'], dct['imag']) + ... return dct + ... + >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', + ... object_hook=as_complex) + (1+2j) + +Extending JSONEncoder:: + + >>> import simplejson + >>> class ComplexEncoder(simplejson.JSONEncoder): + ... def default(self, obj): + ... if isinstance(obj, complex): + ... return [obj.real, obj.imag] + ... return simplejson.JSONEncoder.default(self, obj) + ... + >>> dumps(2 + 1j, cls=ComplexEncoder) + '[2.0, 1.0]' + >>> ComplexEncoder().encode(2 + 1j) + '[2.0, 1.0]' + >>> list(ComplexEncoder().iterencode(2 + 1j)) + ['[', '2.0', ', ', '1.0', ']'] + + +Note that the JSON produced by this module's default settings +is a subset of YAML, so it may be used as a serializer for that as well. +""" +__version__ = '1.7.1' +__all__ = [ + 'dump', 'dumps', 'load', 'loads', + 'JSONDecoder', 'JSONEncoder', +] + +from decoder import JSONDecoder +from encoder import JSONEncoder + +_default_encoder = JSONEncoder( + skipkeys=False, + ensure_ascii=True, + check_circular=True, + allow_nan=True, + indent=None, + separators=None, + encoding='utf-8' +) + +def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, indent=None, separators=None, + encoding='utf-8', **kw): + """ + Serialize ``obj`` as a JSON formatted stream to ``fp`` (a + ``.write()``-supporting file-like object). + + If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp`` + may be ``unicode`` instances, subject to normal Python ``str`` to + ``unicode`` coercion rules. Unless ``fp.write()`` explicitly + understands ``unicode`` (as in ``codecs.getwriter()``) this is likely + to cause an error. + + If ``check_circular`` is ``False``, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) + in strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + If ``indent`` is a non-negative integer, then JSON array elements and object + members will be pretty-printed with that indent level. An indent level + of 0 will only insert newlines. ``None`` is the most compact representation. + + If ``separators`` is an ``(item_separator, dict_separator)`` tuple + then it will be used instead of the default ``(', ', ': ')`` separators. + ``(',', ':')`` is the most compact JSON representation. + + ``encoding`` is the character encoding for str instances, default is UTF-8. + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + # cached encoder + if (skipkeys is False and ensure_ascii is True and + check_circular is True and allow_nan is True and + cls is None and indent is None and separators is None and + encoding == 'utf-8' and not kw): + iterable = _default_encoder.iterencode(obj) + else: + if cls is None: + cls = JSONEncoder + iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, + separators=separators, encoding=encoding, **kw).iterencode(obj) + # could accelerate with writelines in some versions of Python, at + # a debuggability cost + for chunk in iterable: + fp.write(chunk) + + +def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, indent=None, separators=None, + encoding='utf-8', **kw): + """ + Serialize ``obj`` to a JSON formatted ``str``. + + If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is ``False``, then the return value will be a + ``unicode`` instance subject to normal Python ``str`` to ``unicode`` + coercion rules instead of being escaped to an ASCII ``str``. + + If ``check_circular`` is ``False``, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in + strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + If ``indent`` is a non-negative integer, then JSON array elements and + object members will be pretty-printed with that indent level. An indent + level of 0 will only insert newlines. ``None`` is the most compact + representation. + + If ``separators`` is an ``(item_separator, dict_separator)`` tuple + then it will be used instead of the default ``(', ', ': ')`` separators. + ``(',', ':')`` is the most compact JSON representation. + + ``encoding`` is the character encoding for str instances, default is UTF-8. + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + # cached encoder + if (skipkeys is False and ensure_ascii is True and + check_circular is True and allow_nan is True and + cls is None and indent is None and separators is None and + encoding == 'utf-8' and not kw): + return _default_encoder.encode(obj) + if cls is None: + cls = JSONEncoder + return cls( + skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, + separators=separators, encoding=encoding, + **kw).encode(obj) + +_default_decoder = JSONDecoder(encoding=None, object_hook=None) + +def load(fp, encoding=None, cls=None, object_hook=None, **kw): + """ + Deserialize ``fp`` (a ``.read()``-supporting file-like object containing + a JSON document) to a Python object. + + If the contents of ``fp`` is encoded with an ASCII based encoding other + than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must + be specified. Encodings that are not ASCII based (such as UCS-2) are + not allowed, and should be wrapped with + ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` + object and passed to ``loads()`` + + ``object_hook`` is an optional function that will be called with the + result of any object literal decode (a ``dict``). The return value of + ``object_hook`` will be used instead of the ``dict``. This feature + can be used to implement custom decoders (e.g. JSON-RPC class hinting). + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + """ + return loads(fp.read(), + encoding=encoding, cls=cls, object_hook=object_hook, **kw) + +def loads(s, encoding=None, cls=None, object_hook=None, **kw): + """ + Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON + document) to a Python object. + + If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding + other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name + must be specified. Encodings that are not ASCII based (such as UCS-2) + are not allowed and should be decoded to ``unicode`` first. + + ``object_hook`` is an optional function that will be called with the + result of any object literal decode (a ``dict``). The return value of + ``object_hook`` will be used instead of the ``dict``. This feature + can be used to implement custom decoders (e.g. JSON-RPC class hinting). + + 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: + return _default_decoder.decode(s) + if cls is None: + cls = JSONDecoder + if object_hook is not None: + kw['object_hook'] = object_hook + return cls(encoding=encoding, **kw).decode(s) + +def read(s): + """ + json-py API compatibility hook. Use loads(s) instead. + """ + import warnings + warnings.warn("simplejson.loads(s) should be used instead of read(s)", + DeprecationWarning) + return loads(s) + +def write(obj): + """ + 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/src/simplejson/simplejson/_speedups.c b/src/simplejson/simplejson/_speedups.c new file mode 100644 index 00000000..8f290bb4 --- /dev/null +++ b/src/simplejson/simplejson/_speedups.c @@ -0,0 +1,215 @@ +#include "Python.h" +#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) +typedef int Py_ssize_t; +#define PY_SSIZE_T_MAX INT_MAX +#define PY_SSIZE_T_MIN INT_MIN +#endif + +static Py_ssize_t +ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars); +static PyObject * +ascii_escape_unicode(PyObject *pystr); +static PyObject * +ascii_escape_str(PyObject *pystr); +static PyObject * +py_encode_basestring_ascii(PyObject* self __attribute__((__unused__)), PyObject *pystr); +void init_speedups(void); + +#define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '/' && c != '"') + +#define MIN_EXPANSION 6 +#ifdef Py_UNICODE_WIDE +#define MAX_EXPANSION (2 * MIN_EXPANSION) +#else +#define MAX_EXPANSION MIN_EXPANSION +#endif + +static Py_ssize_t +ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) { + Py_UNICODE x; + output[chars++] = '\\'; + switch (c) { + case '/': output[chars++] = (char)c; break; + case '\\': output[chars++] = (char)c; break; + case '"': output[chars++] = (char)c; break; + case '\b': output[chars++] = 'b'; break; + case '\f': output[chars++] = 'f'; break; + case '\n': output[chars++] = 'n'; break; + case '\r': output[chars++] = 'r'; break; + case '\t': output[chars++] = 't'; break; + default: +#ifdef Py_UNICODE_WIDE + if (c >= 0x10000) { + /* UTF-16 surrogate pair */ + Py_UNICODE v = c - 0x10000; + c = 0xd800 | ((v >> 10) & 0x3ff); + output[chars++] = 'u'; + x = (c & 0xf000) >> 12; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x0f00) >> 8; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x00f0) >> 4; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x000f); + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + c = 0xdc00 | (v & 0x3ff); + output[chars++] = '\\'; + } +#endif + output[chars++] = 'u'; + x = (c & 0xf000) >> 12; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x0f00) >> 8; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x00f0) >> 4; + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + x = (c & 0x000f); + output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); + } + return chars; +} + +static PyObject * +ascii_escape_unicode(PyObject *pystr) { + Py_ssize_t i; + Py_ssize_t input_chars; + Py_ssize_t output_size; + Py_ssize_t chars; + PyObject *rval; + char *output; + Py_UNICODE *input_unicode; + + input_chars = PyUnicode_GET_SIZE(pystr); + input_unicode = PyUnicode_AS_UNICODE(pystr); + /* One char input can be up to 6 chars output, estimate 4 of these */ + output_size = 2 + (MIN_EXPANSION * 4) + input_chars; + rval = PyString_FromStringAndSize(NULL, output_size); + if (rval == NULL) { + return NULL; + } + output = PyString_AS_STRING(rval); + chars = 0; + output[chars++] = '"'; + for (i = 0; i < input_chars; i++) { + Py_UNICODE c = input_unicode[i]; + if (S_CHAR(c)) { + output[chars++] = (char)c; + } else { + chars = ascii_escape_char(c, output, chars); + } + if (output_size - chars < (1 + MAX_EXPANSION)) { + /* There's more than four, so let's resize by a lot */ + output_size *= 2; + /* This is an upper bound */ + if (output_size > 2 + (input_chars * MAX_EXPANSION)) { + output_size = 2 + (input_chars * MAX_EXPANSION); + } + if (_PyString_Resize(&rval, output_size) == -1) { + return NULL; + } + output = PyString_AS_STRING(rval); + } + } + output[chars++] = '"'; + if (_PyString_Resize(&rval, chars) == -1) { + return NULL; + } + return rval; +} + +static PyObject * +ascii_escape_str(PyObject *pystr) { + Py_ssize_t i; + Py_ssize_t input_chars; + Py_ssize_t output_size; + Py_ssize_t chars; + PyObject *rval; + char *output; + char *input_str; + + input_chars = PyString_GET_SIZE(pystr); + input_str = PyString_AS_STRING(pystr); + /* One char input can be up to 6 chars output, estimate 4 of these */ + output_size = 2 + (MIN_EXPANSION * 4) + input_chars; + rval = PyString_FromStringAndSize(NULL, output_size); + if (rval == NULL) { + return NULL; + } + output = PyString_AS_STRING(rval); + chars = 0; + output[chars++] = '"'; + for (i = 0; i < input_chars; i++) { + Py_UNICODE c = (Py_UNICODE)input_str[i]; + if (S_CHAR(c)) { + output[chars++] = (char)c; + } else if (c > 0x7F) { + /* We hit a non-ASCII character, bail to unicode mode */ + PyObject *uni; + Py_DECREF(rval); + uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict"); + if (uni == NULL) { + return NULL; + } + rval = ascii_escape_unicode(uni); + Py_DECREF(uni); + return rval; + } else { + chars = ascii_escape_char(c, output, chars); + } + /* An ASCII char can't possibly expand to a surrogate! */ + if (output_size - chars < (1 + MIN_EXPANSION)) { + /* There's more than four, so let's resize by a lot */ + output_size *= 2; + if (output_size > 2 + (input_chars * MIN_EXPANSION)) { + output_size = 2 + (input_chars * MIN_EXPANSION); + } + if (_PyString_Resize(&rval, output_size) == -1) { + return NULL; + } + output = PyString_AS_STRING(rval); + } + } + output[chars++] = '"'; + if (_PyString_Resize(&rval, chars) == -1) { + return NULL; + } + return rval; +} + +PyDoc_STRVAR(pydoc_encode_basestring_ascii, + "encode_basestring_ascii(basestring) -> str\n" + "\n" + "..." +); + +static PyObject * +py_encode_basestring_ascii(PyObject* self __attribute__((__unused__)), PyObject *pystr) { + /* METH_O */ + if (PyString_Check(pystr)) { + return ascii_escape_str(pystr); + } else if (PyUnicode_Check(pystr)) { + return ascii_escape_unicode(pystr); + } + PyErr_SetString(PyExc_TypeError, "first argument must be a string"); + return NULL; +} + +#define DEFN(n, k) \ + { \ + #n, \ + (PyCFunction)py_ ##n, \ + k, \ + pydoc_ ##n \ + } +static PyMethodDef speedups_methods[] = { + DEFN(encode_basestring_ascii, METH_O), + {} +}; +#undef DEFN + +void +init_speedups(void) +{ + PyObject *m; + m = Py_InitModule4("_speedups", speedups_methods, NULL, NULL, PYTHON_API_VERSION); +} diff --git a/src/simplejson/simplejson/decoder.py b/src/simplejson/simplejson/decoder.py new file mode 100644 index 00000000..a1b53b2a --- /dev/null +++ b/src/simplejson/simplejson/decoder.py @@ -0,0 +1,273 @@ +""" +Implementation of JSONDecoder +""" +import re + +from simplejson.scanner import Scanner, pattern + +FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL + +def _floatconstants(): + import struct + import sys + _BYTES = '7FF80000000000007FF0000000000000'.decode('hex') + if sys.byteorder != 'big': + _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1] + nan, inf = struct.unpack('dd', _BYTES) + return nan, inf, -inf + +NaN, PosInf, NegInf = _floatconstants() + +def linecol(doc, pos): + lineno = doc.count('\n', 0, pos) + 1 + if lineno == 1: + colno = pos + else: + colno = pos - doc.rindex('\n', 0, pos) + return lineno, colno + +def errmsg(msg, doc, pos, end=None): + lineno, colno = linecol(doc, pos) + if end is None: + return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos) + endlineno, endcolno = linecol(doc, end) + return '%s: line %d column %d - line %d column %d (char %d - %d)' % ( + msg, lineno, colno, endlineno, endcolno, pos, end) + +_CONSTANTS = { + '-Infinity': NegInf, + 'Infinity': PosInf, + 'NaN': NaN, + 'true': True, + 'false': False, + 'null': None, +} + +def JSONConstant(match, context, c=_CONSTANTS): + return c[match.group(0)], 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 '')) + else: + res = int(integer) + return res, None +pattern(r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?')(JSONNumber) + +STRINGCHUNK = re.compile(r'(.*?)(["\\])', FLAGS) +BACKSLASH = { + '"': u'"', '\\': u'\\', '/': u'/', + 'b': u'\b', 'f': u'\f', 'n': u'\n', 'r': u'\r', 't': u'\t', +} + +DEFAULT_ENCODING = "utf-8" + +def scanstring(s, end, encoding=None, _b=BACKSLASH, _m=STRINGCHUNK.match): + if encoding is None: + encoding = DEFAULT_ENCODING + chunks = [] + _append = chunks.append + begin = end - 1 + while 1: + chunk = _m(s, end) + if chunk is None: + raise ValueError( + errmsg("Unterminated string starting at", s, begin)) + end = chunk.end() + content, terminator = chunk.groups() + if content: + if not isinstance(content, unicode): + content = unicode(content, encoding) + _append(content) + if terminator == '"': + break + try: + esc = s[end] + except IndexError: + raise ValueError( + errmsg("Unterminated string starting at", s, begin)) + if esc != 'u': + try: + m = _b[esc] + except KeyError: + raise ValueError( + errmsg("Invalid \\escape: %r" % (esc,), s, end)) + end += 1 + else: + esc = s[end + 1:end + 5] + try: + m = unichr(int(esc, 16)) + if len(esc) != 4 or not esc.isalnum(): + raise ValueError + except ValueError: + raise ValueError(errmsg("Invalid \\uXXXX escape", s, end)) + end += 5 + _append(m) + return u''.join(chunks), end + +def JSONString(match, context): + encoding = getattr(context, 'encoding', None) + return scanstring(match.string, match.end(), encoding) +pattern(r'"')(JSONString) + +WHITESPACE = re.compile(r'\s*', FLAGS) + +def JSONObject(match, context, _w=WHITESPACE.match): + pairs = {} + s = match.string + end = _w(s, match.end()).end() + nextchar = s[end:end + 1] + # trivial empty object + if nextchar == '}': + return pairs, end + 1 + if nextchar != '"': + raise ValueError(errmsg("Expecting property name", s, end)) + end += 1 + encoding = getattr(context, 'encoding', None) + iterscan = JSONScanner.iterscan + while True: + key, end = scanstring(s, end, encoding) + end = _w(s, end).end() + if s[end:end + 1] != ':': + raise ValueError(errmsg("Expecting : delimiter", s, end)) + end = _w(s, end + 1).end() + try: + value, end = iterscan(s, idx=end, context=context).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + pairs[key] = value + end = _w(s, end).end() + nextchar = s[end:end + 1] + end += 1 + if nextchar == '}': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end - 1)) + end = _w(s, end).end() + nextchar = s[end:end + 1] + end += 1 + if nextchar != '"': + raise ValueError(errmsg("Expecting property name", s, end - 1)) + object_hook = getattr(context, 'object_hook', None) + if object_hook is not None: + pairs = object_hook(pairs) + return pairs, end +pattern(r'{')(JSONObject) + +def JSONArray(match, context, _w=WHITESPACE.match): + values = [] + s = match.string + end = _w(s, match.end()).end() + # look-ahead for trivial empty array + nextchar = s[end:end + 1] + if nextchar == ']': + return values, end + 1 + iterscan = JSONScanner.iterscan + while True: + try: + value, end = iterscan(s, idx=end, context=context).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + values.append(value) + end = _w(s, end).end() + nextchar = s[end:end + 1] + end += 1 + if nextchar == ']': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end)) + end = _w(s, end).end() + return values, end +pattern(r'\[')(JSONArray) + +ANYTHING = [ + JSONObject, + JSONArray, + JSONString, + JSONConstant, + JSONNumber, +] + +JSONScanner = Scanner(ANYTHING) + +class JSONDecoder(object): + """ + Simple JSON <http://json.org> decoder + + Performs the following translations in decoding: + + +---------------+-------------------+ + | JSON | Python | + +===============+===================+ + | object | dict | + +---------------+-------------------+ + | array | list | + +---------------+-------------------+ + | string | unicode | + +---------------+-------------------+ + | number (int) | int, long | + +---------------+-------------------+ + | number (real) | float | + +---------------+-------------------+ + | true | True | + +---------------+-------------------+ + | false | False | + +---------------+-------------------+ + | null | None | + +---------------+-------------------+ + + It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as + their corresponding ``float`` values, which is outside the JSON spec. + """ + + _scanner = Scanner(ANYTHING) + __all__ = ['__init__', 'decode', 'raw_decode'] + + def __init__(self, encoding=None, object_hook=None): + """ + ``encoding`` determines the encoding used to interpret any ``str`` + objects decoded by this instance (utf-8 by default). It has no + effect when decoding ``unicode`` objects. + + Note that currently only encodings that are a superset of ASCII work, + strings of other encodings should be passed in as ``unicode``. + + ``object_hook``, if specified, will be called with the result + of every JSON object decoded and its return value will be used in + place of the given ``dict``. This can be used to provide custom + deserializations (e.g. to support JSON-RPC class hinting). + """ + self.encoding = encoding + self.object_hook = object_hook + + def decode(self, s, _w=WHITESPACE.match): + """ + Return the Python representation of ``s`` (a ``str`` or ``unicode`` + instance containing a JSON document) + """ + obj, end = self.raw_decode(s, idx=_w(s, 0).end()) + end = _w(s, end).end() + if end != len(s): + raise ValueError(errmsg("Extra data", s, end, len(s))) + return obj + + def raw_decode(self, s, **kw): + """ + Decode a JSON document from ``s`` (a ``str`` or ``unicode`` beginning + with a JSON document) and return a 2-tuple of the Python + representation and the index in ``s`` where the document ended. + + This can be used to decode a JSON document from a string that may + have extraneous data at the end. + """ + kw.setdefault('context', self) + try: + obj, end = self._scanner.iterscan(s, **kw).next() + except StopIteration: + raise ValueError("No JSON object could be decoded") + return obj, end + +__all__ = ['JSONDecoder'] diff --git a/src/simplejson/simplejson/encoder.py b/src/simplejson/simplejson/encoder.py new file mode 100644 index 00000000..eec9c7ff --- /dev/null +++ b/src/simplejson/simplejson/encoder.py @@ -0,0 +1,371 @@ +""" +Implementation of JSONEncoder +""" +import re +try: + from simplejson import _speedups +except ImportError: + _speedups = None + +ESCAPE = re.compile(r'[\x00-\x19\\"\b\f\n\r\t]') +ESCAPE_ASCII = re.compile(r'([\\"/]|[^\ -~])') +ESCAPE_DCT = { + # escape all forward slashes to prevent </script> attack + '/': '\\/', + '\\': '\\\\', + '"': '\\"', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', +} +for i in range(0x20): + ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) + +# assume this produces an infinity on all machines (probably not guaranteed) +INFINITY = float('1e66666') + +def floatstr(o, allow_nan=True): + # Check for specials. Note that this type of test is processor- and/or + # platform-specific, so do tests which don't depend on the internals. + + if o != o: + text = 'NaN' + elif o == INFINITY: + text = 'Infinity' + elif o == -INFINITY: + text = '-Infinity' + else: + return str(o) + + if not allow_nan: + raise ValueError("Out of range float values are not JSON compliant: %r" + % (o,)) + + return text + + +def encode_basestring(s): + """ + Return a JSON representation of a Python string + """ + def replace(match): + return ESCAPE_DCT[match.group(0)] + return '"' + ESCAPE.sub(replace, s) + '"' + +def encode_basestring_ascii(s): + def replace(match): + s = match.group(0) + try: + return ESCAPE_DCT[s] + except KeyError: + n = ord(s) + if n < 0x10000: + return '\\u%04x' % (n,) + else: + # surrogate pair + n -= 0x10000 + s1 = 0xd800 | ((n >> 10) & 0x3ff) + s2 = 0xdc00 | (n & 0x3ff) + return '\\u%04x\\u%04x' % (s1, s2) + return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' + +try: + encode_basestring_ascii = _speedups.encode_basestring_ascii + _need_utf8 = True +except AttributeError: + _need_utf8 = False + +class JSONEncoder(object): + """ + Extensible JSON <http://json.org> encoder for Python data structures. + + Supports the following objects and types by default: + + +-------------------+---------------+ + | Python | JSON | + +===================+===============+ + | dict | object | + +-------------------+---------------+ + | list, tuple | array | + +-------------------+---------------+ + | str, unicode | string | + +-------------------+---------------+ + | int, long, float | number | + +-------------------+---------------+ + | True | true | + +-------------------+---------------+ + | False | false | + +-------------------+---------------+ + | None | null | + +-------------------+---------------+ + + To extend this to recognize other objects, subclass and implement a + ``.default()`` method with another method that returns a serializable + object for ``o`` if possible, otherwise it should call the superclass + implementation (to raise ``TypeError``). + """ + __all__ = ['__init__', 'default', 'encode', 'iterencode'] + item_separator = ', ' + key_separator = ': ' + def __init__(self, skipkeys=False, ensure_ascii=True, + check_circular=True, allow_nan=True, sort_keys=False, + indent=None, separators=None, encoding='utf-8'): + """ + Constructor for JSONEncoder, with sensible defaults. + + If skipkeys is False, then it is a TypeError to attempt + encoding of keys that are not str, int, long, float or None. If + skipkeys is True, such items are simply skipped. + + If ensure_ascii is True, the output is guaranteed to be str + objects with all incoming unicode characters escaped. If + ensure_ascii is false, the output will be unicode object. + + If check_circular is True, then lists, dicts, and custom encoded + objects will be checked for circular references during encoding to + prevent an infinite recursion (which would cause an OverflowError). + Otherwise, no such check takes place. + + If allow_nan is True, then NaN, Infinity, and -Infinity will be + encoded as such. This behavior is not JSON specification compliant, + but is consistent with most JavaScript based encoders and decoders. + Otherwise, it will be a ValueError to encode such floats. + + If sort_keys is True, then the output of dictionaries will be + sorted by key; this is useful for regression tests to ensure + that JSON serializations can be compared on a day-to-day basis. + + If indent is a non-negative integer, then JSON array + elements and object members will be pretty-printed with that + indent level. An indent level of 0 will only insert newlines. + None is the most compact representation. + + If specified, separators should be a (item_separator, key_separator) + tuple. The default is (', ', ': '). To get the most compact JSON + representation you should specify (',', ':') to eliminate whitespace. + + If encoding is not None, then all input strings will be + transformed into unicode using that encoding prior to JSON-encoding. + The default is UTF-8. + """ + + self.skipkeys = skipkeys + self.ensure_ascii = ensure_ascii + self.check_circular = check_circular + self.allow_nan = allow_nan + self.sort_keys = sort_keys + self.indent = indent + self.current_indent_level = 0 + if separators is not None: + self.item_separator, self.key_separator = separators + self.encoding = encoding + + def _newline_indent(self): + return '\n' + (' ' * (self.indent * self.current_indent_level)) + + def _iterencode_list(self, lst, markers=None): + if not lst: + yield '[]' + return + if markers is not None: + markerid = id(lst) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = lst + yield '[' + if self.indent is not None: + self.current_indent_level += 1 + newline_indent = self._newline_indent() + separator = self.item_separator + newline_indent + yield newline_indent + else: + newline_indent = None + separator = self.item_separator + first = True + for value in lst: + if first: + first = False + else: + yield separator + for chunk in self._iterencode(value, markers): + yield chunk + if newline_indent is not None: + self.current_indent_level -= 1 + yield self._newline_indent() + yield ']' + if markers is not None: + del markers[markerid] + + def _iterencode_dict(self, dct, markers=None): + if not dct: + yield '{}' + return + if markers is not None: + markerid = id(dct) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = dct + yield '{' + key_separator = self.key_separator + if self.indent is not None: + self.current_indent_level += 1 + newline_indent = self._newline_indent() + item_separator = self.item_separator + newline_indent + yield newline_indent + else: + newline_indent = None + item_separator = self.item_separator + first = True + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + allow_nan = self.allow_nan + if self.sort_keys: + keys = dct.keys() + keys.sort() + items = [(k, dct[k]) for k in keys] + else: + items = dct.iteritems() + _encoding = self.encoding + _do_decode = (_encoding is not None + and not (_need_utf8 and _encoding == 'utf-8')) + for key, value in items: + if isinstance(key, str): + if _do_decode: + key = key.decode(_encoding) + elif isinstance(key, basestring): + pass + # JavaScript is weakly typed for these, so it makes sense to + # also allow them. Many encoders seem to do something like this. + elif isinstance(key, float): + key = floatstr(key, allow_nan) + elif isinstance(key, (int, long)): + key = str(key) + elif key is True: + key = 'true' + elif key is False: + key = 'false' + elif key is None: + key = 'null' + elif self.skipkeys: + continue + else: + raise TypeError("key %r is not a string" % (key,)) + if first: + first = False + else: + yield item_separator + yield encoder(key) + yield key_separator + for chunk in self._iterencode(value, markers): + yield chunk + if newline_indent is not None: + self.current_indent_level -= 1 + yield self._newline_indent() + yield '}' + if markers is not None: + del markers[markerid] + + def _iterencode(self, o, markers=None): + if isinstance(o, basestring): + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + _encoding = self.encoding + if (_encoding is not None and isinstance(o, str) + and not (_need_utf8 and _encoding == 'utf-8')): + o = o.decode(_encoding) + yield encoder(o) + elif o is None: + yield 'null' + elif o is True: + yield 'true' + elif o is False: + yield 'false' + elif isinstance(o, (int, long)): + yield str(o) + elif isinstance(o, float): + yield floatstr(o, self.allow_nan) + elif isinstance(o, (list, tuple)): + for chunk in self._iterencode_list(o, markers): + yield chunk + elif isinstance(o, dict): + for chunk in self._iterencode_dict(o, markers): + yield chunk + else: + if markers is not None: + markerid = id(o) + if markerid in markers: + raise ValueError("Circular reference detected") + markers[markerid] = o + for chunk in self._iterencode_default(o, markers): + yield chunk + if markers is not None: + del markers[markerid] + + def _iterencode_default(self, o, markers=None): + newobj = self.default(o) + return self._iterencode(newobj, markers) + + def default(self, o): + """ + Implement this method in a subclass such that it returns + a serializable object for ``o``, or calls the base implementation + (to raise a ``TypeError``). + + For example, to support arbitrary iterators, you could + implement default like this:: + + def default(self, o): + try: + iterable = iter(o) + except TypeError: + pass + else: + return list(iterable) + return JSONEncoder.default(self, o) + """ + raise TypeError("%r is not JSON serializable" % (o,)) + + def encode(self, o): + """ + Return a JSON string representation of a Python data structure. + + >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) + '{"foo":["bar", "baz"]}' + """ + # This is for extremely simple cases and benchmarks... + if isinstance(o, basestring): + if isinstance(o, str): + _encoding = self.encoding + if (_encoding is not None + and not (_encoding == 'utf-8' and _need_utf8)): + o = o.decode(_encoding) + return encode_basestring_ascii(o) + # This doesn't pass the iterator directly to ''.join() because it + # sucks at reporting exceptions. It's going to do this internally + # anyway because it uses PySequence_Fast or similar. + chunks = list(self.iterencode(o)) + return ''.join(chunks) + + def iterencode(self, o): + """ + Encode the given object and yield each string + representation as available. + + For example:: + + for chunk in JSONEncoder().iterencode(bigobject): + mysocket.write(chunk) + """ + if self.check_circular: + markers = {} + else: + markers = None + return self._iterencode(o, markers) + +__all__ = ['JSONEncoder'] diff --git a/src/simplejson/simplejson/jsonfilter.py b/src/simplejson/simplejson/jsonfilter.py new file mode 100644 index 00000000..01ca21df --- /dev/null +++ b/src/simplejson/simplejson/jsonfilter.py @@ -0,0 +1,40 @@ +import simplejson +import cgi + +class JSONFilter(object): + def __init__(self, app, mime_type='text/x-json'): + self.app = app + self.mime_type = mime_type + + def __call__(self, environ, start_response): + # Read JSON POST input to jsonfilter.json if matching mime type + response = {'status': '200 OK', 'headers': []} + def json_start_response(status, headers): + response['status'] = status + response['headers'].extend(headers) + environ['jsonfilter.mime_type'] = self.mime_type + if environ.get('REQUEST_METHOD', '') == 'POST': + if environ.get('CONTENT_TYPE', '') == self.mime_type: + args = [_ for _ in [environ.get('CONTENT_LENGTH')] if _] + data = environ['wsgi.input'].read(*map(int, args)) + environ['jsonfilter.json'] = simplejson.loads(data) + res = simplejson.dumps(self.app(environ, json_start_response)) + jsonp = cgi.parse_qs(environ.get('QUERY_STRING', '')).get('jsonp') + if jsonp: + content_type = 'text/javascript' + res = ''.join(jsonp + ['(', res, ')']) + elif 'Opera' in environ.get('HTTP_USER_AGENT', ''): + # Opera has bunk XMLHttpRequest support for most mime types + content_type = 'text/plain' + else: + content_type = self.mime_type + headers = [ + ('Content-type', content_type), + ('Content-length', len(res)), + ] + headers.extend(response['headers']) + start_response(response['status'], headers) + return [res] + +def factory(app, global_conf, **kw): + return JSONFilter(app, **kw) diff --git a/src/simplejson/simplejson/scanner.py b/src/simplejson/simplejson/scanner.py new file mode 100644 index 00000000..64f4999f --- /dev/null +++ b/src/simplejson/simplejson/scanner.py @@ -0,0 +1,63 @@ +""" +Iterator based sre token scanner +""" +import sre_parse, sre_compile, sre_constants +from sre_constants import BRANCH, SUBPATTERN +from re import VERBOSE, MULTILINE, DOTALL +import re + +__all__ = ['Scanner', 'pattern'] + +FLAGS = (VERBOSE | MULTILINE | DOTALL) +class Scanner(object): + def __init__(self, lexicon, flags=FLAGS): + self.actions = [None] + # combine phrases into a compound pattern + s = sre_parse.Pattern() + s.flags = flags + p = [] + for idx, token in enumerate(lexicon): + phrase = token.pattern + try: + subpattern = sre_parse.SubPattern(s, + [(SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))]) + except sre_constants.error: + raise + p.append(subpattern) + self.actions.append(token) + + p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) + self.scanner = sre_compile.compile(p) + + + def iterscan(self, string, idx=0, context=None): + """ + Yield match, end_idx for each match + """ + match = self.scanner.scanner(string, idx).match + actions = self.actions + lastend = idx + end = len(string) + while True: + m = match() + if m is None: + break + matchbegin, matchend = m.span() + if lastend == matchend: + break + action = actions[m.lastindex] + if action is not None: + rval, next_pos = action(m, context) + if next_pos is not None and next_pos != matchend: + # "fast forward" the scanner + matchend = next_pos + match = self.scanner.scanner(string, matchend).match + yield rval, matchend + lastend = matchend + +def pattern(pattern, flags=FLAGS): + def decorator(fn): + fn.pattern = pattern + fn.regex = re.compile(pattern, flags) + return fn + return decorator diff --git a/src/simplejson/simplejson/tests/__init__.py b/src/simplejson/simplejson/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/simplejson/simplejson/tests/test_attacks.py b/src/simplejson/simplejson/tests/test_attacks.py new file mode 100644 index 00000000..8ecfed8f --- /dev/null +++ b/src/simplejson/simplejson/tests/test_attacks.py @@ -0,0 +1,6 @@ +def test_script_close_attack(): + import simplejson + res = simplejson.dumps('</script>') + assert '</script>' not in res + res = simplejson.dumps(simplejson.loads('"</script>"')) + assert '</script>' not in res diff --git a/src/simplejson/simplejson/tests/test_dump.py b/src/simplejson/simplejson/tests/test_dump.py new file mode 100644 index 00000000..b4e236e5 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_dump.py @@ -0,0 +1,10 @@ +from cStringIO import StringIO +import simplejson as S + +def test_dump(): + sio = StringIO() + S.dump({}, sio) + assert sio.getvalue() == '{}' + +def test_dumps(): + assert S.dumps({}) == '{}' diff --git a/src/simplejson/simplejson/tests/test_fail.py b/src/simplejson/simplejson/tests/test_fail.py new file mode 100644 index 00000000..a99d9c40 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_fail.py @@ -0,0 +1,70 @@ +# Fri Dec 30 18:57:26 2005 +JSONDOCS = [ + # http://json.org/JSON_checker/test/fail1.json + '"A JSON payload should be an object or array, not a string."', + # http://json.org/JSON_checker/test/fail2.json + '["Unclosed array"', + # http://json.org/JSON_checker/test/fail3.json + '{unquoted_key: "keys must be quoted}', + # http://json.org/JSON_checker/test/fail4.json + '["extra comma",]', + # http://json.org/JSON_checker/test/fail5.json + '["double extra comma",,]', + # http://json.org/JSON_checker/test/fail6.json + '[ , "<-- missing value"]', + # http://json.org/JSON_checker/test/fail7.json + '["Comma after the close"],', + # http://json.org/JSON_checker/test/fail8.json + '["Extra close"]]', + # http://json.org/JSON_checker/test/fail9.json + '{"Extra comma": true,}', + # http://json.org/JSON_checker/test/fail10.json + '{"Extra value after close": true} "misplaced quoted value"', + # http://json.org/JSON_checker/test/fail11.json + '{"Illegal expression": 1 + 2}', + # http://json.org/JSON_checker/test/fail12.json + '{"Illegal invocation": alert()}', + # http://json.org/JSON_checker/test/fail13.json + '{"Numbers cannot have leading zeroes": 013}', + # http://json.org/JSON_checker/test/fail14.json + '{"Numbers cannot be hex": 0x14}', + # http://json.org/JSON_checker/test/fail15.json + '["Illegal backslash escape: \\x15"]', + # http://json.org/JSON_checker/test/fail16.json + '["Illegal backslash escape: \\\'"]', + # http://json.org/JSON_checker/test/fail17.json + '["Illegal backslash escape: \\017"]', + # http://json.org/JSON_checker/test/fail18.json + '[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', + # http://json.org/JSON_checker/test/fail19.json + '{"Missing colon" null}', + # http://json.org/JSON_checker/test/fail20.json + '{"Double colon":: null}', + # http://json.org/JSON_checker/test/fail21.json + '{"Comma instead of colon", null}', + # http://json.org/JSON_checker/test/fail22.json + '["Colon instead of comma": false]', + # http://json.org/JSON_checker/test/fail23.json + '["Bad value", truth]', + # http://json.org/JSON_checker/test/fail24.json + "['single quote']", +] + +SKIPS = { + 1: "why not have a string payload?", + 18: "spec doesn't specify any nesting limitations", +} + +def test_failures(): + import simplejson + for idx, doc in enumerate(JSONDOCS): + idx = idx + 1 + if idx in SKIPS: + simplejson.loads(doc) + continue + try: + simplejson.loads(doc) + except ValueError: + pass + else: + assert False, "Expected failure for fail%d.json: %r" % (idx, doc) diff --git a/src/simplejson/simplejson/tests/test_indent.py b/src/simplejson/simplejson/tests/test_indent.py new file mode 100644 index 00000000..47dd4dc2 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_indent.py @@ -0,0 +1,41 @@ + + + +def test_indent(): + import simplejson + import textwrap + + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + {'nifty': 87}, {'field': 'yes', 'morefield': False} ] + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ], + [ + "whoops" + ], + [], + "d-shtaeou", + "d-nthiouh", + "i-vhbjkhnth", + { + "nifty": 87 + }, + { + "field": "yes", + "morefield": false + } + ]""") + + + d1 = simplejson.dumps(h) + d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) + + h1 = simplejson.loads(d1) + h2 = simplejson.loads(d2) + + assert h1 == h + assert h2 == h + assert d2 == expect diff --git a/src/simplejson/simplejson/tests/test_pass1.py b/src/simplejson/simplejson/tests/test_pass1.py new file mode 100644 index 00000000..4eda1925 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_pass1.py @@ -0,0 +1,72 @@ +# from http://json.org/JSON_checker/test/pass1.json +JSON = r''' +[ + "JSON Test Pattern pass1", + {"object with 1 member":["array with 1 element"]}, + {}, + [], + -42, + true, + false, + null, + { + "integer": 1234567890, + "real": -9876.543210, + "e": 0.123456789e-12, + "E": 1.234567890E+34, + "": 23456789012E666, + "zero": 0, + "one": 1, + "space": " ", + "quote": "\"", + "backslash": "\\", + "controls": "\b\f\n\r\t", + "slash": "/ & \/", + "alpha": "abcdefghijklmnopqrstuvwyz", + "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", + "digit": "0123456789", + "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?", + "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", + "true": true, + "false": false, + "null": null, + "array":[ ], + "object":{ }, + "address": "50 St. James Street", + "url": "http://www.JSON.org/", + "comment": "// /* <!-- --", + "# -- --> */": " ", + " s p a c e d " :[1,2 , 3 + +, + +4 , 5 , 6 ,7 ], + "compact": [1,2,3,4,5,6,7], + "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", + "quotes": "" \u0022 %22 0x22 034 "", + "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" +: "A key can be any string" + }, + 0.5 ,98.6 +, +99.44 +, + +1066 + + +,"rosebud"] +''' + +def test_parse(): + # test in/out equivalence and parsing + import simplejson + res = simplejson.loads(JSON) + out = simplejson.dumps(res) + assert res == simplejson.loads(out) + try: + simplejson.dumps(res, allow_nan=False) + except ValueError: + pass + else: + assert False, "23456789012E666 should be out of range" diff --git a/src/simplejson/simplejson/tests/test_pass2.py b/src/simplejson/simplejson/tests/test_pass2.py new file mode 100644 index 00000000..ae74abbf --- /dev/null +++ b/src/simplejson/simplejson/tests/test_pass2.py @@ -0,0 +1,11 @@ +# from http://json.org/JSON_checker/test/pass2.json +JSON = r''' +[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] +''' + +def test_parse(): + # test in/out equivalence and parsing + import simplejson + res = simplejson.loads(JSON) + out = simplejson.dumps(res) + assert res == simplejson.loads(out) diff --git a/src/simplejson/simplejson/tests/test_pass3.py b/src/simplejson/simplejson/tests/test_pass3.py new file mode 100644 index 00000000..d94893ff --- /dev/null +++ b/src/simplejson/simplejson/tests/test_pass3.py @@ -0,0 +1,16 @@ +# from http://json.org/JSON_checker/test/pass3.json +JSON = r''' +{ + "JSON Test Pattern pass3": { + "The outermost value": "must be an object or array.", + "In this test": "It is an object." + } +} +''' + +def test_parse(): + # test in/out equivalence and parsing + import simplejson + res = simplejson.loads(JSON) + out = simplejson.dumps(res) + assert res == simplejson.loads(out) diff --git a/src/simplejson/simplejson/tests/test_recursion.py b/src/simplejson/simplejson/tests/test_recursion.py new file mode 100644 index 00000000..756b0661 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_recursion.py @@ -0,0 +1,62 @@ +import simplejson + +def test_listrecursion(): + x = [] + x.append(x) + try: + simplejson.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on list recursion" + x = [] + y = [x] + x.append(y) + try: + simplejson.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on alternating list recursion" + y = [] + x = [y, y] + # ensure that the marker is cleared + simplejson.dumps(x) + +def test_dictrecursion(): + x = {} + x["test"] = x + try: + simplejson.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on dict recursion" + x = {} + y = {"a": x, "b": x} + # ensure that the marker is cleared + simplejson.dumps(x) + +class TestObject: + pass + +class RecursiveJSONEncoder(simplejson.JSONEncoder): + recurse = False + def default(self, o): + if o is TestObject: + if self.recurse: + return [TestObject] + else: + return 'TestObject' + simplejson.JSONEncoder.default(o) + +def test_defaultrecursion(): + enc = RecursiveJSONEncoder() + assert enc.encode(TestObject) == '"TestObject"' + enc.recurse = True + try: + enc.encode(TestObject) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on default recursion" diff --git a/src/simplejson/simplejson/tests/test_separators.py b/src/simplejson/simplejson/tests/test_separators.py new file mode 100644 index 00000000..a6153547 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_separators.py @@ -0,0 +1,41 @@ + + + +def test_separators(): + import simplejson + import textwrap + + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + {'nifty': 87}, {'field': 'yes', 'morefield': False} ] + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ] , + [ + "whoops" + ] , + [] , + "d-shtaeou" , + "d-nthiouh" , + "i-vhbjkhnth" , + { + "nifty" : 87 + } , + { + "field" : "yes" , + "morefield" : false + } + ]""") + + + d1 = simplejson.dumps(h) + d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : ')) + + h1 = simplejson.loads(d1) + h2 = simplejson.loads(d2) + + assert h1 == h + assert h2 == h + assert d2 == expect diff --git a/src/simplejson/simplejson/tests/test_unicode.py b/src/simplejson/simplejson/tests/test_unicode.py new file mode 100644 index 00000000..88d09393 --- /dev/null +++ b/src/simplejson/simplejson/tests/test_unicode.py @@ -0,0 +1,16 @@ +import simplejson as S + +def test_encoding1(): + encoder = S.JSONEncoder(encoding='utf-8') + u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' + s = u.encode('utf-8') + ju = encoder.encode(u) + js = encoder.encode(s) + assert ju == js + +def test_encoding2(): + u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' + s = u.encode('utf-8') + ju = S.dumps(u, encoding='utf-8') + js = S.dumps(s, encoding='utf-8') + assert ju == js -- 2.45.2