~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/python/simplejson-2.1.1/docs/index.html

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 &#8212; JSON encoder and decoder &mdash; simplejson v2.1.1 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.1.1',
 
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.1.1 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.1.1 documentation</a> &raquo;</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">simplejson</span></tt> &#8212; JSON encoder and decoder<a class="headerlink" href="#module-simplejson" title="Permalink to this headline">¶</a></h1>
 
42
<p>JSON (JavaScript Object Notation) &lt;<a class="reference external" href="http://json.org">http://json.org</a>&gt; 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> exposes 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 is 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.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">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
52
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">([</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">&#39;bar&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;baz&#39;</span><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><span class="p">)}])</span>
 
53
<span class="go">&#39;[&quot;foo&quot;, {&quot;bar&quot;: [&quot;baz&quot;, null, 1.0, 2]}]&#39;</span>
 
54
<span class="gp">&gt;&gt;&gt; </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">&quot;</span><span class="se">\&quot;</span><span class="s">foo</span><span class="se">\b</span><span class="s">ar&quot;</span><span class="p">)</span>
 
55
<span class="go">&quot;\&quot;foo\bar&quot;</span>
 
56
<span class="gp">&gt;&gt;&gt; </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&#39;</span><span class="se">\u1234</span><span class="s">&#39;</span><span class="p">)</span>
 
57
<span class="go">&quot;\u1234&quot;</span>
 
58
<span class="gp">&gt;&gt;&gt; </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">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">)</span>
 
59
<span class="go">&quot;\\&quot;</span>
 
60
<span class="gp">&gt;&gt;&gt; </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">&quot;c&quot;</span><span class="p">:</span> <span class="mf">0</span><span class="p">,</span> <span class="s">&quot;b&quot;</span><span class="p">:</span> <span class="mf">0</span><span class="p">,</span> <span class="s">&quot;a&quot;</span><span class="p">:</span> <span class="mf">0</span><span 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">{&quot;a&quot;: 0, &quot;b&quot;: 0, &quot;c&quot;: 0}</span>
 
62
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">io</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
 
64
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">([</span><span class="s">&#39;streaming API&#39;</span><span class="p">],</span> <span class="n">io</span><span class="p">)</span>
 
65
<span class="gp">&gt;&gt;&gt; </span><span class="n">io</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
 
66
<span class="go">&#39;[&quot;streaming API&quot;]&#39;</span>
 
67
</pre></div>
 
68
<p>Compact encoding:</p>
 
69
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
70
<span class="gp">&gt;&gt;&gt; </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">&#39;4&#39;</span><span class="p">:</span> <span class="mf">5</span><span class="p">,</span> <span class="s">&#39;6&#39;</span><span class="p">:</span> <span class="mf">7</span><span class="p">}],</span> <span class="n">separators</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;,&#39;</span><span class="p">,</span><span class="s">&#39;:&#39;</span><span class="p">))</span>
 
71
<span class="go">&#39;[1,2,3,{&quot;4&quot;:5,&quot;6&quot;:7}]&#39;</span>
 
72
</pre></div>
 
73
<p>Pretty printing:</p>
 
74
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
75
<span class="gp">&gt;&gt;&gt; </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">&#39;4&#39;</span><span class="p">:</span> <span class="mf">5</span><span class="p">,</span> <span class="s">&#39;6&#39;</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="bp">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mf">4</span> <span class="o">*</span> <span class="s">&#39; &#39;</span><span class="p">)</span>
 
76
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</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">splitlines</span><span class="p">()])</span>
 
77
<span class="go">{</span>
 
78
<span class="go">    &quot;4&quot;: 5,</span>
 
79
<span class="go">    &quot;6&quot;: 7</span>
 
80
<span class="go">}</span>
 
81
</pre></div>
 
82
<p>Decoding JSON:</p>
 
83
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
84
<span class="gp">&gt;&gt;&gt; </span><span class="n">obj</span> <span class="o">=</span> <span class="p">[</span><span class="s">u&#39;foo&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s">u&#39;bar&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">u&#39;baz&#39;</span><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><span class="p">]}]</span>
 
85
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s">&#39;[&quot;foo&quot;, {&quot;bar&quot;:[&quot;baz&quot;, null, 1.0, 2]}]&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="n">obj</span>
 
86
<span class="go">True</span>
 
87
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s">&#39;&quot;</span><span class="se">\\</span><span class="s">&quot;foo</span><span class="se">\\</span><span class="s">bar&quot;&#39;</span><span class="p">)</span> <span class="o">==</span> <span class="s">u&#39;&quot;foo</span><span class="se">\x08</span><span class="s">ar&#39;</span>
 
88
<span class="go">True</span>
 
89
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">io</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s">&#39;[&quot;streaming API&quot;]&#39;</span><span class="p">)</span>
 
91
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">io</span><span class="p">)[</span><span class="mf">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;streaming API&#39;</span>
 
92
<span class="go">True</span>
 
93
</pre></div>
 
94
<p>Using Decimal instead of float:</p>
 
95
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
96
<span class="gp">&gt;&gt;&gt; </span><span class="k">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="n">Decimal</span>
 
97
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s">&#39;1.1&#39;</span><span class="p">,</span> <span class="n">use_decimal</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="o">==</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1.1&#39;</span><span class="p">)</span>
 
98
<span class="go">True</span>
 
99
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1.1&#39;</span><span class="p">),</span> <span class="n">use_decimal</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="o">==</span> <span class="s">&#39;1.1&#39;</span>
 
100
<span class="go">True</span>
 
101
</pre></div>
 
102
<p>Specializing JSON object decoding:</p>
 
103
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
104
<span class="gp">&gt;&gt;&gt; </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>
 
105
<span class="gp">... </span>    <span class="k">if</span> <span class="s">&#39;__complex__&#39;</span> <span class="ow">in</span> <span class="n">dct</span><span class="p">:</span>
 
106
<span class="gp">... </span>        <span class="k">return</span> <span class="nb">complex</span><span class="p">(</span><span class="n">dct</span><span class="p">[</span><span class="s">&#39;real&#39;</span><span class="p">],</span> <span class="n">dct</span><span class="p">[</span><span class="s">&#39;imag&#39;</span><span class="p">])</span>
 
107
<span class="gp">... </span>    <span class="k">return</span> <span class="n">dct</span>
 
108
<span class="gp">...</span>
 
109
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s">&#39;{&quot;__complex__&quot;: true, &quot;real&quot;: 1, &quot;imag&quot;: 2}&#39;</span><span class="p">,</span>
 
110
<span class="gp">... </span>    <span class="n">object_hook</span><span class="o">=</span><span class="n">as_complex</span><span class="p">)</span>
 
111
<span class="go">(1+2j)</span>
 
112
<span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">decimal</span>
 
113
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s">&#39;1.1&#39;</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 class="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><span class="p">(</span><span class="s">&#39;1.1&#39;</span><span class="p">)</span>
 
114
<span class="go">True</span>
 
115
</pre></div>
 
116
<p>Specializing JSON object encoding:</p>
 
117
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
118
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">encode_complex</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
 
119
<span class="gp">... </span>    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">complex</span><span class="p">):</span>
 
120
<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">.</span><span class="n">imag</span><span class="p">]</span>
 
121
<span class="gp">... </span>    <span class="k">raise</span> <span class="ne">TypeError</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">+</span> <span class="s">&quot; is not JSON serializable&quot;</span><span class="p">)</span>
 
122
<span class="gp">...</span>
 
123
<span class="gp">&gt;&gt;&gt; </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>
 
124
<span class="go">&#39;[2.0, 1.0]&#39;</span>
 
125
<span class="gp">&gt;&gt;&gt; </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><span 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>
 
126
<span class="go">&#39;[2.0, 1.0]&#39;</span>
 
127
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;&#39;</span><span class="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 class="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 class="n">iterencode</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>
 
128
<span class="go">&#39;[2.0, 1.0]&#39;</span>
 
129
</pre></div>
 
130
<p>Using <tt class="xref docutils literal"><span class="pre">simplejson.tool</span></tt> from the shell to validate and pretty-print:</p>
 
131
<div class="highlight"><pre>$ echo &#39;{&quot;json&quot;:&quot;obj&quot;}&#39; | python -m simplejson.tool
 
132
{
 
133
    &quot;json&quot;: &quot;obj&quot;
 
134
}
 
135
$ echo &#39;{ 1.2:3.4}&#39; | python -m simplejson.tool
 
136
Expecting property name: line 1 column 2 (char 2)
 
137
</pre></div>
 
138
<div class="admonition note">
 
139
<p class="first admonition-title">Note</p>
 
140
<p class="last">The JSON produced by this module&#8217;s default settings is a subset of
 
141
YAML, so it may be used as a serializer for that as well.</p>
 
142
</div>
 
143
<div class="section" id="basic-usage">
 
144
<h2 id="basic-usage">Basic Usage<a class="headerlink" href="#basic-usage" title="Permalink to this headline">¶</a></h2>
 
145
<dl class="function">
 
146
<dt id="simplejson.dump">
 
147
<!--[simplejson.dump]--><tt class="descclassname">simplejson.</tt><tt class="descname">dump</tt><big>(</big><em>obj</em>, <em>fp</em><span class="optional">[</span>, <em>skipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><span class="optional">[</span>, <em>check_circular</em><span class="optional">[</span>, <em>allow_nan</em><span class="optional">[</span>, <em>cls</em><span class="optional">[</span>, <em>indent</em><span class="optional">[</span>, <em>separators</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>default</em><span class="optional">[</span>, <em>use_decimal</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">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.dump" title="Permalink to this definition">¶</a></dt>
 
148
<dd><p>Serialize <em>obj</em> as a JSON formatted stream to <em>fp</em> (a <tt class="docutils literal"><span class="pre">.write()</span></tt>-supporting
 
149
file-like object).</p>
 
150
<p>If <em>skipkeys</em> is true (default: <tt class="xref docutils literal"><span class="pre">False</span></tt>), then dict keys that are not
 
151
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="xref docutils literal"><span class="pre">long</span></tt>,
 
152
<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 docutils literal"><span class="pre">None</span></tt>) will be skipped instead of raising a
 
153
<tt class="xref docutils literal"><span class="pre">TypeError</span></tt>.</p>
 
154
<p>If <em>ensure_ascii</em> is false (default: <tt class="xref docutils literal"><span class="pre">True</span></tt>), then some chunks written
 
155
to <em>fp</em> may be <tt class="xref docutils literal"><span class="pre">unicode</span></tt> instances, subject to normal Python
 
156
<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>
 
157
explicitly understands <tt class="xref docutils literal"><span class="pre">unicode</span></tt> (as in <tt class="xref docutils literal"><span class="pre">codecs.getwriter()</span></tt>) this
 
158
is likely to cause an error. It&#8217;s best to leave the default settings, because
 
159
they are safe and it is highly optimized.</p>
 
160
<p>If <em>check_circular</em> is false (default: <tt class="xref docutils literal"><span class="pre">True</span></tt>), then the circular
 
161
reference check for container types will be skipped and a circular reference
 
162
will result in an <tt class="xref docutils literal"><span class="pre">OverflowError</span></tt> (or worse).</p>
 
163
<p>If <em>allow_nan</em> is false (default: <tt class="xref docutils literal"><span class="pre">True</span></tt>), then it will be a
 
164
<tt class="xref docutils literal"><span class="pre">ValueError</span></tt> to serialize 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>,
 
165
<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.
 
166
If <em>allow_nan</em> is true, their JavaScript equivalents will be used
 
167
(<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>
 
168
<p>If <em>indent</em> is a string, then JSON array elements and object members
 
169
will be pretty-printed with a newline followed by that string repeated
 
170
for each level of nesting. <tt class="xref docutils literal"><span class="pre">None</span></tt> (the default) selects the most compact
 
171
representation without any newlines. For backwards compatibility with
 
172
versions of simplejson earlier than 2.1.0, an integer is also accepted
 
173
and is converted to a string with that many spaces.</p>
 
174
<p>
 
175
<span class="versionmodified">Changed in version 2.1.0: </span>Changed <em>indent</em> from an integer number of spaces to a string.</p>
 
176
<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>
 
177
tuple.  By default, <tt class="docutils literal"><span class="pre">(',</span> <span class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span></tt> are used.  To get the most compact JSON
 
178
representation, you should specify <tt class="docutils literal"><span class="pre">(',',</span> <span class="pre">':')</span></tt> to eliminate whitespace.</p>
 
179
<p><em>encoding</em> is the character encoding for str instances, default is
 
180
<tt class="docutils literal"><span class="pre">'utf-8'</span></tt>.</p>
 
181
<p><em>default(obj)</em> is a function that should return a serializable version of
 
182
<em>obj</em> or raise <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>.  The default simply raises <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>.</p>
 
183
<p>To use a custom <a title="simplejson.JSONEncoder" class="reference internal" href="#simplejson.JSONEncoder"><tt class="xref docutils literal"><span class="pre">JSONEncoder</span></tt></a> subclass (e.g. one that overrides the
 
184
<tt class="xref docutils literal"><span class="pre">default()</span></tt> method to serialize additional types), specify it with the
 
185
<em>cls</em> kwarg.</p>
 
186
<p>If <em>use_decimal</em> is true (default: <tt class="xref docutils literal"><span class="pre">False</span></tt>) then <tt class="xref docutils literal"><span class="pre">decimal.Decimal</span></tt>
 
187
will be natively serialized to JSON with full precision.</p>
 
188
<p>
 
189
<span class="versionmodified">Changed in version 2.1.0: </span><em>use_decimal</em> is new in 2.1.0.<div class="admonition note">
 
190
<p class="first admonition-title">Note</p>
 
191
<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 literal"><span class="pre">marshal</span></tt> it
 
192
does not make sense to serialize more than one JSON document without some
 
193
container protocol to delimit them.</p>
 
194
</div>
 
195
</p>
 
196
</dd></dl>
 
197
 
 
198
<dl class="function">
 
199
<dt id="simplejson.dumps">
 
200
<!--[simplejson.dumps]--><tt class="descclassname">simplejson.</tt><tt class="descname">dumps</tt><big>(</big><em>obj</em><span class="optional">[</span>, <em>skipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><span class="optional">[</span>, <em>check_circular</em><span class="optional">[</span>, <em>allow_nan</em><span class="optional">[</span>, <em>cls</em><span class="optional">[</span>, <em>indent</em><span class="optional">[</span>, <em>separators</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>default</em><span class="optional">[</span>, <em>use_decimal</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">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.dumps" title="Permalink to this definition">¶</a></dt>
 
201
<dd><p>Serialize <em>obj</em> to a JSON formatted <tt class="xref docutils literal"><span class="pre">str</span></tt>.</p>
 
202
<p>If <em>ensure_ascii</em> is false, then the return value will be a
 
203
<tt class="xref docutils literal"><span class="pre">unicode</span></tt> instance.  The other arguments have the same meaning as in
 
204
<a title="simplejson.dump" class="reference internal" href="#simplejson.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a>. Note that the default <em>ensure_ascii</em> setting has much
 
205
better performance.</p>
 
206
</dd></dl>
 
207
 
 
208
<dl class="function">
 
209
<dt id="simplejson.load">
 
210
<!--[simplejson.load]--><tt class="descclassname">simplejson.</tt><tt class="descname">load</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>encoding</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">[</span>, <em>parse_constant</em><span class="optional">[</span>, <em>object_pairs_hook</em><span class="optional">[</span>, <em>use_decimal</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">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.load" title="Permalink to this definition">¶</a></dt>
 
211
<dd><p>Deserialize <em>fp</em> (a <tt class="docutils literal"><span class="pre">.read()</span></tt>-supporting file-like object containing a JSON
 
212
document) to a Python object.</p>
 
213
<p>If the contents of <em>fp</em> are encoded with an ASCII based encoding other than
 
214
UTF-8 (e.g. latin-1), then an appropriate <em>encoding</em> name must be specified.
 
215
Encodings that are not ASCII based (such as UCS-2) are not allowed, and
 
216
should be wrapped with <tt class="docutils literal"><span class="pre">codecs.getreader(fp)(encoding)</span></tt>, or simply decoded
 
217
to a <tt class="xref docutils literal"><span class="pre">unicode</span></tt> object and passed to <a title="simplejson.loads" class="reference internal" href="#simplejson.loads"><tt class="xref docutils literal"><span class="pre">loads()</span></tt></a>. The default
 
218
setting of <tt class="docutils literal"><span class="pre">'utf-8'</span></tt> is fastest and should be using whenever possible.</p>
 
219
<p>If <em>fp.read()</em> returns <tt class="xref docutils literal"><span class="pre">str</span></tt> then decoded JSON strings that contain
 
220
only ASCII characters may be parsed as <tt class="xref docutils literal"><span class="pre">str</span></tt> for performance and
 
221
memory reasons. If your code expects only <tt class="xref docutils literal"><span class="pre">unicode</span></tt> the appropriate
 
222
solution is to wrap fp with a reader as demonstrated above.</p>
 
223
<p><em>object_hook</em> is an optional function that will be called with the result of
 
224
any object literal decode (a <tt class="xref docutils literal"><span class="pre">dict</span></tt>).  The return value of
 
225
<em>object_hook</em> will be used instead of the <tt class="xref docutils literal"><span class="pre">dict</span></tt>.  This feature can be used
 
226
to implement custom decoders (e.g. JSON-RPC class hinting).</p>
 
227
<p><em>object_pairs_hook</em> is an optional function that will be called with the
 
228
result of any object literal decode with an ordered list of pairs.  The
 
229
return value of <em>object_pairs_hook</em> will be used instead of the
 
230
<tt class="xref docutils literal"><span class="pre">dict</span></tt>.  This feature can be used to implement custom decoders that
 
231
rely on the order that the key and value pairs are decoded (for example,
 
232
<tt class="xref docutils literal"><span class="pre">collections.OrderedDict</span></tt> will remember the order of insertion). If
 
233
<em>object_hook</em> is also defined, the <em>object_pairs_hook</em> takes priority.</p>
 
234
<p>
 
235
<span class="versionmodified">Changed in version 2.1.0: </span>Added support for <em>object_pairs_hook</em>.</p>
 
236
<p><em>parse_float</em>, if specified, will be called with the string of every JSON
 
237
float to be decoded.  By default, this is equivalent to <tt class="docutils literal"><span class="pre">float(num_str)</span></tt>.
 
238
This can be used to use another datatype or parser for JSON floats
 
239
(e.g. <tt class="xref docutils literal"><span class="pre">decimal.Decimal</span></tt>).</p>
 
240
<p><em>parse_int</em>, if specified, will be called with the string of every JSON int
 
241
to be decoded.  By default, this is equivalent to <tt class="docutils literal"><span class="pre">int(num_str)</span></tt>.  This can
 
242
be used to use another datatype or parser for JSON integers
 
243
(e.g. <tt class="xref docutils literal"><span class="pre">float</span></tt>).</p>
 
244
<p><em>parse_constant</em>, if specified, will be called with one of the following
 
245
strings: <tt class="docutils literal"><span class="pre">'-Infinity'</span></tt>, <tt class="docutils literal"><span class="pre">'Infinity'</span></tt>, <tt class="docutils literal"><span class="pre">'NaN'</span></tt>.  This can be used to
 
246
raise an exception if invalid JSON numbers are encountered.</p>
 
247
<p>If <em>use_decimal</em> is true (default: <tt class="xref docutils literal"><span class="pre">False</span></tt>) then <em>parse_float</em> is set to
 
248
<tt class="xref docutils literal"><span class="pre">decimal.Decimal</span></tt>. This is a convenience for parity with the
 
249
<a title="simplejson.dump" class="reference internal" href="#simplejson.dump"><tt class="xref docutils literal"><span class="pre">dump()</span></tt></a> parameter.</p>
 
250
<p>
 
251
<span class="versionmodified">Changed in version 2.1.0: </span><em>use_decimal</em> is new in 2.1.0.</p>
 
252
<p>To use a custom <a title="simplejson.JSONDecoder" class="reference internal" href="#simplejson.JSONDecoder"><tt class="xref docutils literal"><span class="pre">JSONDecoder</span></tt></a> subclass, specify it with the <tt class="docutils literal"><span class="pre">cls</span></tt>
 
253
kwarg.  Additional keyword arguments will be passed to the constructor of the
 
254
class.</p>
 
255
<blockquote>
 
256
<div class="admonition note">
 
257
<p class="first admonition-title">Note</p>
 
258
<p class="last"><a title="simplejson.load" class="reference internal" href="#simplejson.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
 
259
then call <a title="simplejson.loads" class="reference internal" href="#simplejson.loads"><tt class="xref docutils literal"><span class="pre">loads()</span></tt></a>. It does not stop at the end of the first valid
 
260
JSON document it finds and it will raise an error if there is anything
 
261
other than whitespace after the document. Except for files containing
 
262
only one JSON document, it is recommended to use <a title="simplejson.loads" class="reference internal" href="#simplejson.loads"><tt class="xref docutils literal"><span class="pre">loads()</span></tt></a>.</p>
 
263
</div>
 
264
</blockquote>
 
265
</dd></dl>
 
266
 
 
267
<dl class="function">
 
268
<dt id="simplejson.loads">
 
269
<!--[simplejson.loads]--><tt class="descclassname">simplejson.</tt><tt class="descname">loads</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>encoding</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">[</span>, <em>parse_constant</em><span class="optional">[</span>, <em>object_pairs_hook</em><span class="optional">[</span>, <em>use_decimal</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">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.loads" title="Permalink to this definition">¶</a></dt>
 
270
<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">unicode</span></tt> instance containing a JSON
 
271
document) to a Python object.</p>
 
272
<p>If <em>s</em> is a <tt class="xref docutils literal"><span class="pre">str</span></tt> instance and is encoded with an ASCII based encoding
 
273
other than UTF-8 (e.g. latin-1), then an appropriate <em>encoding</em> name must be
 
274
specified.  Encodings that are not ASCII based (such as UCS-2) are not
 
275
allowed and should be decoded to <tt class="xref docutils literal"><span class="pre">unicode</span></tt> first.</p>
 
276
<p>If <em>s</em> is a <tt class="xref docutils literal"><span class="pre">str</span></tt> then decoded JSON strings that contain
 
277
only ASCII characters may be parsed as <tt class="xref docutils literal"><span class="pre">str</span></tt> for performance and
 
278
memory reasons. If your code expects only <tt class="xref docutils literal"><span class="pre">unicode</span></tt> the appropriate
 
279
solution is decode <em>s</em> to <tt class="xref docutils literal"><span class="pre">unicode</span></tt> prior to calling loads.</p>
 
280
<p>The other arguments have the same meaning as in <a title="simplejson.load" class="reference internal" href="#simplejson.load"><tt class="xref docutils literal"><span class="pre">load()</span></tt></a>.</p>
 
281
</dd></dl>
 
282
 
 
283
</div>
 
284
<div class="section" id="encoders-and-decoders">
 
285
<h2 id="encoders-and-decoders">Encoders and decoders<a class="headerlink" href="#encoders-and-decoders" title="Permalink to this headline">¶</a></h2>
 
286
<dl class="class">
 
287
<dt id="simplejson.JSONDecoder">
 
288
<!--[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>parse_int</em><span class="optional">[</span>, <em>parse_constant</em><span class="optional">[</span>, <em>object_pairs_hook</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><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.JSONDecoder" title="Permalink to this definition">¶</a></dt>
 
289
<dd><p>Simple JSON decoder.</p>
 
290
<p>Performs the following translations in decoding by default:</p>
 
291
<table border="1" class="docutils">
 
292
<colgroup>
 
293
<col width="44%" />
 
294
<col width="56%" />
 
295
</colgroup>
 
296
<thead valign="bottom">
 
297
<tr><th class="head">JSON</th>
 
298
<th class="head">Python</th>
 
299
</tr>
 
300
</thead>
 
301
<tbody valign="top">
 
302
<tr><td>object</td>
 
303
<td>dict</td>
 
304
</tr>
 
305
<tr><td>array</td>
 
306
<td>list</td>
 
307
</tr>
 
308
<tr><td>string</td>
 
309
<td>unicode</td>
 
310
</tr>
 
311
<tr><td>number (int)</td>
 
312
<td>int, long</td>
 
313
</tr>
 
314
<tr><td>number (real)</td>
 
315
<td>float</td>
 
316
</tr>
 
317
<tr><td>true</td>
 
318
<td>True</td>
 
319
</tr>
 
320
<tr><td>false</td>
 
321
<td>False</td>
 
322
</tr>
 
323
<tr><td>null</td>
 
324
<td>None</td>
 
325
</tr>
 
326
</tbody>
 
327
</table>
 
328
<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
 
329
corresponding <tt class="docutils literal"><span class="pre">float</span></tt> values, which is outside the JSON spec.</p>
 
330
<p><em>encoding</em> determines the encoding used to interpret any <tt class="xref docutils literal"><span class="pre">str</span></tt> objects
 
331
decoded by this instance (<tt class="docutils literal"><span class="pre">'utf-8'</span></tt> by default).  It has no effect when decoding
 
332
<tt class="xref docutils literal"><span class="pre">unicode</span></tt> objects.</p>
 
333
<p>Note that currently only encodings that are a superset of ASCII work, strings
 
334
of other encodings should be passed in as <tt class="xref docutils literal"><span class="pre">unicode</span></tt>.</p>
 
335
<p><em>object_hook</em> is an optional function that will be called with the result of
 
336
every JSON object decoded and its return value will be used in place of the
 
337
given <tt class="xref docutils literal"><span class="pre">dict</span></tt>.  This can be used to provide custom deserializations
 
338
(e.g. to support JSON-RPC class hinting).</p>
 
339
<p><em>object_pairs_hook</em> is an optional function that will be called with the
 
340
result of any object literal decode with an ordered list of pairs.  The
 
341
return value of <em>object_pairs_hook</em> will be used instead of the
 
342
<tt class="xref docutils literal"><span class="pre">dict</span></tt>.  This feature can be used to implement custom decoders that
 
343
rely on the order that the key and value pairs are decoded (for example,
 
344
<tt class="xref docutils literal"><span class="pre">collections.OrderedDict</span></tt> will remember the order of insertion). If
 
345
<em>object_hook</em> is also defined, the <em>object_pairs_hook</em> takes priority.</p>
 
346
<p>
 
347
<span class="versionmodified">Changed in version 2.1.0: </span>Added support for <em>object_pairs_hook</em>.</p>
 
348
<p><em>parse_float</em>, if specified, will be called with the string of every JSON
 
349
float to be decoded.  By default, this is equivalent to <tt class="docutils literal"><span class="pre">float(num_str)</span></tt>.
 
350
This can be used to use another datatype or parser for JSON floats
 
351
(e.g. <tt class="xref docutils literal"><span class="pre">decimal.Decimal</span></tt>).</p>
 
352
<p><em>parse_int</em>, if specified, will be called with the string of every JSON int
 
353
to be decoded.  By default, this is equivalent to <tt class="docutils literal"><span class="pre">int(num_str)</span></tt>.  This can
 
354
be used to use another datatype or parser for JSON integers
 
355
(e.g. <tt class="xref docutils literal"><span class="pre">float</span></tt>).</p>
 
356
<p><em>parse_constant</em>, if specified, will be called with one of the following
 
357
strings: <tt class="docutils literal"><span class="pre">'-Infinity'</span></tt>, <tt class="docutils literal"><span class="pre">'Infinity'</span></tt>, <tt class="docutils literal"><span class="pre">'NaN'</span></tt>.  This can be used to
 
358
raise an exception if invalid JSON numbers are encountered.</p>
 
359
<p><em>strict</em> controls the parser&#8217;s behavior when it encounters an invalid
 
360
control character in a string. The default setting of <tt class="xref docutils literal"><span class="pre">True</span></tt> means that
 
361
unescaped control characters are parse errors, if <tt class="xref docutils literal"><span class="pre">False</span></tt> then control
 
362
characters will be allowed in strings.</p>
 
363
<dl class="method">
 
364
<dt id="simplejson.JSONDecoder.decode">
 
365
<!--[simplejson.JSONDecoder.decode]--><tt class="descname">decode</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#simplejson.JSONDecoder.decode" title="Permalink to this definition">¶</a></dt>
 
366
<dd><p>Return the Python representation of <em>s</em> (a <tt class="xref docutils literal"><span class="pre">str</span></tt> or
 
367
<tt class="xref docutils literal"><span class="pre">unicode</span></tt> instance containing a JSON document)</p>
 
368
<p>If <em>s</em> is a <tt class="xref docutils literal"><span class="pre">str</span></tt> then decoded JSON strings that contain
 
369
only ASCII characters may be parsed as <tt class="xref docutils literal"><span class="pre">str</span></tt> for performance and
 
370
memory reasons. If your code expects only <tt class="xref docutils literal"><span class="pre">unicode</span></tt> the
 
371
appropriate solution is decode <em>s</em> to <tt class="xref docutils literal"><span class="pre">unicode</span></tt> prior to calling
 
372
decode.</p>
 
373
</dd></dl>
 
374
 
 
375
<dl class="method">
 
376
<dt id="simplejson.JSONDecoder.raw_decode">
 
377
<!--[simplejson.JSONDecoder.raw_decode]--><tt class="descname">raw_decode</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#simplejson.JSONDecoder.raw_decode" title="Permalink to this definition">¶</a></dt>
 
378
<dd><p>Decode a JSON document from <em>s</em> (a <tt class="xref docutils literal"><span class="pre">str</span></tt> or <tt class="xref docutils literal"><span class="pre">unicode</span></tt>
 
379
beginning with a JSON document) and return a 2-tuple of the Python
 
380
representation and the index in <em>s</em> where the document ended.</p>
 
381
<p>This can be used to decode a JSON document from a string that may have
 
382
extraneous data at the end.</p>
 
383
</dd></dl>
 
384
 
 
385
</dd></dl>
 
386
 
 
387
<dl class="class">
 
388
<dt id="simplejson.JSONEncoder">
 
389
<!--[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 class="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>separators</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 class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder" title="Permalink to this definition">¶</a></dt>
 
390
<dd><p>Extensible JSON encoder for Python data structures.</p>
 
391
<p>Supports the following objects and types by default:</p>
 
392
<table border="1" class="docutils">
 
393
<colgroup>
 
394
<col width="56%" />
 
395
<col width="44%" />
 
396
</colgroup>
 
397
<thead valign="bottom">
 
398
<tr><th class="head">Python</th>
 
399
<th class="head">JSON</th>
 
400
</tr>
 
401
</thead>
 
402
<tbody valign="top">
 
403
<tr><td>dict</td>
 
404
<td>object</td>
 
405
</tr>
 
406
<tr><td>list, tuple</td>
 
407
<td>array</td>
 
408
</tr>
 
409
<tr><td>str, unicode</td>
 
410
<td>string</td>
 
411
</tr>
 
412
<tr><td>int, long, float</td>
 
413
<td>number</td>
 
414
</tr>
 
415
<tr><td>True</td>
 
416
<td>true</td>
 
417
</tr>
 
418
<tr><td>False</td>
 
419
<td>false</td>
 
420
</tr>
 
421
<tr><td>None</td>
 
422
<td>null</td>
 
423
</tr>
 
424
</tbody>
 
425
</table>
 
426
<p>To extend this to recognize other objects, subclass and implement a
 
427
<a title="simplejson.JSONEncoder.default" class="reference internal" href="#simplejson.JSONEncoder.default"><tt class="xref docutils literal"><span class="pre">default()</span></tt></a> method with another method that returns a serializable object
 
428
for <tt class="docutils literal"><span class="pre">o</span></tt> if possible, otherwise it should call the superclass implementation
 
429
(to raise <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>).</p>
 
430
<p>If <em>skipkeys</em> is false (the default), then it is a <tt class="xref docutils literal"><span class="pre">TypeError</span></tt> to
 
431
attempt encoding of keys that are not str, int, long, float or None.  If
 
432
<em>skipkeys</em> is true, such items are simply skipped.</p>
 
433
<p>If <em>ensure_ascii</em> is true (the default), the output is guaranteed to be
 
434
<tt class="xref docutils literal"><span class="pre">str</span></tt> objects with all incoming unicode characters escaped.  If
 
435
<em>ensure_ascii</em> is false, the output will be a unicode object.</p>
 
436
<p>If <em>check_circular</em> is false (the default), then lists, dicts, and custom
 
437
encoded objects will be checked for circular references during encoding to
 
438
prevent an infinite recursion (which would cause an <tt class="xref docutils literal"><span class="pre">OverflowError</span></tt>).
 
439
Otherwise, no such check takes place.</p>
 
440
<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="pre">Infinity</span></tt>, and
 
441
<tt class="docutils literal"><span class="pre">-Infinity</span></tt> will be encoded as such.  This behavior is not JSON
 
442
specification compliant, but is consistent with most JavaScript based
 
443
encoders and decoders.  Otherwise, it will be a <tt class="xref docutils literal"><span class="pre">ValueError</span></tt> to encode
 
444
such floats.</p>
 
445
<p>If <em>sort_keys</em> is true (not the default), then the output of dictionaries
 
446
will be sorted by key; this is useful for regression tests to ensure that
 
447
JSON serializations can be compared on a day-to-day basis.</p>
 
448
<p>If <em>indent</em> is a string, then JSON array elements and object members
 
449
will be pretty-printed with a newline followed by that string repeated
 
450
for each level of nesting. <tt class="xref docutils literal"><span class="pre">None</span></tt> (the default) selects the most compact
 
451
representation without any newlines. For backwards compatibility with
 
452
versions of simplejson earlier than 2.1.0, an integer is also accepted
 
453
and is converted to a string with that many spaces.</p>
 
454
<p>
 
455
<span class="versionmodified">Changed in version 2.1.0: </span>Changed <em>indent</em> from an integer number of spaces to a string.</p>
 
456
<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>
 
457
tuple.  By default, <tt class="docutils literal"><span class="pre">(',</span> <span class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span></tt> are used.  To get the most compact JSON
 
458
representation, you should specify <tt class="docutils literal"><span class="pre">(',',</span> <span class="pre">':')</span></tt> to eliminate whitespace.</p>
 
459
<p>If specified, <em>default</em> should be a function that gets called for objects
 
460
that can&#8217;t otherwise be serialized.  It should return a JSON encodable
 
461
version of the object or raise a <tt class="xref docutils literal"><span class="pre">TypeError</span></tt>.</p>
 
462
<p>If <em>encoding</em> is not <tt class="xref docutils literal"><span class="pre">None</span></tt>, then all input strings will be transformed
 
463
into unicode using that encoding prior to JSON-encoding.  The default is
 
464
<tt class="docutils literal"><span class="pre">'utf-8'</span></tt>.</p>
 
465
<dl class="method">
 
466
<dt id="simplejson.JSONEncoder.default">
 
467
<!--[simplejson.JSONEncoder.default]--><tt class="descname">default</tt><big>(</big><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder.default" title="Permalink to this definition">¶</a></dt>
 
468
<dd><p>Implement this method in a subclass such that it returns a serializable
 
469
object for <em>o</em>, or calls the base implementation (to raise a
 
470
<tt class="xref docutils literal"><span class="pre">TypeError</span></tt>).</p>
 
471
<p>For example, to support arbitrary iterators, you could implement default
 
472
like this:</p>
 
473
<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">,</span> <span class="n">o</span><span class="p">):</span>
 
474
   <span class="k">try</span><span class="p">:</span>
 
475
       <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">)</span>
 
476
   <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
 
477
       <span class="k">pass</span>
 
478
   <span class="k">else</span><span class="p">:</span>
 
479
       <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
 
480
   <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>
 
481
</pre></div>
 
482
</dd></dl>
 
483
 
 
484
<dl class="method">
 
485
<dt id="simplejson.JSONEncoder.encode">
 
486
<!--[simplejson.JSONEncoder.encode]--><tt class="descname">encode</tt><big>(</big><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder.encode" title="Permalink to this definition">¶</a></dt>
 
487
<dd><p>Return a JSON string representation of a Python data structure, <em>o</em>.  For
 
488
example:</p>
 
489
<div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">import</span> <span class="nn">simplejson</span> <span class="k">as</span> <span class="nn">json</span>
 
490
<span class="gp">&gt;&gt;&gt; </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">&quot;foo&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;baz&quot;</span><span class="p">]})</span>
 
491
<span class="go">&#39;{&quot;foo&quot;: [&quot;bar&quot;, &quot;baz&quot;]}&#39;</span>
 
492
</pre></div>
 
493
</dd></dl>
 
494
 
 
495
<dl class="method">
 
496
<dt id="simplejson.JSONEncoder.iterencode">
 
497
<!--[simplejson.JSONEncoder.iterencode]--><tt class="descname">iterencode</tt><big>(</big><em>o</em><big>)</big><a class="headerlink" href="#simplejson.JSONEncoder.iterencode" title="Permalink to this definition">¶</a></dt>
 
498
<dd><p>Encode the given object, <em>o</em>, and yield each string representation as
 
499
available.  For example:</p>
 
500
<div class="highlight"><pre><span class="k">for</span> <span class="n">chunk</span> <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>
 
501
    <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">)</span>
 
502
</pre></div>
 
503
<p>Note that <a title="simplejson.JSONEncoder.encode" class="reference internal" href="#simplejson.JSONEncoder.encode"><tt class="xref docutils literal"><span class="pre">encode()</span></tt></a> has much better performance than
 
504
<a title="simplejson.JSONEncoder.iterencode" class="reference internal" href="#simplejson.JSONEncoder.iterencode"><tt class="xref docutils literal"><span class="pre">iterencode()</span></tt></a>.</p>
 
505
</dd></dl>
 
506
 
 
507
</dd></dl>
 
508
 
 
509
<dl class="class">
 
510
<dt id="simplejson.JSONEncoderForHTML">
 
511
<!--[simplejson.JSONEncoderForHTML]-->class <tt class="descclassname">simplejson.</tt><tt class="descname">JSONEncoderForHTML</tt><big>(</big><span class="optional">[</span><em>skipkeys</em><span class="optional">[</span>, <em>ensure_ascii</em><span class="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>separators</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 class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#simplejson.JSONEncoderForHTML" title="Permalink to this definition">¶</a></dt>
 
512
<dd><p>Subclass of <a title="simplejson.JSONEncoder" class="reference internal" href="#simplejson.JSONEncoder"><tt class="xref docutils literal"><span class="pre">JSONEncoder</span></tt></a> that escapes &amp;, &lt;, and &gt; for embedding in HTML.</p>
 
513
<p>
 
514
<span class="versionmodified">Changed in version 2.1.0: </span>New in 2.1.0</p>
 
515
</dd></dl>
 
516
 
 
517
</div>
 
518
</div>
 
519
 
 
520
 
 
521
          </div>
 
522
        </div>
 
523
      </div>
 
524
      <div class="sphinxsidebar">
 
525
        <div class="sphinxsidebarwrapper">
 
526
            <h3>Table Of Contents</h3>
 
527
            <ul>
 
528
<li><a class="reference external" href=""><tt class="docutils literal"><span class="pre">simplejson</span></tt> &#8212; JSON encoder and decoder</a><ul>
 
529
<li><a class="reference external" href="#basic-usage">Basic Usage</a></li>
 
530
<li><a class="reference external" href="#encoders-and-decoders">Encoders and decoders</a></li>
 
531
</ul>
 
532
</li>
 
533
</ul>
 
534
 
 
535
            <h3>This Page</h3>
 
536
            <ul class="this-page-menu">
 
537
              <li><a href="_sources/index.txt">Show Source</a></li>
 
538
            </ul>
 
539
            <h3>Quick search</h3>
 
540
            <form class="search" action="search.html" method="get">
 
541
              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
 
542
              <input type="hidden" name="check_keywords" value="yes" />
 
543
              <input type="hidden" name="area" value="default" />
 
544
            </form>
 
545
        </div>
 
546
      </div>
 
547
      <div class="clearer"></div>
 
548
    </div>
 
549
    <div class="related">
 
550
      <h3>Navigation</h3>
 
551
      <ul>
 
552
        <li class="right" style="margin-right: 10px">
 
553
          <a href="genindex.html" title="General Index"
 
554
             accesskey="I">index</a></li>
 
555
        <li><a href="">simplejson v2.1.1 documentation</a> &raquo;</li>
 
556
      </ul>
 
557
    </div>
 
558
    <div class="footer">
 
559
      &copy; Copyright 2008, Bob Ippolito.
 
560
      Last updated on Mar 31, 2010.
 
561
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
 
562
    </div>
 
563
  </body>
 
564
</html>
 
 
b'\\ No newline at end of file'