blob: cbcac8deb7a433f636ee502c22bcc238753f6346 [file] [log] [blame]
Christian Heimes90540002008-05-08 14:29:10 +00001:mod:`json` --- JSON encoder and decoder
2========================================
3
4.. module:: json
5 :synopsis: Encode and decode the JSON format.
6.. moduleauthor:: Bob Ippolito <bob@redivi.com>
7.. sectionauthor:: Bob Ippolito <bob@redivi.com>
Christian Heimes90540002008-05-08 14:29:10 +00008
Benjamin Petersonf3352e72011-02-27 15:15:06 +00009`JSON (JavaScript Object Notation) <http://json.org>`_ is a subset of JavaScript
Christian Heimes90540002008-05-08 14:29:10 +000010syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
11
12:mod:`json` exposes an API familiar to users of the standard library
13:mod:`marshal` and :mod:`pickle` modules.
14
15Encoding basic Python object hierarchies::
Georg Brandl48310cd2009-01-03 21:18:54 +000016
Christian Heimes90540002008-05-08 14:29:10 +000017 >>> import json
18 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
19 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
Neal Norwitz752abd02008-05-13 04:55:24 +000020 >>> print(json.dumps("\"foo\bar"))
Christian Heimes90540002008-05-08 14:29:10 +000021 "\"foo\bar"
Benjamin Peterson2505bc62008-05-15 02:17:58 +000022 >>> print(json.dumps('\u1234'))
Christian Heimes90540002008-05-08 14:29:10 +000023 "\u1234"
Neal Norwitz752abd02008-05-13 04:55:24 +000024 >>> print(json.dumps('\\'))
Christian Heimes90540002008-05-08 14:29:10 +000025 "\\"
Neal Norwitz752abd02008-05-13 04:55:24 +000026 >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
Christian Heimes90540002008-05-08 14:29:10 +000027 {"a": 0, "b": 0, "c": 0}
Benjamin Peterson2505bc62008-05-15 02:17:58 +000028 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000029 >>> io = StringIO()
30 >>> json.dump(['streaming API'], io)
31 >>> io.getvalue()
32 '["streaming API"]'
33
34Compact encoding::
35
36 >>> import json
Éric Araujode579d42011-04-21 02:37:41 +020037 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))
Christian Heimes90540002008-05-08 14:29:10 +000038 '[1,2,3,{"4":5,"6":7}]'
39
40Pretty printing::
41
42 >>> import json
Neal Norwitz752abd02008-05-13 04:55:24 +000043 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
Christian Heimes90540002008-05-08 14:29:10 +000044 {
Georg Brandl48310cd2009-01-03 21:18:54 +000045 "4": 5,
Christian Heimes90540002008-05-08 14:29:10 +000046 "6": 7
47 }
48
49Decoding JSON::
Georg Brandl48310cd2009-01-03 21:18:54 +000050
Christian Heimes90540002008-05-08 14:29:10 +000051 >>> import json
52 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000053 ['foo', {'bar': ['baz', None, 1.0, 2]}]
Christian Heimes90540002008-05-08 14:29:10 +000054 >>> json.loads('"\\"foo\\bar"')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000055 '"foo\x08ar'
56 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000057 >>> io = StringIO('["streaming API"]')
58 >>> json.load(io)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000059 ['streaming API']
Christian Heimes90540002008-05-08 14:29:10 +000060
61Specializing JSON object decoding::
62
63 >>> import json
64 >>> def as_complex(dct):
65 ... if '__complex__' in dct:
66 ... return complex(dct['real'], dct['imag'])
67 ... return dct
Benjamin Peterson2505bc62008-05-15 02:17:58 +000068 ...
Christian Heimes90540002008-05-08 14:29:10 +000069 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
70 ... object_hook=as_complex)
71 (1+2j)
72 >>> import decimal
73 >>> json.loads('1.1', parse_float=decimal.Decimal)
74 Decimal('1.1')
75
76Extending :class:`JSONEncoder`::
Georg Brandl48310cd2009-01-03 21:18:54 +000077
Christian Heimes90540002008-05-08 14:29:10 +000078 >>> import json
79 >>> class ComplexEncoder(json.JSONEncoder):
80 ... def default(self, obj):
81 ... if isinstance(obj, complex):
82 ... return [obj.real, obj.imag]
83 ... return json.JSONEncoder.default(self, obj)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000084 ...
Georg Brandl0bb73b82010-09-03 22:36:22 +000085 >>> json.dumps(2 + 1j, cls=ComplexEncoder)
Christian Heimes90540002008-05-08 14:29:10 +000086 '[2.0, 1.0]'
87 >>> ComplexEncoder().encode(2 + 1j)
88 '[2.0, 1.0]'
89 >>> list(ComplexEncoder().iterencode(2 + 1j))
Georg Brandl0bb73b82010-09-03 22:36:22 +000090 ['[2.0', ', 1.0', ']']
Georg Brandl48310cd2009-01-03 21:18:54 +000091
Christian Heimes90540002008-05-08 14:29:10 +000092
Ezio Melotti84e59aa2012-04-13 21:02:18 -060093.. highlight:: bash
Christian Heimes90540002008-05-08 14:29:10 +000094
95Using json.tool from the shell to validate and pretty-print::
Georg Brandl48310cd2009-01-03 21:18:54 +000096
Christian Heimes90540002008-05-08 14:29:10 +000097 $ echo '{"json":"obj"}' | python -mjson.tool
98 {
99 "json": "obj"
100 }
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600101 $ echo '{1.2:3.4}' | python -mjson.tool
102 Expecting property name: line 1 column 1 (char 1)
Christian Heimes90540002008-05-08 14:29:10 +0000103
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600104.. highlight:: python3
Christian Heimes90540002008-05-08 14:29:10 +0000105
Georg Brandl48310cd2009-01-03 21:18:54 +0000106.. note::
Christian Heimes90540002008-05-08 14:29:10 +0000107
108 The JSON produced by this module's default settings is a subset of
109 YAML, so it may be used as a serializer for that as well.
110
111
112Basic Usage
113-----------
114
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000115.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000116
117 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
118 file-like object).
119
120 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000121 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
122 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000123
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000124 The :mod:`json` module always produces :class:`str` objects, not
125 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
126 input.
127
Éric Araujo6f7aa002012-01-16 10:09:20 +0100128 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
129 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
130 ``False``, these characters will be output as-is.
131
Christian Heimes90540002008-05-08 14:29:10 +0000132 If *check_circular* is ``False`` (default: ``True``), then the circular
133 reference check for container types will be skipped and a circular reference
134 will result in an :exc:`OverflowError` (or worse).
135
136 If *allow_nan* is ``False`` (default: ``True``), then it will be a
137 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
138 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
139 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
140
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000141 If *indent* is a non-negative integer or string, then JSON array elements and
142 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400143 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
144 selects the most compact representation. Using a positive integer indent
145 indents that many spaces per level. If *indent* is a string (such at '\t'),
146 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000147
148 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
149 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
150 ':')`` is the most compact JSON representation.
151
Christian Heimes90540002008-05-08 14:29:10 +0000152 *default(obj)* is a function that should return a serializable version of
153 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
154
Georg Brandl1f01deb2009-01-03 22:47:39 +0000155 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000156 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000157 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000158
159
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000160.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000161
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000162 Serialize *obj* to a JSON formatted :class:`str`. The arguments have the
163 same meaning as in :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000164
Ezio Melotti60adf952011-04-15 07:37:00 +0300165 .. note::
166
Georg Brandl340d2692011-04-16 16:54:15 +0200167 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
168 so trying to serialize multiple objects with repeated calls to
169 :func:`dump` using the same *fp* will result in an invalid JSON file.
170
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700171 .. note::
172
173 Keys in key/value pairs of JSON are always of the type :class:`str`. When
174 a dictionary is converted into JSON, all the keys of the dictionary are
175 coerced to strings. As a result of this, if a dictionary is convered
176 into JSON and then back into a dictionary, the dictionary may not equal
177 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
178 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000179
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000180.. function:: load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000181
182 Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
183 document) to a Python object.
184
Christian Heimes90540002008-05-08 14:29:10 +0000185 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000186 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000187 *object_hook* will be used instead of the :class:`dict`. This feature can be used
188 to implement custom decoders (e.g. JSON-RPC class hinting).
189
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000190 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000191 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000192 return value of *object_pairs_hook* will be used instead of the
193 :class:`dict`. This feature can be used to implement custom decoders that
194 rely on the order that the key and value pairs are decoded (for example,
195 :func:`collections.OrderedDict` will remember the order of insertion). If
196 *object_hook* is also defined, the *object_pairs_hook* takes priority.
197
198 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000199 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000200
Christian Heimes90540002008-05-08 14:29:10 +0000201 *parse_float*, if specified, will be called with the string of every JSON
202 float to be decoded. By default, this is equivalent to ``float(num_str)``.
203 This can be used to use another datatype or parser for JSON floats
204 (e.g. :class:`decimal.Decimal`).
205
206 *parse_int*, if specified, will be called with the string of every JSON int
207 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
208 be used to use another datatype or parser for JSON integers
209 (e.g. :class:`float`).
210
211 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200212 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
213 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000214 are encountered.
215
216 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000217 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
218 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000219
220
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000221.. function:: loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000222
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000223 Deserialize *s* (a :class:`str` instance containing a JSON document) to a
224 Python object.
Christian Heimes90540002008-05-08 14:29:10 +0000225
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000226 The other arguments have the same meaning as in :func:`load`, except
227 *encoding* which is ignored and deprecated.
Christian Heimes90540002008-05-08 14:29:10 +0000228
229
230Encoders and decoders
231---------------------
232
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000233.. class:: JSONDecoder(object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)
Christian Heimes90540002008-05-08 14:29:10 +0000234
235 Simple JSON decoder.
236
237 Performs the following translations in decoding by default:
238
239 +---------------+-------------------+
240 | JSON | Python |
241 +===============+===================+
242 | object | dict |
243 +---------------+-------------------+
244 | array | list |
245 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000246 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000247 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000248 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000249 +---------------+-------------------+
250 | number (real) | float |
251 +---------------+-------------------+
252 | true | True |
253 +---------------+-------------------+
254 | false | False |
255 +---------------+-------------------+
256 | null | None |
257 +---------------+-------------------+
258
259 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
260 corresponding ``float`` values, which is outside the JSON spec.
261
Christian Heimes90540002008-05-08 14:29:10 +0000262 *object_hook*, if specified, will be called with the result of every JSON
263 object decoded and its return value will be used in place of the given
264 :class:`dict`. This can be used to provide custom deserializations (e.g. to
265 support JSON-RPC class hinting).
266
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000267 *object_pairs_hook*, if specified will be called with the result of every
268 JSON object decoded with an ordered list of pairs. The return value of
269 *object_pairs_hook* will be used instead of the :class:`dict`. This
270 feature can be used to implement custom decoders that rely on the order
271 that the key and value pairs are decoded (for example,
272 :func:`collections.OrderedDict` will remember the order of insertion). If
273 *object_hook* is also defined, the *object_pairs_hook* takes priority.
274
275 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000276 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000277
Christian Heimes90540002008-05-08 14:29:10 +0000278 *parse_float*, if specified, will be called with the string of every JSON
279 float to be decoded. By default, this is equivalent to ``float(num_str)``.
280 This can be used to use another datatype or parser for JSON floats
281 (e.g. :class:`decimal.Decimal`).
282
283 *parse_int*, if specified, will be called with the string of every JSON int
284 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
285 be used to use another datatype or parser for JSON integers
286 (e.g. :class:`float`).
287
288 *parse_constant*, if specified, will be called with one of the following
289 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
290 ``'false'``. This can be used to raise an exception if invalid JSON numbers
291 are encountered.
292
Georg Brandld4460aa2010-10-15 17:03:02 +0000293 If *strict* is ``False`` (``True`` is the default), then control characters
294 will be allowed inside strings. Control characters in this context are
295 those with character codes in the 0-31 range, including ``'\t'`` (tab),
296 ``'\n'``, ``'\r'`` and ``'\0'``.
297
Christian Heimes90540002008-05-08 14:29:10 +0000298
299 .. method:: decode(s)
300
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000301 Return the Python representation of *s* (a :class:`str` instance
302 containing a JSON document)
Christian Heimes90540002008-05-08 14:29:10 +0000303
304 .. method:: raw_decode(s)
305
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000306 Decode a JSON document from *s* (a :class:`str` beginning with a
307 JSON document) and return a 2-tuple of the Python representation
308 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000309
310 This can be used to decode a JSON document from a string that may have
311 extraneous data at the end.
312
313
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000314.. class:: JSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
Christian Heimes90540002008-05-08 14:29:10 +0000315
316 Extensible JSON encoder for Python data structures.
317
318 Supports the following objects and types by default:
319
320 +-------------------+---------------+
321 | Python | JSON |
322 +===================+===============+
323 | dict | object |
324 +-------------------+---------------+
325 | list, tuple | array |
326 +-------------------+---------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000327 | str | string |
Christian Heimes90540002008-05-08 14:29:10 +0000328 +-------------------+---------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000329 | int, float | number |
Christian Heimes90540002008-05-08 14:29:10 +0000330 +-------------------+---------------+
331 | True | true |
332 +-------------------+---------------+
333 | False | false |
334 +-------------------+---------------+
335 | None | null |
336 +-------------------+---------------+
337
338 To extend this to recognize other objects, subclass and implement a
339 :meth:`default` method with another method that returns a serializable object
340 for ``o`` if possible, otherwise it should call the superclass implementation
341 (to raise :exc:`TypeError`).
342
343 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
Georg Brandl639ce962009-04-11 18:18:16 +0000344 attempt encoding of keys that are not str, int, float or None. If
Christian Heimes90540002008-05-08 14:29:10 +0000345 *skipkeys* is ``True``, such items are simply skipped.
346
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000347 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
348 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
349 ``False``, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000350
351 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
352 encoded objects will be checked for circular references during encoding to
353 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
354 Otherwise, no such check takes place.
355
356 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
357 ``-Infinity`` will be encoded as such. This behavior is not JSON
358 specification compliant, but is consistent with most JavaScript based
359 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
360 such floats.
361
Georg Brandl6a74da32010-08-22 20:23:38 +0000362 If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000363 will be sorted by key; this is useful for regression tests to ensure that
364 JSON serializations can be compared on a day-to-day basis.
365
366 If *indent* is a non-negative integer (it is ``None`` by default), then JSON
367 array elements and object members will be pretty-printed with that indent
368 level. An indent level of 0 will only insert newlines. ``None`` is the most
369 compact representation.
370
371 If specified, *separators* should be an ``(item_separator, key_separator)``
372 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
373 representation, you should specify ``(',', ':')`` to eliminate whitespace.
374
375 If specified, *default* is a function that gets called for objects that can't
376 otherwise be serialized. It should return a JSON encodable version of the
377 object or raise a :exc:`TypeError`.
378
Christian Heimes90540002008-05-08 14:29:10 +0000379
380 .. method:: default(o)
381
382 Implement this method in a subclass such that it returns a serializable
383 object for *o*, or calls the base implementation (to raise a
384 :exc:`TypeError`).
385
386 For example, to support arbitrary iterators, you could implement default
387 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000388
Christian Heimes90540002008-05-08 14:29:10 +0000389 def default(self, o):
390 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000391 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000392 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000393 pass
Christian Heimes90540002008-05-08 14:29:10 +0000394 else:
395 return list(iterable)
Georg Brandl0bb73b82010-09-03 22:36:22 +0000396 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000397
398
399 .. method:: encode(o)
400
401 Return a JSON string representation of a Python data structure, *o*. For
402 example::
403
Georg Brandl0bb73b82010-09-03 22:36:22 +0000404 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000405 '{"foo": ["bar", "baz"]}'
406
407
408 .. method:: iterencode(o)
409
410 Encode the given object, *o*, and yield each string representation as
411 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000412
Georg Brandl0bb73b82010-09-03 22:36:22 +0000413 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000414 mysocket.write(chunk)