OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://
www.w3.org/1999/xhtml"> |
| 3 <head> |
| 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
| 5 <title>simplejson — JSON encoder and decoder — simplejson v2.0.0
documentation</title> |
| 6 <link rel="stylesheet" href="_static/default.css" type="text/css" /> |
| 7 <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
| 8 <script type="text/javascript"> |
| 9 var DOCUMENTATION_OPTIONS = { |
| 10 URL_ROOT: '', |
| 11 VERSION: '2.0.0', |
| 12 COLLAPSE_MODINDEX: false, |
| 13 FILE_SUFFIX: '.html' |
| 14 }; |
| 15 </script> |
| 16 <script type="text/javascript" src="_static/jquery.js"></script> |
| 17 <script type="text/javascript" src="_static/interface.js"></script> |
| 18 <script type="text/javascript" src="_static/doctools.js"></script> |
| 19 <link rel="contents" title="Global table of contents" href="contents.html" /
> |
| 20 <link rel="index" title="Global index" href="genindex.html" /> |
| 21 <link rel="search" title="Search" href="search.html" /> |
| 22 <link rel="top" title="simplejson v2.0.0 documentation" href="" /> |
| 23 </head> |
| 24 <body> |
| 25 <div class="related"> |
| 26 <h3>Navigation</h3> |
| 27 <ul> |
| 28 <li class="right" style="margin-right: 10px"> |
| 29 <a href="genindex.html" title="General Index" |
| 30 accesskey="I">index</a></li> |
| 31 <li><a href="">simplejson v2.0.0 documentation</a> »</li> |
| 32 </ul> |
| 33 </div> |
| 34 <div class="document"> |
| 35 <div class="documentwrapper"> |
| 36 <div class="bodywrapper"> |
| 37 <div class="body"> |
| 38 |
| 39 |
| 40 <div class="section" id="module-simplejson"> |
| 41 <h1 id="module-simplejson"><tt class="xref docutils literal"><span class="pre">s
implejson</span></tt> — JSON encoder and decoder<a class="headerlink" href
="#module-simplejson" title="Permalink to this headline">¶</a></h1> |
| 42 <p>JSON (JavaScript Object Notation) <<a class="reference external" href="htt
p://json.org">http://json.org</a>> is a subset of JavaScript |
| 43 syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.</p> |
| 44 <p><tt class="xref docutils literal"><span class="pre">simplejson</span></tt> ex
poses an API familiar to users of the standard library |
| 45 <tt class="xref docutils literal"><span class="pre">marshal</span></tt> and <tt
class="xref docutils literal"><span class="pre">pickle</span></tt> modules. It i
s the externally maintained |
| 46 version of the <tt class="xref docutils literal"><span class="pre">json</span></
tt> library contained in Python 2.6, but maintains |
| 47 compatibility with Python 2.4 and Python 2.5 and (currently) has |
| 48 significant performance advantages, even without using the optional C |
| 49 extension for speedups.</p> |
| 50 <p>Encoding basic Python object hierarchies:</p> |
| 51 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 52 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">dumps</span><span class="p">([</span><span class="s">
9;foo'</span><span class="p">,</span> <span class="p">{</span><span class="s
">'bar'</span><span class="p">:</span> <span class="p">(</span><span cla
ss="s">'baz'</span><span class="p">,</span> <span class="bp">None</span>
<span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <sp
an class="mf">2</span><span class="p">)}])</span> |
| 53 <span class="go">'["foo", {"bar": ["baz", null
, 1.0, 2]}]'</span> |
| 54 <span class="gp">>>> </span><span class="k">print</span> <span class="n
">json</span><span class="o">.</span><span class="n">dumps</span><span class="p"
>(</span><span class="s">"</span><span class="se">\"</span><span class
="s">foo</span><span class="se">\b</span><span class="s">ar"</span><span cl
ass="p">)</span> |
| 55 <span class="go">"\"foo\bar"</span> |
| 56 <span class="gp">>>> </span><span class="k">print</span> <span class="n
">json</span><span class="o">.</span><span class="n">dumps</span><span class="p"
>(</span><span class="s">u'</span><span class="se">\u1234</span><span class=
"s">'</span><span class="p">)</span> |
| 57 <span class="go">"\u1234"</span> |
| 58 <span class="gp">>>> </span><span class="k">print</span> <span class="n
">json</span><span class="o">.</span><span class="n">dumps</span><span class="p"
>(</span><span class="s">'</span><span class="se">\\</span><span class="s">&
#39;</span><span class="p">)</span> |
| 59 <span class="go">"\\"</span> |
| 60 <span class="gp">>>> </span><span class="k">print</span> <span class="n
">json</span><span class="o">.</span><span class="n">dumps</span><span class="p"
>({</span><span class="s">"c"</span><span class="p">:</span> <span cla
ss="mf">0</span><span class="p">,</span> <span class="s">"b"</span><sp
an class="p">:</span> <span class="mf">0</span><span class="p">,</span> <span cl
ass="s">"a"</span><span class="p">:</span> <span class="mf">0</span><s
pan class="p">},</span> <span class="n">sort_keys</span><span class="o">=</span>
<span class="bp">True</span><span class="p">)</span> |
| 61 <span class="go">{"a": 0, "b": 0, "c": 0}</span> |
| 62 <span class="gp">>>> </span><span class="k">from</span> <span class="nn
">StringIO</span> <span class="k">import</span> <span class="n">StringIO</span> |
| 63 <span class="gp">>>> </span><span class="n">io</span> <span class="o">=
</span> <span class="n">StringIO</span><span class="p">()</span> |
| 64 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">dump</span><span class="p">([</span><span class="s">'
;streaming API'</span><span class="p">],</span> <span class="n">io</span><sp
an class="p">)</span> |
| 65 <span class="gp">>>> </span><span class="n">io</span><span class="o">.<
/span><span class="n">getvalue</span><span class="p">()</span> |
| 66 <span class="go">'["streaming API"]'</span> |
| 67 </pre></div> |
| 68 <p>Compact encoding:</p> |
| 69 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 70 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">dumps</span><span class="p">([</span><span class="mf">1<
/span><span class="p">,</span><span class="mf">2</span><span class="p">,</span><
span class="mf">3</span><span class="p">,{</span><span class="s">'4'</sp
an><span class="p">:</span> <span class="mf">5</span><span class="p">,</span> <s
pan class="s">'6'</span><span class="p">:</span> <span class="mf">7</spa
n><span class="p">}],</span> <span class="n">separators</span><span class="o">=<
/span><span class="p">(</span><span class="s">','</span><span class="p">
,</span><span class="s">':'</span><span class="p">))</span> |
| 71 <span class="go">'[1,2,3,{"4":5,"6":7}]'</span> |
| 72 </pre></div> |
| 73 <p>Pretty printing:</p> |
| 74 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 75 <span class="gp">>>> </span><span class="n">s</span> <span class="o">=<
/span> <span class="n">json</span><span class="o">.</span><span class="n">dumps<
/span><span class="p">({</span><span class="s">'4'</span><span class="p"
>:</span> <span class="mf">5</span><span class="p">,</span> <span class="s">'
;6'</span><span class="p">:</span> <span class="mf">7</span><span class="p">
},</span> <span class="n">sort_keys</span><span class="o">=</span><span class="b
p">True</span><span class="p">,</span> <span class="n">indent</span><span class=
"o">=</span><span class="mf">4</span><span class="p">)</span> |
| 76 <span class="gp">>>> </span><span class="k">print</span> <span class="s
">'</span><span class="se">\n</span><span class="s">'</span><span class=
"o">.</span><span class="n">join</span><span class="p">([</span><span class="n">
l</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()<
/span> <span class="k">for</span> <span class="n">l</span> <span class="ow">in</
span> <span class="n">s</span><span class="o">.</span><span class="n">splitline
s</span><span class="p">()])</span> |
| 77 <span class="go">{</span> |
| 78 <span class="go"> "4": 5,</span> |
| 79 <span class="go"> "6": 7</span> |
| 80 <span class="go">}</span> |
| 81 </pre></div> |
| 82 <p>Decoding JSON:</p> |
| 83 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 84 <span class="gp">>>> </span><span class="n">obj</span> <span class="o">
=</span> <span class="p">[</span><span class="s">u'foo'</span><span clas
s="p">,</span> <span class="p">{</span><span class="s">u'bar'</span><spa
n class="p">:</span> <span class="p">[</span><span class="s">u'baz'</spa
n><span class="p">,</span> <span class="bp">None</span><span class="p">,</span>
<span class="mf">1.0</span><span class="p">,</span> <span class="mf">2</span><sp
an class="p">]}]</span> |
| 85 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">loads</span><span class="p">(</span><span class="s">'
;["foo", {"bar":["baz", null, 1.0, 2]}]'</span
><span class="p">)</span> <span class="o">==</span> <span class="n">obj</span> |
| 86 <span class="go">True</span> |
| 87 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">loads</span><span class="p">(</span><span class="s">'
;"</span><span class="se">\\</span><span class="s">"foo</span><span cl
ass="se">\\</span><span class="s">bar"'</span><span class="p">)</span>
<span class="o">==</span> <span class="s">u'"foo</span><span class="se"
>\x08</span><span class="s">ar'</span> |
| 88 <span class="go">True</span> |
| 89 <span class="gp">>>> </span><span class="k">from</span> <span class="nn
">StringIO</span> <span class="k">import</span> <span class="n">StringIO</span> |
| 90 <span class="gp">>>> </span><span class="n">io</span> <span class="o">=
</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">&
#39;["streaming API"]'</span><span class="p">)</span> |
| 91 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">load</span><span class="p">(</span><span class="n">io</s
pan><span class="p">)[</span><span class="mf">0</span><span class="p">]</span> <
span class="o">==</span> <span class="s">'streaming API'</span> |
| 92 <span class="go">True</span> |
| 93 </pre></div> |
| 94 <p>Specializing JSON object decoding:</p> |
| 95 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 96 <span class="gp">>>> </span><span class="k">def</span> <span class="nf"
>as_complex</span><span class="p">(</span><span class="n">dct</span><span class=
"p">):</span> |
| 97 <span class="gp">... </span> <span class="k">if</span> <span class="s">'_
_complex__'</span> <span class="ow">in</span> <span class="n">dct</span><spa
n class="p">:</span> |
| 98 <span class="gp">... </span> <span class="k">return</span> <span class="n
b">complex</span><span class="p">(</span><span class="n">dct</span><span class="
p">[</span><span class="s">'real'</span><span class="p">],</span> <span
class="n">dct</span><span class="p">[</span><span class="s">'imag'</span
><span class="p">])</span> |
| 99 <span class="gp">... </span> <span class="k">return</span> <span class="n">dc
t</span> |
| 100 <span class="gp">...</span> |
| 101 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">loads</span><span class="p">(</span><span class="s">'
;{"__complex__": true, "real": 1, "imag": 2}'<
/span><span class="p">,</span> |
| 102 <span class="gp">... </span> <span class="n">object_hook</span><span class="o
">=</span><span class="n">as_complex</span><span class="p">)</span> |
| 103 <span class="go">(1+2j)</span> |
| 104 <span class="gp">>>> </span><span class="k">import</span> <span class="
nn">decimal</span> |
| 105 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">loads</span><span class="p">(</span><span class="s">'
;1.1'</span><span class="p">,</span> <span class="n">parse_float</span><span
class="o">=</span><span class="n">decimal</span><span class="o">.</span><span c
lass="n">Decimal</span><span class="p">)</span> <span class="o">==</span> <span
class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><s
pan class="p">(</span><span class="s">'1.1'</span><span class="p">)</spa
n> |
| 106 <span class="go">True</span> |
| 107 </pre></div> |
| 108 <p>Specializing JSON object encoding:</p> |
| 109 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 110 <span class="gp">>>> </span><span class="k">def</span> <span class="nf"
>encode_complex</span><span class="p">(</span><span class="n">obj</span><span cl
ass="p">):</span> |
| 111 <span class="gp">... </span> <span class="k">if</span> <span class="nb">isins
tance</span><span class="p">(</span><span class="n">obj</span><span class="p">,<
/span> <span class="nb">complex</span><span class="p">):</span> |
| 112 <span class="gp">... </span> <span class="k">return</span> <span class="p
">[</span><span class="n">obj</span><span class="o">.</span><span class="n">real
</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</spa
n><span class="n">imag</span><span class="p">]</span> |
| 113 <span class="gp">... </span> <span class="k">raise</span> <span class="ne">Ty
peError</span><span class="p">(</span><span class="nb">repr</span><span class="p
">(</span><span class="n">o</span><span class="p">)</span> <span class="o">+</sp
an> <span class="s">" is not JSON serializable"</span><span class="p">
)</span> |
| 114 <span class="gp">...</span> |
| 115 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">dumps</span><span class="p">(</span><span class="mf">2</
span> <span class="o">+</span> <span class="mf">1</span><span class="n">j</span>
<span class="p">,</span> <span class="n">default</span><span class="o">=</span><
span class="n">encode_complex</span><span class="p">)</span> |
| 116 <span class="go">'[2.0, 1.0]'</span> |
| 117 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">JSONEncoder</span><span class="p">(</span><span class="n
">default</span><span class="o">=</span><span class="n">encode_complex</span><sp
an class="p">)</span><span class="o">.</span><span class="n">encode</span><span
class="p">(</span><span class="mf">2</span> <span class="o">+</span> <span class
="mf">1</span><span class="n">j</span><span class="p">)</span> |
| 118 <span class="go">'[2.0, 1.0]'</span> |
| 119 <span class="gp">>>> </span><span class="s">''</span><span clas
s="o">.</span><span class="n">join</span><span class="p">(</span><span class="n"
>json</span><span class="o">.</span><span class="n">JSONEncoder</span><span clas
s="p">(</span><span class="n">default</span><span class="o">=</span><span class=
"n">encode_complex</span><span class="p">)</span><span class="o">.</span><span c
lass="n">iterencode</span><span class="p">(</span><span class="mf">2</span> <spa
n class="o">+</span> <span class="mf">1</span><span class="n">j</span><span clas
s="p">))</span> |
| 120 <span class="go">'[2.0, 1.0]'</span> |
| 121 </pre></div> |
| 122 <p>Using <tt class="xref docutils literal"><span class="pre">simplejson.tool</sp
an></tt> from the shell to validate and pretty-print:</p> |
| 123 <div class="highlight"><pre>$ echo '{"json":"obj"}'
| python -m simplejson.tool |
| 124 { |
| 125 "json": "obj" |
| 126 } |
| 127 $ echo '{ 1.2:3.4}' | python -m simplejson.tool |
| 128 Expecting property name: line 1 column 2 (char 2) |
| 129 </pre></div> |
| 130 <div class="admonition note"> |
| 131 <p class="first admonition-title">Note</p> |
| 132 <p class="last">The JSON produced by this module’s default settings is a s
ubset of |
| 133 YAML, so it may be used as a serializer for that as well.</p> |
| 134 </div> |
| 135 <div class="section" id="basic-usage"> |
| 136 <h2 id="basic-usage">Basic Usage<a class="headerlink" href="#basic-usage" title=
"Permalink to this headline">¶</a></h2> |
| 137 <dl class="function"> |
| 138 <dt id="simplejson.dump"> |
| 139 <!--[simplejson.dump]--><tt class="descclassname">simplejson.</tt><tt class="des
cname">dump</tt><big>(</big><em>obj</em>, <em>fp</em><span class="optional">[</s
pan>, <em>skipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><sp
an class="optional">[</span>, <em>check_circular</em><span class="optional">[</s
pan>, <em>allow_nan</em><span class="optional">[</span>, <em>cls</em><span class
="optional">[</span>, <em>indent</em><span class="optional">[</span>, <em>separa
tors</em><span class="optional">[</span>, <em>encoding</em><span class="optional
">[</span>, <em>default</em><span class="optional">[</span>, <em>**kw</em><span
class="optional">]</span><span class="optional">]</span><span class="optional">]
</span><span class="optional">]</span><span class="optional">]</span><span class
="optional">]</span><span class="optional">]</span><span class="optional">]</spa
n><span class="optional">]</span><span class="optional">]</span><big>)</big><a c
lass="headerlink" href="#simplejson.dump" title="Permalink to this definition">¶
</a></dt> |
| 140 <dd><p>Serialize <em>obj</em> as a JSON formatted stream to <em>fp</em> (a <tt c
lass="docutils literal"><span class="pre">.write()</span></tt>-supporting |
| 141 file-like object).</p> |
| 142 <p>If <em>skipkeys</em> is true (default: <tt class="xref docutils literal"><spa
n class="pre">False</span></tt>), then dict keys that are not |
| 143 of a basic type (<tt class="xref docutils literal"><span class="pre">str</span><
/tt>, <tt class="xref docutils literal"><span class="pre">unicode</span></tt>, <
tt class="xref docutils literal"><span class="pre">int</span></tt>, <tt class="x
ref docutils literal"><span class="pre">long</span></tt>, |
| 144 <tt class="xref docutils literal"><span class="pre">float</span></tt>, <tt class
="xref docutils literal"><span class="pre">bool</span></tt>, <tt class="xref doc
utils literal"><span class="pre">None</span></tt>) will be skipped instead of ra
ising a |
| 145 <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>.</p> |
| 146 <p>If <em>ensure_ascii</em> is false (default: <tt class="xref docutils literal"
><span class="pre">True</span></tt>), then some chunks written |
| 147 to <em>fp</em> may be <tt class="xref docutils literal"><span class="pre">unicod
e</span></tt> instances, subject to normal Python |
| 148 <tt class="xref docutils literal"><span class="pre">str</span></tt> to <tt class
="xref docutils literal"><span class="pre">unicode</span></tt> coercion rules.
Unless <tt class="docutils literal"><span class="pre">fp.write()</span></tt> |
| 149 explicitly understands <tt class="xref docutils literal"><span class="pre">unico
de</span></tt> (as in <tt class="xref docutils literal"><span class="pre">codecs
.getwriter()</span></tt>) this |
| 150 is likely to cause an error. It’s best to leave the default settings, beca
use |
| 151 they are safe and it is highly optimized.</p> |
| 152 <p>If <em>check_circular</em> is false (default: <tt class="xref docutils litera
l"><span class="pre">True</span></tt>), then the circular |
| 153 reference check for container types will be skipped and a circular reference |
| 154 will result in an <tt class="xref docutils literal"><span class="pre">OverflowEr
ror</span></tt> (or worse).</p> |
| 155 <p>If <em>allow_nan</em> is false (default: <tt class="xref docutils literal"><s
pan class="pre">True</span></tt>), then it will be a |
| 156 <tt class="xref docutils literal"><span class="pre">ValueError</span></tt> to se
rialize out of range <tt class="xref docutils literal"><span class="pre">float</
span></tt> values (<tt class="docutils literal"><span class="pre">nan</span></tt
>, |
| 157 <tt class="docutils literal"><span class="pre">inf</span></tt>, <tt class="docut
ils literal"><span class="pre">-inf</span></tt>) in strict compliance of the JSO
N specification. |
| 158 If <em>allow_nan</em> is true, their JavaScript equivalents will be used |
| 159 (<tt class="docutils literal"><span class="pre">NaN</span></tt>, <tt class="docu
tils literal"><span class="pre">Infinity</span></tt>, <tt class="docutils litera
l"><span class="pre">-Infinity</span></tt>).</p> |
| 160 <p>If <em>indent</em> is a non-negative integer, then JSON array elements and ob
ject |
| 161 members will be pretty-printed with that indent level. An indent level of 0 |
| 162 will only insert newlines. <tt class="xref docutils literal"><span class="pre">
None</span></tt> (the default) selects the most compact |
| 163 representation.</p> |
| 164 <p>If specified, <em>separators</em> should be an <tt class="docutils literal"><
span class="pre">(item_separator,</span> <span class="pre">dict_separator)</span
></tt> |
| 165 tuple. By default, <tt class="docutils literal"><span class="pre">(',</span> <s
pan class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span
></tt> are used. To get the most compact JSON |
| 166 representation, you should specify <tt class="docutils literal"><span class="pre
">(',',</span> <span class="pre">':')</span></tt> to eliminate whitespace.</p> |
| 167 <p><em>encoding</em> is the character encoding for str instances, default is |
| 168 <tt class="docutils literal"><span class="pre">'utf-8'</span></tt>.</p> |
| 169 <p>If specified, <em>default</em> should be a function that gets called for obje
cts |
| 170 that can’t otherwise be serialized. It should return a JSON encodable |
| 171 version of the object or raise a <tt class="xref docutils literal"><span class="
pre">TypeError</span></tt>. If not specified, |
| 172 <tt class="xref docutils literal"><span class="pre">TypeError</span></tt> is alw
ays raised in those cases.</p> |
| 173 <p>To use a custom <a title="simplejson.JSONEncoder" class="reference internal"
href="#simplejson.JSONEncoder"><tt class="xref docutils literal"><span class="pr
e">JSONEncoder</span></tt></a> subclass (e.g. one that overrides the |
| 174 <tt class="xref docutils literal"><span class="pre">default()</span></tt> method
to serialize additional types), specify it with the |
| 175 <em>cls</em> kwarg.</p> |
| 176 <blockquote> |
| 177 <div class="admonition note"> |
| 178 <p class="first admonition-title">Note</p> |
| 179 <p class="last">JSON is not a framed protocol so unlike <tt class="xref docutils
literal"><span class="pre">pickle</span></tt> or <tt class="xref docutils liter
al"><span class="pre">marshal</span></tt> it |
| 180 does not make sense to serialize more than one JSON document without some |
| 181 container protocol to delimit them.</p> |
| 182 </div> |
| 183 </blockquote> |
| 184 </dd></dl> |
| 185 |
| 186 <dl class="function"> |
| 187 <dt id="simplejson.dumps"> |
| 188 <!--[simplejson.dumps]--><tt class="descclassname">simplejson.</tt><tt class="de
scname">dumps</tt><big>(</big><em>obj</em><span class="optional">[</span>, <em>s
kipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><span class="o
ptional">[</span>, <em>check_circular</em><span class="optional">[</span>, <em>a
llow_nan</em><span class="optional">[</span>, <em>cls</em><span class="optional"
>[</span>, <em>indent</em><span class="optional">[</span>, <em>separators</em><s
pan class="optional">[</span>, <em>encoding</em><span class="optional">[</span>,
<em>default</em><span class="optional">[</span>, <em>**kw</em><span class="opti
onal">]</span><span class="optional">]</span><span class="optional">]</span><spa
n class="optional">]</span><span class="optional">]</span><span class="optional"
>]</span><span class="optional">]</span><span class="optional">]</span><span cla
ss="optional">]</span><span class="optional">]</span><big>)</big><a class="heade
rlink" href="#simplejson.dumps" title="Permalink to this definition">¶</a></dt> |
| 189 <dd><p>Serialize <em>obj</em> to a JSON formatted <tt class="xref docutils liter
al"><span class="pre">str</span></tt>.</p> |
| 190 <p>If <em>ensure_ascii</em> is false, then the return value will be a |
| 191 <tt class="xref docutils literal"><span class="pre">unicode</span></tt> instance
. The other arguments have the same meaning as in |
| 192 <a title="simplejson.dump" class="reference internal" href="#simplejson.dump"><t
t class="xref docutils literal"><span class="pre">dump()</span></tt></a>. Note t
hat the default <em>ensure_ascii</em> setting has much |
| 193 better performance.</p> |
| 194 </dd></dl> |
| 195 |
| 196 <dl class="function"> |
| 197 <dt id="simplejson.load"> |
| 198 <!--[simplejson.load]--><tt class="descclassname">simplejson.</tt><tt class="des
cname">load</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>enco
ding</em><span class="optional">[</span>, <em>cls</em><span class="optional">[</
span>, <em>object_hook</em><span class="optional">[</span>, <em>parse_float</em>
<span class="optional">[</span>, <em>parse_int</em><span class="optional">[</spa
n>, <em>parse_constant</em><span class="optional">[</span>, <em>**kw</em><span c
lass="optional">]</span><span class="optional">]</span><span class="optional">]<
/span><span class="optional">]</span><span class="optional">]</span><span class=
"optional">]</span><span class="optional">]</span><big>)</big><a class="headerli
nk" href="#simplejson.load" title="Permalink to this definition">¶</a></dt> |
| 199 <dd><p>Deserialize <em>fp</em> (a <tt class="docutils literal"><span class="pre"
>.read()</span></tt>-supporting file-like object containing a JSON |
| 200 document) to a Python object.</p> |
| 201 <p>If the contents of <em>fp</em> are encoded with an ASCII based encoding other
than |
| 202 UTF-8 (e.g. latin-1), then an appropriate <em>encoding</em> name must be specifi
ed. |
| 203 Encodings that are not ASCII based (such as UCS-2) are not allowed, and |
| 204 should be wrapped with <tt class="docutils literal"><span class="pre">codecs.get
reader(fp)(encoding)</span></tt>, or simply decoded |
| 205 to a <tt class="xref docutils literal"><span class="pre">unicode</span></tt> obj
ect and passed to <a title="simplejson.loads" class="reference internal" href="#
simplejson.loads"><tt class="xref docutils literal"><span class="pre">loads()</s
pan></tt></a>. The default |
| 206 setting of <tt class="docutils literal"><span class="pre">'utf-8'</span></tt> is
fastest and should be using whenever possible.</p> |
| 207 <p><em>object_hook</em> is an optional function that will be called with the res
ult of |
| 208 any object literal decode (a <tt class="xref docutils literal"><span class="pre"
>dict</span></tt>). The return value of |
| 209 <em>object_hook</em> will be used instead of the <tt class="xref docutils litera
l"><span class="pre">dict</span></tt>. This feature can be used |
| 210 to implement custom decoders (e.g. JSON-RPC class hinting).</p> |
| 211 <p><em>parse_float</em>, if specified, will be called with the string of every J
SON |
| 212 float to be decoded. By default, this is equivalent to <tt class="docutils lite
ral"><span class="pre">float(num_str)</span></tt>. |
| 213 This can be used to use another datatype or parser for JSON floats |
| 214 (e.g. <tt class="xref docutils literal"><span class="pre">decimal.Decimal</span>
</tt>).</p> |
| 215 <p><em>parse_int</em>, if specified, will be called with the string of every JSO
N int |
| 216 to be decoded. By default, this is equivalent to <tt class="docutils literal"><
span class="pre">int(num_str)</span></tt>. This can |
| 217 be used to use another datatype or parser for JSON integers |
| 218 (e.g. <tt class="xref docutils literal"><span class="pre">float</span></tt>).</p
> |
| 219 <p><em>parse_constant</em>, if specified, will be called with one of the followi
ng |
| 220 strings: <tt class="docutils literal"><span class="pre">'-Infinity'</span></tt>,
<tt class="docutils literal"><span class="pre">'Infinity'</span></tt>, <tt clas
s="docutils literal"><span class="pre">'NaN'</span></tt>. This can be used to |
| 221 raise an exception if invalid JSON numbers are encountered.</p> |
| 222 <p>To use a custom <a title="simplejson.JSONDecoder" class="reference internal"
href="#simplejson.JSONDecoder"><tt class="xref docutils literal"><span class="pr
e">JSONDecoder</span></tt></a> subclass, specify it with the <tt class="docutils
literal"><span class="pre">cls</span></tt> |
| 223 kwarg. Additional keyword arguments will be passed to the constructor of the |
| 224 class.</p> |
| 225 <blockquote> |
| 226 <div class="admonition note"> |
| 227 <p class="first admonition-title">Note</p> |
| 228 <p class="last"><a title="simplejson.load" class="reference internal" href="#sim
plejson.load"><tt class="xref docutils literal"><span class="pre">load()</span><
/tt></a> will read the rest of the file-like object as a string and |
| 229 then call <a title="simplejson.loads" class="reference internal" href="#simplejs
on.loads"><tt class="xref docutils literal"><span class="pre">loads()</span></tt
></a>. It does not stop at the end of the first valid |
| 230 JSON document it finds and it will raise an error if there is anything |
| 231 other than whitespace after the document. Except for files containing |
| 232 only one JSON document, it is recommended to use <a title="simplejson.loads" cla
ss="reference internal" href="#simplejson.loads"><tt class="xref docutils litera
l"><span class="pre">loads()</span></tt></a>.</p> |
| 233 </div> |
| 234 </blockquote> |
| 235 </dd></dl> |
| 236 |
| 237 <dl class="function"> |
| 238 <dt id="simplejson.loads"> |
| 239 <!--[simplejson.loads]--><tt class="descclassname">simplejson.</tt><tt class="de
scname">loads</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>enc
oding</em><span class="optional">[</span>, <em>cls</em><span class="optional">[<
/span>, <em>object_hook</em><span class="optional">[</span>, <em>parse_float</em
><span class="optional">[</span>, <em>parse_int</em><span class="optional">[</sp
an>, <em>parse_constant</em><span class="optional">[</span>, <em>**kw</em><span
class="optional">]</span><span class="optional">]</span><span class="optional">]
</span><span class="optional">]</span><span class="optional">]</span><span class
="optional">]</span><span class="optional">]</span><big>)</big><a class="headerl
ink" href="#simplejson.loads" title="Permalink to this definition">¶</a></dt> |
| 240 <dd><p>Deserialize <em>s</em> (a <tt class="xref docutils literal"><span class="
pre">str</span></tt> or <tt class="xref docutils literal"><span class="pre">unic
ode</span></tt> instance containing a JSON |
| 241 document) to a Python object.</p> |
| 242 <p>If <em>s</em> is a <tt class="xref docutils literal"><span class="pre">str</s
pan></tt> instance and is encoded with an ASCII based encoding |
| 243 other than UTF-8 (e.g. latin-1), then an appropriate <em>encoding</em> name must
be |
| 244 specified. Encodings that are not ASCII based (such as UCS-2) are not |
| 245 allowed and should be decoded to <tt class="xref docutils literal"><span class="
pre">unicode</span></tt> first.</p> |
| 246 <p>The other arguments have the same meaning as in <a title="simplejson.load" cl
ass="reference internal" href="#simplejson.load"><tt class="xref docutils litera
l"><span class="pre">load()</span></tt></a>.</p> |
| 247 </dd></dl> |
| 248 |
| 249 </div> |
| 250 <div class="section" id="encoders-and-decoders"> |
| 251 <h2 id="encoders-and-decoders">Encoders and decoders<a class="headerlink" href="
#encoders-and-decoders" title="Permalink to this headline">¶</a></h2> |
| 252 <dl class="class"> |
| 253 <dt id="simplejson.JSONDecoder"> |
| 254 <!--[simplejson.JSONDecoder]-->class <tt class="descclassname">simplejson.</tt><
tt class="descname">JSONDecoder</tt><big>(</big><span class="optional">[</span><
em>encoding</em><span class="optional">[</span>, <em>object_hook</em><span class
="optional">[</span>, <em>parse_float</em><span class="optional">[</span>, <em>p
arse_int</em><span class="optional">[</span>, <em>parse_constant</em><span class
="optional">[</span>, <em>strict</em><span class="optional">]</span><span class=
"optional">]</span><span class="optional">]</span><span class="optional">]</span
><span class="optional">]</span><span class="optional">]</span><big>)</big><a cl
ass="headerlink" href="#simplejson.JSONDecoder" title="Permalink to this definit
ion">¶</a></dt> |
| 255 <dd><p>Simple JSON decoder.</p> |
| 256 <p>Performs the following translations in decoding by default:</p> |
| 257 <table border="1" class="docutils"> |
| 258 <colgroup> |
| 259 <col width="44%" /> |
| 260 <col width="56%" /> |
| 261 </colgroup> |
| 262 <thead valign="bottom"> |
| 263 <tr><th class="head">JSON</th> |
| 264 <th class="head">Python</th> |
| 265 </tr> |
| 266 </thead> |
| 267 <tbody valign="top"> |
| 268 <tr><td>object</td> |
| 269 <td>dict</td> |
| 270 </tr> |
| 271 <tr><td>array</td> |
| 272 <td>list</td> |
| 273 </tr> |
| 274 <tr><td>string</td> |
| 275 <td>unicode</td> |
| 276 </tr> |
| 277 <tr><td>number (int)</td> |
| 278 <td>int, long</td> |
| 279 </tr> |
| 280 <tr><td>number (real)</td> |
| 281 <td>float</td> |
| 282 </tr> |
| 283 <tr><td>true</td> |
| 284 <td>True</td> |
| 285 </tr> |
| 286 <tr><td>false</td> |
| 287 <td>False</td> |
| 288 </tr> |
| 289 <tr><td>null</td> |
| 290 <td>None</td> |
| 291 </tr> |
| 292 </tbody> |
| 293 </table> |
| 294 <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 |
| 295 corresponding <tt class="docutils literal"><span class="pre">float</span></tt> v
alues, which is outside the JSON spec.</p> |
| 296 <p><em>encoding</em> determines the encoding used to interpret any <tt class="xr
ef docutils literal"><span class="pre">str</span></tt> objects |
| 297 decoded by this instance (<tt class="docutils literal"><span class="pre">'utf-8'
</span></tt> by default). It has no effect when decoding |
| 298 <tt class="xref docutils literal"><span class="pre">unicode</span></tt> objects.
</p> |
| 299 <p>Note that currently only encodings that are a superset of ASCII work, strings |
| 300 of other encodings should be passed in as <tt class="xref docutils literal"><spa
n class="pre">unicode</span></tt>.</p> |
| 301 <p><em>object_hook</em>, if specified, will be called with the result of every J
SON |
| 302 object decoded and its return value will be used in place of the given |
| 303 <tt class="xref docutils literal"><span class="pre">dict</span></tt>. This can
be used to provide custom deserializations (e.g. to |
| 304 support JSON-RPC class hinting).</p> |
| 305 <p><em>parse_float</em>, if specified, will be called with the string of every J
SON |
| 306 float to be decoded. By default, this is equivalent to <tt class="docutils lite
ral"><span class="pre">float(num_str)</span></tt>. |
| 307 This can be used to use another datatype or parser for JSON floats |
| 308 (e.g. <tt class="xref docutils literal"><span class="pre">decimal.Decimal</span>
</tt>).</p> |
| 309 <p><em>parse_int</em>, if specified, will be called with the string of every JSO
N int |
| 310 to be decoded. By default, this is equivalent to <tt class="docutils literal"><
span class="pre">int(num_str)</span></tt>. This can |
| 311 be used to use another datatype or parser for JSON integers |
| 312 (e.g. <tt class="xref docutils literal"><span class="pre">float</span></tt>).</p
> |
| 313 <p><em>parse_constant</em>, if specified, will be called with one of the followi
ng |
| 314 strings: <tt class="docutils literal"><span class="pre">'-Infinity'</span></tt>,
<tt class="docutils literal"><span class="pre">'Infinity'</span></tt>, <tt clas
s="docutils literal"><span class="pre">'NaN'</span></tt>. This can be used to |
| 315 raise an exception if invalid JSON numbers are encountered.</p> |
| 316 <p><em>strict</em> controls the parser’s behavior when it encounters an in
valid |
| 317 control character in a string. The default setting of <tt class="xref docutils l
iteral"><span class="pre">True</span></tt> means that |
| 318 unescaped control characters are parse errors, if <tt class="xref docutils liter
al"><span class="pre">False</span></tt> then control |
| 319 characters will be allowed in strings.</p> |
| 320 <dl class="method"> |
| 321 <dt id="simplejson.JSONDecoder.decode"> |
| 322 <!--[simplejson.JSONDecoder.decode]--><tt class="descname">decode</tt><big>(</bi
g><em>s</em><big>)</big><a class="headerlink" href="#simplejson.JSONDecoder.deco
de" title="Permalink to this definition">¶</a></dt> |
| 323 <dd>Return the Python representation of <em>s</em> (a <tt class="xref docutils l
iteral"><span class="pre">str</span></tt> or |
| 324 <tt class="xref docutils literal"><span class="pre">unicode</span></tt> instance
containing a JSON document)</dd></dl> |
| 325 |
| 326 <dl class="method"> |
| 327 <dt id="simplejson.JSONDecoder.raw_decode"> |
| 328 <!--[simplejson.JSONDecoder.raw_decode]--><tt class="descname">raw_decode</tt><b
ig>(</big><em>s</em><big>)</big><a class="headerlink" href="#simplejson.JSONDeco
der.raw_decode" title="Permalink to this definition">¶</a></dt> |
| 329 <dd><p>Decode a JSON document from <em>s</em> (a <tt class="xref docutils litera
l"><span class="pre">str</span></tt> or <tt class="xref docutils literal"><span
class="pre">unicode</span></tt> |
| 330 beginning with a JSON document) and return a 2-tuple of the Python |
| 331 representation and the index in <em>s</em> where the document ended.</p> |
| 332 <p>This can be used to decode a JSON document from a string that may have |
| 333 extraneous data at the end.</p> |
| 334 </dd></dl> |
| 335 |
| 336 </dd></dl> |
| 337 |
| 338 <dl class="class"> |
| 339 <dt id="simplejson.JSONEncoder"> |
| 340 <!--[simplejson.JSONEncoder]-->class <tt class="descclassname">simplejson.</tt><
tt class="descname">JSONEncoder</tt><big>(</big><span class="optional">[</span><
em>skipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><span clas
s="optional">[</span>, <em>check_circular</em><span class="optional">[</span>, <
em>allow_nan</em><span class="optional">[</span>, <em>sort_keys</em><span class=
"optional">[</span>, <em>indent</em><span class="optional">[</span>, <em>separat
ors</em><span class="optional">[</span>, <em>encoding</em><span class="optional"
>[</span>, <em>default</em><span class="optional">]</span><span class="optional"
>]</span><span class="optional">]</span><span class="optional">]</span><span cla
ss="optional">]</span><span class="optional">]</span><span class="optional">]</s
pan><span class="optional">]</span><span class="optional">]</span><big>)</big><a
class="headerlink" href="#simplejson.JSONEncoder" title="Permalink to this defi
nition">¶</a></dt> |
| 341 <dd><p>Extensible JSON encoder for Python data structures.</p> |
| 342 <p>Supports the following objects and types by default:</p> |
| 343 <table border="1" class="docutils"> |
| 344 <colgroup> |
| 345 <col width="56%" /> |
| 346 <col width="44%" /> |
| 347 </colgroup> |
| 348 <thead valign="bottom"> |
| 349 <tr><th class="head">Python</th> |
| 350 <th class="head">JSON</th> |
| 351 </tr> |
| 352 </thead> |
| 353 <tbody valign="top"> |
| 354 <tr><td>dict</td> |
| 355 <td>object</td> |
| 356 </tr> |
| 357 <tr><td>list, tuple</td> |
| 358 <td>array</td> |
| 359 </tr> |
| 360 <tr><td>str, unicode</td> |
| 361 <td>string</td> |
| 362 </tr> |
| 363 <tr><td>int, long, float</td> |
| 364 <td>number</td> |
| 365 </tr> |
| 366 <tr><td>True</td> |
| 367 <td>true</td> |
| 368 </tr> |
| 369 <tr><td>False</td> |
| 370 <td>false</td> |
| 371 </tr> |
| 372 <tr><td>None</td> |
| 373 <td>null</td> |
| 374 </tr> |
| 375 </tbody> |
| 376 </table> |
| 377 <p>To extend this to recognize other objects, subclass and implement a |
| 378 <a title="simplejson.JSONEncoder.default" class="reference internal" href="#simp
lejson.JSONEncoder.default"><tt class="xref docutils literal"><span class="pre">
default()</span></tt></a> method with another method that returns a serializable
object |
| 379 for <tt class="docutils literal"><span class="pre">o</span></tt> if possible, ot
herwise it should call the superclass implementation |
| 380 (to raise <tt class="xref docutils literal"><span class="pre">TypeError</span></
tt>).</p> |
| 381 <p>If <em>skipkeys</em> is false (the default), then it is a <tt class="xref doc
utils literal"><span class="pre">TypeError</span></tt> to |
| 382 attempt encoding of keys that are not str, int, long, float or None. If |
| 383 <em>skipkeys</em> is true, such items are simply skipped.</p> |
| 384 <p>If <em>ensure_ascii</em> is true (the default), the output is guaranteed to b
e |
| 385 <tt class="xref docutils literal"><span class="pre">str</span></tt> objects with
all incoming unicode characters escaped. If |
| 386 <em>ensure_ascii</em> is false, the output will be a unicode object.</p> |
| 387 <p>If <em>check_circular</em> is false (the default), then lists, dicts, and cus
tom |
| 388 encoded objects will be checked for circular references during encoding to |
| 389 prevent an infinite recursion (which would cause an <tt class="xref docutils lit
eral"><span class="pre">OverflowError</span></tt>). |
| 390 Otherwise, no such check takes place.</p> |
| 391 <p>If <em>allow_nan</em> is true (the default), then <tt class="docutils literal
"><span class="pre">NaN</span></tt>, <tt class="docutils literal"><span class="p
re">Infinity</span></tt>, and |
| 392 <tt class="docutils literal"><span class="pre">-Infinity</span></tt> will be enc
oded as such. This behavior is not JSON |
| 393 specification compliant, but is consistent with most JavaScript based |
| 394 encoders and decoders. Otherwise, it will be a <tt class="xref docutils literal
"><span class="pre">ValueError</span></tt> to encode |
| 395 such floats.</p> |
| 396 <p>If <em>sort_keys</em> is true (the default), then the output of dictionaries |
| 397 will be sorted by key; this is useful for regression tests to ensure that |
| 398 JSON serializations can be compared on a day-to-day basis.</p> |
| 399 <p>If <em>indent</em> is a non-negative integer (it is <tt class="xref docutils
literal"><span class="pre">None</span></tt> by default), then JSON |
| 400 array elements and object members will be pretty-printed with that indent |
| 401 level. An indent level of 0 will only insert newlines. <tt class="xref docutil
s literal"><span class="pre">None</span></tt> is the most |
| 402 compact representation.</p> |
| 403 <p>If specified, <em>separators</em> should be an <tt class="docutils literal"><
span class="pre">(item_separator,</span> <span class="pre">key_separator)</span>
</tt> |
| 404 tuple. By default, <tt class="docutils literal"><span class="pre">(',</span> <s
pan class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span
></tt> are used. To get the most compact JSON |
| 405 representation, you should specify <tt class="docutils literal"><span class="pre
">(',',</span> <span class="pre">':')</span></tt> to eliminate whitespace.</p> |
| 406 <p>If specified, <em>default</em> should be a function that gets called for obje
cts |
| 407 that can’t otherwise be serialized. It should return a JSON encodable |
| 408 version of the object or raise a <tt class="xref docutils literal"><span class="
pre">TypeError</span></tt>.</p> |
| 409 <p>If <em>encoding</em> is not <tt class="xref docutils literal"><span class="pr
e">None</span></tt>, then all input strings will be transformed |
| 410 into unicode using that encoding prior to JSON-encoding. The default is |
| 411 <tt class="docutils literal"><span class="pre">'utf-8'</span></tt>.</p> |
| 412 <dl class="method"> |
| 413 <dt id="simplejson.JSONEncoder.default"> |
| 414 <!--[simplejson.JSONEncoder.default]--><tt class="descname">default</tt><big>(</
big><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder.de
fault" title="Permalink to this definition">¶</a></dt> |
| 415 <dd><p>Implement this method in a subclass such that it returns a serializable |
| 416 object for <em>o</em>, or calls the base implementation (to raise a |
| 417 <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>).</p> |
| 418 <p>For example, to support arbitrary iterators, you could implement default |
| 419 like this:</p> |
| 420 <div class="highlight"><pre><span class="k">def</span> <span class="nf">default<
/span><span class="p">(</span><span class="bp">self</span><span class="p">,</spa
n> <span class="n">o</span><span class="p">):</span> |
| 421 <span class="k">try</span><span class="p">:</span> |
| 422 <span class="n">iterable</span> <span class="o">=</span> <span class="nb"
>iter</span><span class="p">(</span><span class="n">o</span><span class="p">)</s
pan> |
| 423 <span class="k">except</span> <span class="ne">TypeError</span><span class="p
">:</span> |
| 424 <span class="k">pass</span> |
| 425 <span class="k">else</span><span class="p">:</span> |
| 426 <span class="k">return</span> <span class="nb">list</span><span class="p"
>(</span><span class="n">iterable</span><span class="p">)</span> |
| 427 <span class="k">return</span> <span class="n">JSONEncoder</span><span class="
o">.</span><span class="n">default</span><span class="p">(</span><span class="bp
">self</span><span class="p">,</span> <span class="n">o</span><span class="p">)<
/span> |
| 428 </pre></div> |
| 429 </dd></dl> |
| 430 |
| 431 <dl class="method"> |
| 432 <dt id="simplejson.JSONEncoder.encode"> |
| 433 <!--[simplejson.JSONEncoder.encode]--><tt class="descname">encode</tt><big>(</bi
g><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder.enco
de" title="Permalink to this definition">¶</a></dt> |
| 434 <dd><p>Return a JSON string representation of a Python data structure, <em>o</em
>. For |
| 435 example:</p> |
| 436 <div class="highlight"><pre><span class="gp">>>> </span><span class="k"
>import</span> <span class="nn">simplejson</span> <span class="k">as</span> <spa
n class="nn">json</span> |
| 437 <span class="gp">>>> </span><span class="n">json</span><span class="o">
.</span><span class="n">JSONEncoder</span><span class="p">()</span><span class="
o">.</span><span class="n">encode</span><span class="p">({</span><span class="s"
>"foo"</span><span class="p">:</span> <span class="p">[</span><span cl
ass="s">"bar"</span><span class="p">,</span> <span class="s">"baz
"</span><span class="p">]})</span> |
| 438 <span class="go">'{"foo": ["bar", "baz"]}'
</span> |
| 439 </pre></div> |
| 440 </dd></dl> |
| 441 |
| 442 <dl class="method"> |
| 443 <dt id="simplejson.JSONEncoder.iterencode"> |
| 444 <!--[simplejson.JSONEncoder.iterencode]--><tt class="descname">iterencode</tt><b
ig>(</big><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEnco
der.iterencode" title="Permalink to this definition">¶</a></dt> |
| 445 <dd><p>Encode the given object, <em>o</em>, and yield each string representation
as |
| 446 available. For example:</p> |
| 447 <div class="highlight"><pre><span class="k">for</span> <span class="n">chunk</sp
an> <span class="ow">in</span> <span class="n">JSONEncoder</span><span class="p"
>()</span><span class="o">.</span><span class="n">iterencode</span><span class="
p">(</span><span class="n">bigobject</span><span class="p">):</span> |
| 448 <span class="n">mysocket</span><span class="o">.</span><span class="n">write
</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</sp
an> |
| 449 </pre></div> |
| 450 <p>Note that <a title="simplejson.JSONEncoder.encode" class="reference internal"
href="#simplejson.JSONEncoder.encode"><tt class="xref docutils literal"><span c
lass="pre">encode()</span></tt></a> has much better performance than |
| 451 <a title="simplejson.JSONEncoder.iterencode" class="reference internal" href="#s
implejson.JSONEncoder.iterencode"><tt class="xref docutils literal"><span class=
"pre">iterencode()</span></tt></a>.</p> |
| 452 </dd></dl> |
| 453 |
| 454 </dd></dl> |
| 455 |
| 456 </div> |
| 457 </div> |
| 458 |
| 459 |
| 460 </div> |
| 461 </div> |
| 462 </div> |
| 463 <div class="sphinxsidebar"> |
| 464 <div class="sphinxsidebarwrapper"> |
| 465 <h3>Table Of Contents</h3> |
| 466 <ul> |
| 467 <li><a class="reference external" href=""><tt class="docutils literal"><span cla
ss="pre">simplejson</span></tt> — JSON encoder and decoder</a><ul> |
| 468 <li><a class="reference external" href="#basic-usage">Basic Usage</a></li> |
| 469 <li><a class="reference external" href="#encoders-and-decoders">Encoders and dec
oders</a></li> |
| 470 </ul> |
| 471 </li> |
| 472 </ul> |
| 473 |
| 474 <h3>This Page</h3> |
| 475 <ul class="this-page-menu"> |
| 476 <li><a href="_sources/index.txt">Show Source</a></li> |
| 477 </ul> |
| 478 <h3>Quick search</h3> |
| 479 <form class="search" action="search.html" method="get"> |
| 480 <input type="text" name="q" size="18" /> <input type="submit" valu
e="Go" /> |
| 481 <input type="hidden" name="check_keywords" value="yes" /> |
| 482 <input type="hidden" name="area" value="default" /> |
| 483 </form> |
| 484 </div> |
| 485 </div> |
| 486 <div class="clearer"></div> |
| 487 </div> |
| 488 <div class="related"> |
| 489 <h3>Navigation</h3> |
| 490 <ul> |
| 491 <li class="right" style="margin-right: 10px"> |
| 492 <a href="genindex.html" title="General Index" |
| 493 accesskey="I">index</a></li> |
| 494 <li><a href="">simplejson v2.0.0 documentation</a> »</li> |
| 495 </ul> |
| 496 </div> |
| 497 <div class="footer"> |
| 498 © Copyright 2008, Bob Ippolito. |
| 499 Last updated on Feb 18, 2009. |
| 500 Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>. |
| 501 </div> |
| 502 </body> |
| 503 </html> |
OLD | NEW |