diff options
author | Bob Ippolito <bob@redivi.com> | 2005-12-31 21:13:05 +0000 |
---|---|---|
committer | Bob Ippolito <bob@redivi.com> | 2005-12-31 21:13:05 +0000 |
commit | 5aa76448ef5a7a4cb53540c58c5cf42a04e0a4b2 (patch) | |
tree | 4a24c74184daa419b341f10e459d508f77856b4f | |
download | simplejson-5aa76448ef5a7a4cb53540c58c5cf42a04e0a4b2.tar.gz |
simplejson
git-svn-id: http://simplejson.googlecode.com/svn/trunk@3 a4795897-2c25-0410-b006-0d3caba88fa1
-rw-r--r-- | docs/class-simple_json.JSONDecoder.html | 186 | ||||
-rw-r--r-- | docs/class-simple_json.JSONEncoder.html | 199 | ||||
-rw-r--r-- | docs/index.html | 448 | ||||
-rw-r--r-- | docs/layout.css | 208 | ||||
-rw-r--r-- | docs/module-index.html | 73 | ||||
-rw-r--r-- | docs/module-simple_json-index.html | 133 | ||||
-rw-r--r-- | docs/module-simple_json.html | 448 | ||||
-rw-r--r-- | docs/pudge.css | 60 | ||||
-rw-r--r-- | docs/rst.css | 142 | ||||
-rw-r--r-- | docs/simple_json/__init__.py.html | 263 | ||||
-rw-r--r-- | setup.py | 45 | ||||
-rw-r--r-- | simple_json/__init__.py | 193 | ||||
-rw-r--r-- | simple_json/decoder.py | 258 | ||||
-rw-r--r-- | simple_json/encoder.py | 275 | ||||
-rw-r--r-- | simple_json/scanner.py | 67 | ||||
-rw-r--r-- | simple_json/tests/__init__.py | 0 | ||||
-rw-r--r-- | simple_json/tests/test_fail.py | 70 | ||||
-rw-r--r-- | simple_json/tests/test_pass1.py | 72 | ||||
-rw-r--r-- | simple_json/tests/test_pass2.py | 11 | ||||
-rw-r--r-- | simple_json/tests/test_pass3.py | 16 | ||||
-rw-r--r-- | simple_json/tests/test_recursion.py | 62 |
21 files changed, 3229 insertions, 0 deletions
diff --git a/docs/class-simple_json.JSONDecoder.html b/docs/class-simple_json.JSONDecoder.html new file mode 100644 index 0000000..e8c4bc4 --- /dev/null +++ b/docs/class-simple_json.JSONDecoder.html @@ -0,0 +1,186 @@ +<!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>simple_json.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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + + <A HREF="class-simple_json.JSONDecoder.html">details</A> + + <A HREF="class-simple_json.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-simple_json.html"> + <TT>simple_json</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 (1) + </A> + + </LI> + + <LI> + <A HREF="simple_json/decoder.py.html?f=196&l=254#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-simple_json.JSONDecoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, encoding=None)</TT> + <A HREF="simple_json/decoder.py.html?f=225&l=227#225" TITLE="View Source">...</A> + </H4> + <DIV CLASS="pudge-section rst"> + + + + + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simple_json/decoder.py.html?f=196&l=254#196" TITLE="simple_json/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</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/class-simple_json.JSONEncoder.html b/docs/class-simple_json.JSONEncoder.html new file mode 100644 index 0000000..dc85ea1 --- /dev/null +++ b/docs/class-simple_json.JSONEncoder.html @@ -0,0 +1,199 @@ +<!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>simple_json.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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + + <A HREF="class-simple_json.JSONEncoder.html">details</A> + + <A HREF="class-simple_json.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-simple_json.html"> + <TT>simple_json</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 (1) + </A> + + </LI> + + <LI> + <A HREF="simple_json/encoder.py.html?f=64&l=274#64" 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(o)</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-simple_json.JSONEncoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True)</TT> + <A HREF="simple_json/encoder.py.html?f=93&l=121#93" 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> + + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simple_json/encoder.py.html?f=64&l=274#64" TITLE="simple_json/encoder.py:64">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</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html new file mode 100644 index 0000000..0870129 --- /dev/null +++ b/docs/index.html @@ -0,0 +1,448 @@ +<!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>simple_json -- 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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + + <A HREF="module-simple_json.html">details</A> + + <A HREF="module-simple_json-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>simple_json</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-simple_json-index.html" CLASS="pudge-section-link"> + Index + </A> + </LI> + <LI> + <A HREF="simple_json/__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>simple_json 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 simple_json +>>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) +'["foo", {"bar":["baz", null, 1.0, 2]}]' +>>> print simple_json.dumps("\"foo\bar") +"\"foo\bar" +>>> print simple_json.dumps(u'\u1234') +"\u1234" +>>> print simple_json.dumps('\\') +"\\" +>>> from StringIO import StringIO +>>> io = StringIO() +>>> simple_json.dump(['streaming API'], io) +>>> io.getvalue() +'["streaming API"]' +</PRE> +<P>Decoding JSON:</P> +<PRE CLASS="literal-block"> +>>> import simple_json +>>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') +[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] +>>> simple_json.loads('"\\"foo\\bar"') +u'"foo\x08ar' +>>> from StringIO import StringIO +>>> io = StringIO('["streaming API"]') +>>> simple_json.load(io) +[u'streaming API'] +</PRE> +<P>Extending JSONEncoder:</P> +<PRE CLASS="literal-block"> +>>> import simple_json +>>> class ComplexEncoder(simple_json.JSONEncoder): +... def default(self, obj): +... if isinstance(obj, complex): +... return [obj.real, obj.imag] +... return simple_json.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 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-simple_json.html#dump" CLASS="pudge-obj-link">dump</A>(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=69&l=107#69" 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>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-simple_json.html#dumps" CLASS="pudge-obj-link">dumps</A>(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=108&l=138#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> 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>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-simple_json.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=139&l=157#139" 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>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-simple_json.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=158&l=174#158" 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>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-simple_json.JSONEncoder.html" CLASS="pudge-obj-link">JSONEncoder</A>(...)</TT> + <A HREF="simple_json/encoder.py.html?f=64&l=274#64" 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(o)</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-simple_json.JSONEncoder.html#members"> + 1 member</A>. + </P> + </DIV> + </DIV> + <DIV CLASS="pudge-member class alias"> + <H4 CLASS="pudge-member-name"> + <SPAN CLASS="prefix">C</SPAN> + <TT> + <A HREF="class-simple_json.JSONDecoder.html" CLASS="pudge-obj-link">JSONDecoder</A>(...)</TT> + <A HREF="simple_json/decoder.py.html?f=196&l=254#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-simple_json.JSONDecoder.html#members"> + 1 member</A>. + </P> + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simple_json/__init__.py.html" TITLE="simple_json/__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</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/layout.css b/docs/layout.css new file mode 100644 index 0000000..a45c58b --- /dev/null +++ b/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/docs/module-index.html b/docs/module-index.html new file mode 100644 index 0000000..4775597 --- /dev/null +++ b/docs/module-index.html @@ -0,0 +1,73 @@ +<!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>simple_json 1.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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + simple_json 1.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-simple_json.html" CLASS="pudge-obj-link"> + simple_json + </A></TT> + </DT> + <DD> + A simple, fast, extensible JSON encoder and decoder + </DD> + + </DL> + +</DIV> + + <DIV ID="footer"> + + <P ID="pudge"> + Built with + <A HREF="http://lesscode.org/projects/pudge/"> + Pudge/0.1</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/module-simple_json-index.html b/docs/module-simple_json-index.html new file mode 100644 index 0000000..f40a51f --- /dev/null +++ b/docs/module-simple_json-index.html @@ -0,0 +1,133 @@ +<!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 simple_json 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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + + <A HREF="module-simple_json.html">details</A> + + <A HREF="module-simple_json-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1>Index of the <TT>simple_json</TT> module</H1> + + <UL CLASS="pudge-module-index"> + <LI CLASS="module"> + <SPAN CLASS="prefix">m</SPAN> + <A HREF="module-simple_json.html"> + <TT>simple_json</TT> + </A> + <A HREF="simple_json/__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-simple_json.html#load"> + <TT>load</TT> + </A> + <A HREF="simple_json/__init__.py.html?f=139&l=157#139">...</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-simple_json.html#dump"> + <TT>dump</TT> + </A> + <A HREF="simple_json/__init__.py.html?f=69&l=107#69">...</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-simple_json.html#dumps"> + <TT>dumps</TT> + </A> + <A HREF="simple_json/__init__.py.html?f=108&l=138#108">...</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-simple_json.html#loads"> + <TT>loads</TT> + </A> + <A HREF="simple_json/__init__.py.html?f=158&l=174#158">...</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</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/module-simple_json.html b/docs/module-simple_json.html new file mode 100644 index 0000000..0870129 --- /dev/null +++ b/docs/module-simple_json.html @@ -0,0 +1,448 @@ +<!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>simple_json -- 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">simple_json 1.1</A></H1> + <DIV CLASS="online-navigation"> + <A HREF="index.html" REL="index">index</A> + <SPAN ID="nav-docs"> + + </SPAN> + + + <BR> + + + + <A HREF="module-simple_json.html" TITLE="simple_json reference"> + simple_json + </A> + + + <A HREF="module-simple_json.html">details</A> + + <A HREF="module-simple_json-index.html"> + tree + </A> + + + </DIV> + </DIV> + + <DIV ID="main-content"> + + <H1 CLASS="pudge-member-page-heading"> + <TT>simple_json</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-simple_json-index.html" CLASS="pudge-section-link"> + Index + </A> + </LI> + <LI> + <A HREF="simple_json/__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>simple_json 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 simple_json +>>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) +'["foo", {"bar":["baz", null, 1.0, 2]}]' +>>> print simple_json.dumps("\"foo\bar") +"\"foo\bar" +>>> print simple_json.dumps(u'\u1234') +"\u1234" +>>> print simple_json.dumps('\\') +"\\" +>>> from StringIO import StringIO +>>> io = StringIO() +>>> simple_json.dump(['streaming API'], io) +>>> io.getvalue() +'["streaming API"]' +</PRE> +<P>Decoding JSON:</P> +<PRE CLASS="literal-block"> +>>> import simple_json +>>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') +[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] +>>> simple_json.loads('"\\"foo\\bar"') +u'"foo\x08ar' +>>> from StringIO import StringIO +>>> io = StringIO('["streaming API"]') +>>> simple_json.load(io) +[u'streaming API'] +</PRE> +<P>Extending JSONEncoder:</P> +<PRE CLASS="literal-block"> +>>> import simple_json +>>> class ComplexEncoder(simple_json.JSONEncoder): +... def default(self, obj): +... if isinstance(obj, complex): +... return [obj.real, obj.imag] +... return simple_json.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 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-simple_json.html#dump" CLASS="pudge-obj-link">dump</A>(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=69&l=107#69" 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>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-simple_json.html#dumps" CLASS="pudge-obj-link">dumps</A>(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=108&l=138#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> 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>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-simple_json.html#load" CLASS="pudge-obj-link">load</A>(fp, encoding=None, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=139&l=157#139" 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>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-simple_json.html#loads" CLASS="pudge-obj-link">loads</A>(s, encoding=None, cls=None)</TT> + <A HREF="simple_json/__init__.py.html?f=158&l=174#158" 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>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-simple_json.JSONEncoder.html" CLASS="pudge-obj-link">JSONEncoder</A>(...)</TT> + <A HREF="simple_json/encoder.py.html?f=64&l=274#64" 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(o)</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-simple_json.JSONEncoder.html#members"> + 1 member</A>. + </P> + </DIV> + </DIV> + <DIV CLASS="pudge-member class alias"> + <H4 CLASS="pudge-member-name"> + <SPAN CLASS="prefix">C</SPAN> + <TT> + <A HREF="class-simple_json.JSONDecoder.html" CLASS="pudge-obj-link">JSONDecoder</A>(...)</TT> + <A HREF="simple_json/decoder.py.html?f=196&l=254#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-simple_json.JSONDecoder.html#members"> + 1 member</A>. + </P> + </DIV> + </DIV> + + + + <P> + <SMALL> + + + See + <A HREF="simple_json/__init__.py.html" TITLE="simple_json/__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</A> + </P> + + </DIV> + </DIV> + </BODY> +</HTML>
\ No newline at end of file diff --git a/docs/pudge.css b/docs/pudge.css new file mode 100644 index 0000000..b370217 --- /dev/null +++ b/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/docs/rst.css b/docs/rst.css new file mode 100644 index 0000000..f48c723 --- /dev/null +++ b/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/docs/simple_json/__init__.py.html b/docs/simple_json/__init__.py.html new file mode 100644 index 0000000..cc8e849 --- /dev/null +++ b/docs/simple_json/__init__.py.html @@ -0,0 +1,263 @@ +<html><head><title>/src/simple_json/simple_json/__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">simple_json 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 simple_json</span><br /> +<a class="lnum" href="#14" name="14">0014</a><span class="string">    >>> simple_json.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 simple_json.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 simple_json.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 simple_json.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">    >>> from StringIO import StringIO</span><br /> +<a class="lnum" href="#23" name="23">0023</a><span class="string">    >>> io = StringIO()</span><br /> +<a class="lnum" href="#24" name="24">0024</a><span class="string">    >>> simple_json.dump(['streaming API'], io)</span><br /> +<a class="lnum" href="#25" name="25">0025</a><span class="string">    >>> io.getvalue()</span><br /> +<a class="lnum" href="#26" name="26">0026</a><span class="string">    '["streaming API"]'</span><br /> +<a class="lnum" href="#27" name="27">0027</a><span class="string"></span><br /> +<a class="lnum" href="#28" name="28">0028</a><span class="string">Decoding JSON::</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">    >>> import simple_json</span><br /> +<a class="lnum" href="#31" name="31">0031</a><span class="string">    >>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')</span><br /> +<a class="lnum" href="#32" name="32">0032</a><span class="string">    [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]</span><br /> +<a class="lnum" href="#33" name="33">0033</a><span class="string">    >>> simple_json.loads('"\\"foo\\bar"')</span><br /> +<a class="lnum" href="#34" name="34">0034</a><span class="string">    u'"foo\x08ar'</span><br /> +<a class="lnum" href="#35" name="35">0035</a><span class="string">    >>> from StringIO import StringIO</span><br /> +<a class="lnum" href="#36" name="36">0036</a><span class="string">    >>> io = StringIO('["streaming API"]')</span><br /> +<a class="lnum" href="#37" name="37">0037</a><span class="string">    >>> simple_json.load(io)</span><br /> +<a class="lnum" href="#38" name="38">0038</a><span class="string">    [u'streaming API']</span><br /> +<a class="lnum" href="#39" name="39">0039</a><span class="string"></span><br /> +<a class="lnum" href="#40" name="40">0040</a><span class="string">Extending JSONEncoder::</span><br /> +<a class="lnum" href="#41" name="41">0041</a><span class="string">    </span><br /> +<a class="lnum" href="#42" name="42">0042</a><span class="string">    >>> import simple_json</span><br /> +<a class="lnum" href="#43" name="43">0043</a><span class="string">    >>> class ComplexEncoder(simple_json.JSONEncoder):</span><br /> +<a class="lnum" href="#44" name="44">0044</a><span class="string">    ...     def default(self, obj):</span><br /> +<a class="lnum" href="#45" name="45">0045</a><span class="string">    ...         if isinstance(obj, complex):</span><br /> +<a class="lnum" href="#46" name="46">0046</a><span class="string">    ...             return [obj.real, obj.imag]</span><br /> +<a class="lnum" href="#47" name="47">0047</a><span class="string">    ...         return simple_json.JSONEncoder.default(self, obj)</span><br /> +<a class="lnum" href="#48" name="48">0048</a><span class="string">    ... </span><br /> +<a class="lnum" href="#49" name="49">0049</a><span class="string">    >>> dumps(2 + 1j, cls=ComplexEncoder)</span><br /> +<a class="lnum" href="#50" name="50">0050</a><span class="string">    '[2.0, 1.0]'</span><br /> +<a class="lnum" href="#51" name="51">0051</a><span class="string">    >>> ComplexEncoder().encode(2 + 1j)</span><br /> +<a class="lnum" href="#52" name="52">0052</a><span class="string">    '[2.0, 1.0]'</span><br /> +<a class="lnum" href="#53" name="53">0053</a><span class="string">    >>> list(ComplexEncoder().iterencode(2 + 1j))</span><br /> +<a class="lnum" href="#54" name="54">0054</a><span class="string">    ['[', '2.0', ', ', '1.0', ']']</span><br /> +<a class="lnum" href="#55" name="55">0055</a><span class="string">    </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">Note that the JSON produced by this module is a subset of YAML,</span><br /> +<a class="lnum" href="#58" name="58">0058</a><span class="string">so it may be used as a serializer for that as well.</span><br /> +<a class="lnum" href="#59" name="59">0059</a><span class="string">"""</span><br /> +<a class="lnum" href="#60" name="60">0060</a><span class="name">__version__</span> <span class="op">=</span> <span class="string">'1.1'</span><br /> +<a class="lnum" href="#61" name="61">0061</a><span class="name">__all__</span> <span class="op">=</span> <span class="op">[</span><br /> +<a class="lnum" href="#62" name="62">0062</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="#63" name="63">0063</a>    <span class="string">'JSONDecoder'</span><span class="op">,</span> <span class="string">'JSONEncoder'</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="keyword">from</span> <span class="name">decoder</span> <span class="keyword">import</span> <span class="name">JSONDecoder</span><br /> +<a class="lnum" href="#67" name="67">0067</a><span class="keyword">from</span> <span class="name">encoder</span> <span class="keyword">import</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#68" name="68">0068</a><br /> +<a class="lnum" href="#69" name="69">0069</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="#70" name="70">0070</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="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#71" name="71">0071</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#72" name="72">0072</a><span class="string">    Serialize ``obj`` as a JSON formatted stream to ``fp`` (a</span><br /> +<a class="lnum" href="#73" name="73">0073</a><span class="string">    ``.write()``-supporting file-like object).</span><br /> +<a class="lnum" href="#74" name="74">0074</a><span class="string"></span><br /> +<a class="lnum" href="#75" name="75">0075</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> +<a class="lnum" href="#76" name="76">0076</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> +<a class="lnum" href="#77" name="77">0077</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> +<a class="lnum" href="#78" name="78">0078</a><span class="string"></span><br /> +<a class="lnum" href="#79" name="79">0079</a><span class="string">    If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``</span><br /> +<a class="lnum" href="#80" name="80">0080</a><span class="string">    may be ``unicode`` instances, subject to normal Python ``str`` to</span><br /> +<a class="lnum" href="#81" name="81">0081</a><span class="string">    ``unicode`` coercion rules.  Unless ``fp.write()`` explicitly</span><br /> +<a class="lnum" href="#82" name="82">0082</a><span class="string">    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely</span><br /> +<a class="lnum" href="#83" name="83">0083</a><span class="string">    to cause an error.</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">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> +<a class="lnum" href="#86" name="86">0086</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> +<a class="lnum" href="#87" name="87">0087</a><span class="string">    result in an ``OverflowError`` (or worse).</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">    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> +<a class="lnum" href="#90" name="90">0090</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)</span><br /> +<a class="lnum" href="#91" name="91">0091</a><span class="string">    in strict compliance of the JSON specification, instead of using the</span><br /> +<a class="lnum" href="#92" name="92">0092</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> +<a class="lnum" href="#93" name="93">0093</a><span class="string"></span><br /> +<a class="lnum" href="#94" name="94">0094</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> +<a class="lnum" href="#95" name="95">0095</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> +<a class="lnum" href="#96" name="96">0096</a><span class="string">    the ``cls`` kwarg.</span><br /> +<a class="lnum" href="#97" name="97">0097</a><span class="string">    """</span><br /> +<a class="lnum" href="#98" name="98">0098</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="#99" name="99">0099</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#100" name="100">0100</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="#101" name="101">0101</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><br /> +<a class="lnum" href="#102" name="102">0102</a>        <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="#103" name="103">0103</a>    <span class="comment"># could accelerate with writelines in some versions of Python, at</span><br /> +<a class="lnum" href="#104" name="104">0104</a><span class="comment"></span>    <span class="comment"># a debuggability cost</span><br /> +<a class="lnum" href="#105" name="105">0105</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="#106" name="106">0106</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="#107" name="107">0107</a><br /> +<a class="lnum" href="#108" name="108">0108</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="#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="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#110" name="110">0110</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#111" name="111">0111</a><span class="string">    Serialize ``obj`` to a JSON formatted ``str``.</span><br /> +<a class="lnum" href="#112" name="112">0112</a><span class="string"></span><br /> +<a class="lnum" href="#113" name="113">0113</a><span class="string">    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types</span><br /> +<a class="lnum" href="#114" name="114">0114</a><span class="string">    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) </span><br /> +<a class="lnum" href="#115" name="115">0115</a><span class="string">    will be skipped instead of raising a ``TypeError``.</span><br /> +<a class="lnum" href="#116" name="116">0116</a><span class="string"></span><br /> +<a class="lnum" href="#117" name="117">0117</a><span class="string">    If ``ensure_ascii`` is ``False``, then the return value will be a</span><br /> +<a class="lnum" href="#118" name="118">0118</a><span class="string">    ``unicode`` instance subject to normal Python ``str`` to ``unicode``</span><br /> +<a class="lnum" href="#119" name="119">0119</a><span class="string">    coercion rules instead of being escaped to an ASCII ``str``.</span><br /> +<a class="lnum" href="#120" name="120">0120</a><span class="string"></span><br /> +<a class="lnum" href="#121" name="121">0121</a><span class="string">    If ``check_circular`` is ``False``, then the circular reference check</span><br /> +<a class="lnum" href="#122" name="122">0122</a><span class="string">    for container types will be skipped and a circular reference will</span><br /> +<a class="lnum" href="#123" name="123">0123</a><span class="string">    result in an ``OverflowError`` (or worse).</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 ``allow_nan`` is ``False``, then it will be a ``ValueError`` to</span><br /> +<a class="lnum" href="#126" name="126">0126</a><span class="string">    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in</span><br /> +<a class="lnum" href="#127" name="127">0127</a><span class="string">    strict compliance of the JSON specification, instead of using the</span><br /> +<a class="lnum" href="#128" name="128">0128</a><span class="string">    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).</span><br /> +<a class="lnum" href="#129" name="129">0129</a><span class="string"></span><br /> +<a class="lnum" href="#130" name="130">0130</a><span class="string">    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the</span><br /> +<a class="lnum" href="#131" name="131">0131</a><span class="string">    ``.default()`` method to serialize additional types), specify it with</span><br /> +<a class="lnum" href="#132" name="132">0132</a><span class="string">    the ``cls`` kwarg.</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="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="#135" name="135">0135</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONEncoder</span><br /> +<a class="lnum" href="#136" name="136">0136</a>    <span class="keyword">return</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="#137" name="137">0137</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="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="#138" name="138">0138</a><br /> +<a class="lnum" href="#139" name="139">0139</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="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#140" name="140">0140</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#141" name="141">0141</a><span class="string">    Deserialize ``fp`` (a ``.read()``-supporting file-like object containing</span><br /> +<a class="lnum" href="#142" name="142">0142</a><span class="string">    a JSON document) to a Python object.</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">    If the contents of ``fp`` is encoded with an ASCII based encoding other</span><br /> +<a class="lnum" href="#145" name="145">0145</a><span class="string">    than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must</span><br /> +<a class="lnum" href="#146" name="146">0146</a><span class="string">    be specified.  Encodings that are not ASCII based (such as UCS-2) are</span><br /> +<a class="lnum" href="#147" name="147">0147</a><span class="string">    not allowed, and should be wrapped with</span><br /> +<a class="lnum" href="#148" name="148">0148</a><span class="string">    ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``</span><br /> +<a class="lnum" href="#149" name="149">0149</a><span class="string">    object and passed to ``loads()``</span><br /> +<a class="lnum" href="#150" name="150">0150</a><span class="string">    </span><br /> +<a class="lnum" href="#151" name="151">0151</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> +<a class="lnum" href="#152" name="152">0152</a><span class="string">    kwarg.</span><br /> +<a class="lnum" href="#153" name="153">0153</a><span class="string">    """</span><br /> +<a class="lnum" href="#154" name="154">0154</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="#155" name="155">0155</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONDecoder</span><br /> +<a class="lnum" href="#156" name="156">0156</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">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="#157" name="157">0157</a><br /> +<a class="lnum" href="#158" name="158">0158</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="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br /> +<a class="lnum" href="#159" name="159">0159</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#160" name="160">0160</a><span class="string">    Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON</span><br /> +<a class="lnum" href="#161" name="161">0161</a><span class="string">    document) to a Python object.</span><br /> +<a class="lnum" href="#162" name="162">0162</a><span class="string"></span><br /> +<a class="lnum" href="#163" name="163">0163</a><span class="string">    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding</span><br /> +<a class="lnum" href="#164" name="164">0164</a><span class="string">    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name</span><br /> +<a class="lnum" href="#165" name="165">0165</a><span class="string">    must be specified.  Encodings that are not ASCII based (such as UCS-2)</span><br /> +<a class="lnum" href="#166" name="166">0166</a><span class="string">    are not allowed and should be decoded to ``unicode`` first.</span><br /> +<a class="lnum" href="#167" name="167">0167</a><span class="string"></span><br /> +<a class="lnum" href="#168" name="168">0168</a><span class="string">    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``</span><br /> +<a class="lnum" href="#169" name="169">0169</a><span class="string">    kwarg.</span><br /> +<a class="lnum" href="#170" name="170">0170</a><span class="string">    """</span><br /> +<a class="lnum" href="#171" name="171">0171</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="#172" name="172">0172</a>        <span class="name">cls</span> <span class="op">=</span> <span class="name">JSONDecoder</span><br /> +<a class="lnum" href="#173" name="173">0173</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="#174" name="174">0174</a><br /> +<a class="lnum" href="#175" name="175">0175</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="#176" name="176">0176</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#177" name="177">0177</a><span class="string">    json-py API compatibility hook.  Use loads(s) instead.</span><br /> +<a class="lnum" href="#178" name="178">0178</a><span class="string">    """</span><br /> +<a class="lnum" href="#179" name="179">0179</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#180" name="180">0180</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simple_json.loads(s) should be used instead of read(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#181" name="181">0181</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#182" name="182">0182</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="#183" name="183">0183</a><br /> +<a class="lnum" href="#184" name="184">0184</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="#185" name="185">0185</a>    <span class="string">"""</span><br /> +<a class="lnum" href="#186" name="186">0186</a><span class="string">    json-py API compatibility hook.  Use dumps(s) instead.</span><br /> +<a class="lnum" href="#187" name="187">0187</a><span class="string">    """</span><br /> +<a class="lnum" href="#188" name="188">0188</a>    <span class="keyword">import</span> <span class="name">warnings</span><br /> +<a class="lnum" href="#189" name="189">0189</a>    <span class="name">warnings</span><span class="op">.</span><span class="name">warn</span><span class="op">(</span><span class="string">"simple_json.dumps(s) should be used instead of write(s)"</span><span class="op">,</span><br /> +<a class="lnum" href="#190" name="190">0190</a>        <span class="name">DeprecationWarning</span><span class="op">)</span><br /> +<a class="lnum" href="#191" name="191">0191</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/setup.py b/setup.py new file mode 100644 index 0000000..f48cba0 --- /dev/null +++ b/setup.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +import ez_setup +ez_setup.use_setuptools() + +from setuptools import setup, find_packages + +VERSION = '1.1' +DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" +LONG_DESCRIPTION = """ +simple_json 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. + +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())) + +setup( + name="simple_json", + version=VERSION, + description=DESCRIPTION, + long_description=LONG_DESCRIPTION, + classifiers=CLASSIFIERS, + author="Bob Ippolito", + author_email="bob@redivi.com", + url="http://undefined.org/python/#simple_json", + license="MIT License", + packages=find_packages(exclude=['ez_setup']), + platforms=['any'], + test_suite="nose.collector", + zip_safe=True, +) diff --git a/simple_json/__init__.py b/simple_json/__init__.py new file mode 100644 index 0000000..5354999 --- /dev/null +++ b/simple_json/__init__.py @@ -0,0 +1,193 @@ +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. + +simple_json exposes an API familiar to uses of the standard library +marshal and pickle modules. + +Encoding basic Python object hierarchies:: + + >>> import simple_json + >>> simple_json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) + '["foo", {"bar":["baz", null, 1.0, 2]}]' + >>> print simple_json.dumps("\"foo\bar") + "\"foo\bar" + >>> print simple_json.dumps(u'\u1234') + "\u1234" + >>> print simple_json.dumps('\\') + "\\" + >>> from StringIO import StringIO + >>> io = StringIO() + >>> simple_json.dump(['streaming API'], io) + >>> io.getvalue() + '["streaming API"]' + +Decoding JSON:: + + >>> import simple_json + >>> simple_json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') + [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] + >>> simple_json.loads('"\\"foo\\bar"') + u'"foo\x08ar' + >>> from StringIO import StringIO + >>> io = StringIO('["streaming API"]') + >>> simple_json.load(io) + [u'streaming API'] + +Extending JSONEncoder:: + + >>> import simple_json + >>> class ComplexEncoder(simple_json.JSONEncoder): + ... def default(self, obj): + ... if isinstance(obj, complex): + ... return [obj.real, obj.imag] + ... return simple_json.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 is a subset of YAML, +so it may be used as a serializer for that as well. +""" +__version__ = '1.1' +__all__ = [ + 'dump', 'dumps', 'load', 'loads', + 'JSONDecoder', 'JSONEncoder', +] + +from decoder import JSONDecoder +from encoder import JSONEncoder + +def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, **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``). + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + if cls is None: + cls = JSONEncoder + iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, + **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, **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``). + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + """ + if cls is None: + cls = JSONEncoder + return cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, **kw).encode(obj) + +def load(fp, encoding=None, cls=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()`` + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + """ + if cls is None: + cls = JSONDecoder + return cls(encoding=encoding, **kw).decode(fp.read()) + +def loads(s, encoding=None, cls=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. + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + """ + if cls is None: + cls = JSONDecoder + return cls(encoding=encoding, **kw).decode(s) + +def read(s): + """ + json-py API compatibility hook. Use loads(s) instead. + """ + import warnings + warnings.warn("simple_json.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("simple_json.dumps(s) should be used instead of write(s)", + DeprecationWarning) + return dumps(obj) + + diff --git a/simple_json/decoder.py b/simple_json/decoder.py new file mode 100644 index 0000000..ec52215 --- /dev/null +++ b/simple_json/decoder.py @@ -0,0 +1,258 @@ +""" +Implementation of JSONDecoder +""" +import re + +from simple_json.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) + +def JSONInfinity(match, context): + return PosInf, None +pattern('Infinity')(JSONInfinity) + +def JSONNegInfinity(match, context): + return NegInf, None +pattern('-Infinity')(JSONNegInfinity) + +def JSONNaN(match, context): + return NaN, None +pattern('NaN')(JSONNaN) + +def JSONTrue(match, context): + return True, None +pattern('true')(JSONTrue) + +def JSONFalse(match, context): + return False, None +pattern('false')(JSONFalse) + +def JSONNull(match, context): + return None, None +pattern('null')(JSONNull) + +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) +STRINGBACKSLASH = re.compile(r'([\\/bfnrt"]|u[A-Fa-f0-9]{4})', 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): + if encoding is None: + encoding = DEFAULT_ENCODING + chunks = [] + while 1: + chunk = STRINGCHUNK.match(s, end) + end = chunk.end() + m = chunk.group(1) + if m == '"': + break + if m == '\\': + chunk = STRINGBACKSLASH.match(s, end) + if chunk is None: + raise ValueError(errmsg("Invalid \\escape", s, end)) + end = chunk.end() + esc = chunk.group(1) + try: + m = BACKSLASH[esc] + except KeyError: + m = unichr(int(esc[1:], 16)) + if not isinstance(m, unicode): + m = unicode(m, encoding) + chunks.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 skipwhitespace(s, end): + m = WHITESPACE.match(s, end) + if m is not None: + return m.end() + return end + +def JSONObject(match, context): + pairs = {} + s = match.string + end = skipwhitespace(s, match.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) + while True: + key, end = scanstring(s, end, encoding) + end = skipwhitespace(s, end) + if s[end:end + 1] != ':': + raise ValueError(errmsg("Expecting : delimiter", s, end)) + end = skipwhitespace(s, end + 1) + try: + value, end = JSONScanner.iterscan(s, idx=end).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + pairs[key] = value + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar == '}': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end - 1)) + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar != '"': + raise ValueError(errmsg("Expecting property name", s, end - 1)) + return pairs, end +pattern(r'{')(JSONObject) + +def JSONArray(match, context): + values = [] + s = match.string + end = skipwhitespace(s, match.end()) + # look-ahead for trivial empty array + nextchar = s[end:end + 1] + if nextchar == ']': + return values, end + 1 + while True: + try: + value, end = JSONScanner.iterscan(s, idx=end).next() + except StopIteration: + raise ValueError(errmsg("Expecting object", s, end)) + values.append(value) + end = skipwhitespace(s, end) + nextchar = s[end:end + 1] + end += 1 + if nextchar == ']': + break + if nextchar != ',': + raise ValueError(errmsg("Expecting , delimiter", s, end)) + end = skipwhitespace(s, end) + return values, end +pattern(r'\[')(JSONArray) + +ANYTHING = [ + JSONTrue, + JSONFalse, + JSONNull, + JSONNaN, + JSONInfinity, + JSONNegInfinity, + JSONNumber, + JSONString, + JSONArray, + JSONObject, +] + +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) + + def __init__(self, encoding=None): + self.encoding = encoding + + 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 + + def decode(self, s): + """ + Return the Python representation of ``s`` (a ``str`` or ``unicode`` + instance containing a JSON document) + """ + obj, end = self.raw_decode(s, idx=skipwhitespace(s, 0)) + end = skipwhitespace(s, end) + if end != len(s): + raise ValueError(errmsg("Extra data", s, end, len(s))) + return obj + +__all__ = ['JSONDecoder'] diff --git a/simple_json/encoder.py b/simple_json/encoder.py new file mode 100644 index 0000000..71dfdb4 --- /dev/null +++ b/simple_json/encoder.py @@ -0,0 +1,275 @@ +""" +Implementation of JSONEncoder +""" +import re +import math + +# this should match any kind of infinity +INFCHARS = re.compile(r'[infINF]') +ESCAPE = re.compile(r'[\x00-\x19\\"\b\f\n\r\t]') +ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') +ESCAPE_DCT = { + '\\': '\\\\', + '"': '\\"', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', +} +for i in range(20): + ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) + +def floatstr(o, allow_nan=True): + s = str(o) + # If the first non-sign is a digit then it's not a special value + if (o < 0.0 and s[1].isdigit()) or s[0].isdigit(): + return s + elif not allow_nan: + raise ValueError("Out of range float values are not JSON compliant: %r" + % (o,)) + # These are the string representations on the platforms I've tried + if s == 'nan': + return 'NaN' + if s == 'inf': + return 'Infinity' + if s == '-inf': + return '-Infinity' + # NaN should either be inequal to itself, or equal to everything + if o != o or o == 0.0: + return 'NaN' + # Last ditch effort, assume inf + if o < 0: + return '-Infinity' + return 'Infinity' + +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: + return '\\u%04x' % (ord(s),) + return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' + + +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(o)`` method with another method that returns a serializable + object for ``o`` if possible, otherwise it should call the superclass + implementation (to raise ``TypeError``). + """ + def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True): + """ + 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. + """ + + self.skipkeys = skipkeys + self.ensure_ascii = ensure_ascii + self.check_circular = check_circular + self.allow_nan = allow_nan + + 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 '[' + first = True + for value in lst: + if first: + first = False + else: + yield ', ' + for chunk in self._iterencode(value, markers): + yield chunk + 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 '{' + first = True + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + allow_nan = self.allow_nan + for key, value in dct.iteritems(): + if 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 ', ' + yield encoder(key) + yield ':' + for chunk in self._iterencode(value, markers): + yield chunk + yield '}' + if markers is not None: + del markers[markerid] + + 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) + + def _iterencode(self, o, markers=None): + if isinstance(o, basestring): + if self.ensure_ascii: + encoder = encode_basestring_ascii + else: + encoder = encode_basestring + 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. + """ + # 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) + +__all__ = ['JSONEncoder'] diff --git a/simple_json/scanner.py b/simple_json/scanner.py new file mode 100644 index 0000000..95d3f2b --- /dev/null +++ b/simple_json/scanner.py @@ -0,0 +1,67 @@ +""" +Iterator based sre token scanner +""" +import sre_parse, sre_compile, sre_constants +from sre_constants import BRANCH, SUBPATTERN +from sre 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 + +def InsignificantWhitespace(match, context): + return None, None +pattern(r'\s+')(InsignificantWhitespace) diff --git a/simple_json/tests/__init__.py b/simple_json/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/simple_json/tests/__init__.py diff --git a/simple_json/tests/test_fail.py b/simple_json/tests/test_fail.py new file mode 100644 index 0000000..41d94b0 --- /dev/null +++ b/simple_json/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 simple_json + for idx, doc in enumerate(JSONDOCS): + idx = idx + 1 + if idx in SKIPS: + simple_json.loads(doc) + continue + try: + simple_json.loads(doc) + except ValueError: + pass + else: + assert False, "Expected failure for fail%d.json: %r" % (idx, doc) diff --git a/simple_json/tests/test_pass1.py b/simple_json/tests/test_pass1.py new file mode 100644 index 0000000..d20ef11 --- /dev/null +++ b/simple_json/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 simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) + try: + simple_json.dumps(res, allow_nan=False) + except ValueError: + pass + else: + assert False, "23456789012E666 should be out of range" diff --git a/simple_json/tests/test_pass2.py b/simple_json/tests/test_pass2.py new file mode 100644 index 0000000..6ee1787 --- /dev/null +++ b/simple_json/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 simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) diff --git a/simple_json/tests/test_pass3.py b/simple_json/tests/test_pass3.py new file mode 100644 index 0000000..53d7dfa --- /dev/null +++ b/simple_json/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 simple_json + res = simple_json.loads(JSON) + out = simple_json.dumps(res) + assert res == simple_json.loads(out) diff --git a/simple_json/tests/test_recursion.py b/simple_json/tests/test_recursion.py new file mode 100644 index 0000000..360db2b --- /dev/null +++ b/simple_json/tests/test_recursion.py @@ -0,0 +1,62 @@ +import simple_json + +def test_listrecursion(): + x = [] + x.append(x) + try: + simple_json.dumps(x) + except ValueError: + pass + else: + assert False, "didn't raise ValueError on list recursion" + x = [] + y = [x] + x.append(y) + try: + simple_json.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 + simple_json.dumps(x) + +def test_dictrecursion(): + x = {} + x["test"] = x + try: + simple_json.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 + simple_json.dumps(x) + +class TestObject: + pass + +class RecursiveJSONEncoder(simple_json.JSONEncoder): + recurse = False + def default(self, o): + if o is TestObject: + if self.recurse: + return [TestObject] + else: + return 'TestObject' + simple_json.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" |