1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">
<TITLE>simplejson.JSONEncoder -- Extensible JSON &lt;<a class="reference" href="http://json.org">http://json.org</a>&gt; encoder for Python data structures.</TITLE>
<LINK HREF="layout.css" TYPE="text/css" REL="stylesheet">
</HEAD>
<BODY>
<DIV ID="page">
<DIV ID="top-nav">
<H1 ID="doc-title">
<A HREF="index.html" REL="index">simplejson 1.3</A></H1>
<DIV CLASS="online-navigation">
<A HREF="index.html" REL="index">index</A>
<SPAN ID="nav-docs">
</SPAN>
<BR>
<A HREF="module-simplejson.html" TITLE="simplejson reference">
simplejson
</A>
<A HREF="class-simplejson.JSONEncoder.html">details</A>
<A HREF="class-simplejson.JSONEncoder-index.html">
tree
</A>
</DIV>
</DIV>
<DIV ID="main-content">
<H1 CLASS="pudge-member-page-heading">
<TT>JSONEncoder</TT>
</H1>
<H4 CLASS="pudge-member-page-subheading">
Extensible JSON <<A HREF="http://json.org" CLASS="reference">http://json.org</A>> encoder for Python data structures.
</H4>
<P CLASS="pudge-member-parent-link">
<SMALL>
The JSONEncoder class is accessible via the
<A HREF="module-simplejson.html">
<TT>simplejson</TT>
</A> module.
</SMALL>
</P>
<DIV ID="pudge-section-nav">
<UL>
<LI>
<SPAN CLASS="pudge-missing-section-link">
Attributes
</SPAN>
</LI>
<LI>
<A HREF="#methods" CLASS="pudge-section-link">
Methods (4)
</A>
</LI>
<LI>
<A HREF="simplejson/encoder.py.html?f=63&l=288#63" CLASS="pudge-section-link">
Source
</A>
</LI>
</UL>
</DIV>
<DIV STYLE="clear: left">
</DIV>
<DIV CLASS="rst pudge-module-doc">
<P>Supports the following objects and types by default:</P>
<TABLE BORDER="1" CLASS="docutils">
<COLGROUP>
<COL WIDTH="56%">
<COL WIDTH="44%">
</COLGROUP>
<THEAD VALIGN="bottom">
<TR>
<TH CLASS="head">Python</TH>
<TH CLASS="head">JSON</TH>
</TR>
</THEAD>
<TBODY VALIGN="top">
<TR>
<TD>dict</TD>
<TD>object</TD>
</TR>
<TR>
<TD>list, tuple</TD>
<TD>array</TD>
</TR>
<TR>
<TD>str, unicode</TD>
<TD>string</TD>
</TR>
<TR>
<TD>int, long, float</TD>
<TD>number</TD>
</TR>
<TR>
<TD>True</TD>
<TD>true</TD>
</TR>
<TR>
<TD>False</TD>
<TD>false</TD>
</TR>
<TR>
<TD>None</TD>
<TD>null</TD>
</TR>
</TBODY>
</TABLE>
<P>To extend this to recognize other objects, subclass and implement a
<TT CLASS="docutils literal"><SPAN CLASS="pre">.default()</SPAN></TT> method with another method that returns a serializable
object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT> if possible, otherwise it should call the superclass
implementation (to raise <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).</P>
</DIV>
<HR>
<A NAME="methods">
</A>
<H2>Methods</H2>
<DIV CLASS="pudge-member routine alias">
<A NAME="__init__">
</A>
<H4 CLASS="pudge-member-name">
<SPAN CLASS="prefix">f</SPAN>
<TT>
<A HREF="class-simplejson.JSONEncoder.html#__init__" CLASS="pudge-obj-link">__init__</A>(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False)</TT>
<A HREF="simplejson/encoder.py.html?f=93&l=126#93" TITLE="View Source">...</A>
</H4>
<DIV CLASS="pudge-section rst">
<P CLASS="pudge-member-blurb">
Constructor for JSONEncoder, with sensible defaults.
</P>
<P>If skipkeys is False, then it is a TypeError to attempt
encoding of keys that are not str, int, long, float or None. If
skipkeys is True, such items are simply skipped.</P>
<P>If ensure_ascii is True, the output is guaranteed to be str
objects with all incoming unicode characters escaped. If
ensure_ascii is false, the output will be unicode object.</P>
<P>If check_circular is True, then lists, dicts, and custom encoded
objects will be checked for circular references during encoding to
prevent an infinite recursion (which would cause an OverflowError).
Otherwise, no such check takes place.</P>
<P>If allow_nan is True, then NaN, Infinity, and -Infinity will be
encoded as such. This behavior is not JSON specification compliant,
but is consistent with most JavaScript based encoders and decoders.
Otherwise, it will be a ValueError to encode such floats.</P>
<P>If sort_keys is True, then the output of dictionaries will be
sorted by key; this is useful for regression tests to ensure
that JSON serializations can be compared on a day-to-day basis.</P>
</DIV>
</DIV>
<DIV CLASS="pudge-member routine alias">
<A NAME="default">
</A>
<H4 CLASS="pudge-member-name">
<SPAN CLASS="prefix">f</SPAN>
<TT>
<A HREF="class-simplejson.JSONEncoder.html#default" CLASS="pudge-obj-link">default</A>(self, o)</TT>
<A HREF="simplejson/encoder.py.html?f=240&l=259#240" TITLE="View Source">...</A>
</H4>
<DIV CLASS="pudge-section rst">
<P CLASS="pudge-member-blurb">
Implement this method in a subclass such that it returns
a serializable object for <TT CLASS="docutils literal"><SPAN CLASS="pre">o</SPAN></TT>, or calls the base implementation
(to raise a <TT CLASS="docutils literal"><SPAN CLASS="pre">TypeError</SPAN></TT>).
</P>
<P>For example, to support arbitrary iterators, you could
implement default like this:</P>
<PRE CLASS="literal-block">
def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, o)
</PRE>
</DIV>
</DIV>
<DIV CLASS="pudge-member routine alias">
<A NAME="encode">
</A>
<H4 CLASS="pudge-member-name">
<SPAN CLASS="prefix">f</SPAN>
<TT>
<A HREF="class-simplejson.JSONEncoder.html#encode" CLASS="pudge-obj-link">encode</A>(self, o)</TT>
<A HREF="simplejson/encoder.py.html?f=260&l=272#260" TITLE="View Source">...</A>
</H4>
<DIV CLASS="pudge-section rst">
<P CLASS="pudge-member-blurb">
Return a JSON string representation of a Python data structure.
</P>
<PRE CLASS="doctest-block">
>>> JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo":["bar", "baz"]}'
</PRE>
</DIV>
</DIV>
<DIV CLASS="pudge-member routine alias">
<A NAME="iterencode">
</A>
<H4 CLASS="pudge-member-name">
<SPAN CLASS="prefix">f</SPAN>
<TT>
<A HREF="class-simplejson.JSONEncoder.html#iterencode" CLASS="pudge-obj-link">iterencode</A>(self, o)</TT>
<A HREF="simplejson/encoder.py.html?f=273&l=288#273" TITLE="View Source">...</A>
</H4>
<DIV CLASS="pudge-section rst">
<P CLASS="pudge-member-blurb">
Encode the given object and yield each string
representation as available.
</P>
<P>For example:</P>
<PRE CLASS="literal-block">
for chunk in JSONEncoder().iterencode(bigobject):
mysocket.write(chunk)
</PRE>
</DIV>
</DIV>
<P>
<SMALL>
See
<A HREF="simplejson/encoder.py.html?f=63&l=288#63" TITLE="simplejson/encoder.py:63">the source</A>
for more information.
</SMALL>
</P>
</DIV>
<DIV ID="footer">
<P ID="pudge">
Built with
<A HREF="http://lesscode.org/projects/pudge/">
Pudge/0.1.1</A>
</P>
</DIV>
</DIV>
</BODY>
</HTML>
|