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

« back to all changes in this revision

Viewing changes to mozilla/python/simplejson-2.1.1/index.rst

  • 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
:mod:`simplejson` --- JSON encoder and decoder
 
2
==============================================
 
3
 
 
4
.. module:: simplejson
 
5
   :synopsis: Encode and decode the JSON format.
 
6
.. moduleauthor:: Bob Ippolito <bob@redivi.com>
 
7
.. sectionauthor:: Bob Ippolito <bob@redivi.com>
 
8
 
 
9
JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript
 
10
syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
 
11
 
 
12
:mod:`simplejson` exposes an API familiar to users of the standard library
 
13
:mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
 
14
version of the :mod:`json` library contained in Python 2.6, but maintains
 
15
compatibility with Python 2.5 and (currently) has
 
16
significant performance advantages, even without using the optional C
 
17
extension for speedups.
 
18
 
 
19
Encoding basic Python object hierarchies::
 
20
 
 
21
    >>> import simplejson as json
 
22
    >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
 
23
    '["foo", {"bar": ["baz", null, 1.0, 2]}]'
 
24
    >>> print json.dumps("\"foo\bar")
 
25
    "\"foo\bar"
 
26
    >>> print json.dumps(u'\u1234')
 
27
    "\u1234"
 
28
    >>> print json.dumps('\\')
 
29
    "\\"
 
30
    >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
 
31
    {"a": 0, "b": 0, "c": 0}
 
32
    >>> from StringIO import StringIO
 
33
    >>> io = StringIO()
 
34
    >>> json.dump(['streaming API'], io)
 
35
    >>> io.getvalue()
 
36
    '["streaming API"]'
 
37
 
 
38
Compact encoding::
 
39
 
 
40
    >>> import simplejson as json
 
41
    >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
 
42
    '[1,2,3,{"4":5,"6":7}]'
 
43
 
 
44
Pretty printing::
 
45
 
 
46
    >>> import simplejson as json
 
47
    >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4 * ' ')
 
48
    >>> print '\n'.join([l.rstrip() for l in  s.splitlines()])
 
49
    {
 
50
        "4": 5,
 
51
        "6": 7
 
52
    }
 
53
 
 
54
Decoding JSON::
 
55
 
 
56
    >>> import simplejson as json
 
57
    >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
 
58
    >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
 
59
    True
 
60
    >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
 
61
    True
 
62
    >>> from StringIO import StringIO
 
63
    >>> io = StringIO('["streaming API"]')
 
64
    >>> json.load(io)[0] == 'streaming API'
 
65
    True
 
66
 
 
67
Using Decimal instead of float::
 
68
 
 
69
    >>> import simplejson as json
 
70
    >>> from decimal import Decimal
 
71
    >>> json.loads('1.1', use_decimal=True) == Decimal('1.1')
 
72
    True
 
73
    >>> json.dumps(Decimal('1.1'), use_decimal=True) == '1.1'
 
74
    True
 
75
 
 
76
Specializing JSON object decoding::
 
77
 
 
78
    >>> import simplejson as json
 
79
    >>> def as_complex(dct):
 
80
    ...     if '__complex__' in dct:
 
81
    ...         return complex(dct['real'], dct['imag'])
 
82
    ...     return dct
 
83
    ...
 
84
    >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
 
85
    ...     object_hook=as_complex)
 
86
    (1+2j)
 
87
    >>> import decimal
 
88
    >>> json.loads('1.1', parse_float=decimal.Decimal) == decimal.Decimal('1.1')
 
89
    True
 
90
 
 
91
Specializing JSON object encoding::
 
92
 
 
93
    >>> import simplejson as json
 
94
    >>> def encode_complex(obj):
 
95
    ...     if isinstance(obj, complex):
 
96
    ...         return [obj.real, obj.imag]
 
97
    ...     raise TypeError(repr(o) + " is not JSON serializable")
 
98
    ...
 
99
    >>> json.dumps(2 + 1j, default=encode_complex)
 
100
    '[2.0, 1.0]'
 
101
    >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
 
102
    '[2.0, 1.0]'
 
103
    >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
 
104
    '[2.0, 1.0]'
 
105
 
 
106
 
 
107
.. highlight:: none
 
108
 
 
109
Using :mod:`simplejson.tool` from the shell to validate and pretty-print::
 
110
 
 
111
    $ echo '{"json":"obj"}' | python -m simplejson.tool
 
112
    {
 
113
        "json": "obj"
 
114
    }
 
115
    $ echo '{ 1.2:3.4}' | python -m simplejson.tool
 
116
    Expecting property name: line 1 column 2 (char 2)
 
117
 
 
118
.. highlight:: python
 
119
 
 
120
.. note::
 
121
 
 
122
   The JSON produced by this module's default settings is a subset of
 
123
   YAML, so it may be used as a serializer for that as well.
 
124
 
 
125
 
 
126
Basic Usage
 
127
-----------
 
128
 
 
129
.. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, use_decimal[, **kw]]]]]]]]]]])
 
130
 
 
131
   Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
 
132
   file-like object).
 
133
 
 
134
   If *skipkeys* is true (default: ``False``), then dict keys that are not
 
135
   of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`,
 
136
   :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
 
137
   :exc:`TypeError`.
 
138
 
 
139
   If *ensure_ascii* is false (default: ``True``), then some chunks written
 
140
   to *fp* may be :class:`unicode` instances, subject to normal Python
 
141
   :class:`str` to :class:`unicode` coercion rules.  Unless ``fp.write()``
 
142
   explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this
 
143
   is likely to cause an error. It's best to leave the default settings, because
 
144
   they are safe and it is highly optimized.
 
145
 
 
146
   If *check_circular* is false (default: ``True``), then the circular
 
147
   reference check for container types will be skipped and a circular reference
 
148
   will result in an :exc:`OverflowError` (or worse).
 
149
 
 
150
   If *allow_nan* is false (default: ``True``), then it will be a
 
151
   :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
 
152
   ``inf``, ``-inf``) in strict compliance of the JSON specification.
 
153
   If *allow_nan* is true, their JavaScript equivalents will be used
 
154
   (``NaN``, ``Infinity``, ``-Infinity``).
 
155
 
 
156
   If *indent* is a string, then JSON array elements and object members
 
157
   will be pretty-printed with a newline followed by that string repeated
 
158
   for each level of nesting. ``None`` (the default) selects the most compact
 
159
   representation without any newlines. For backwards compatibility with
 
160
   versions of simplejson earlier than 2.1.0, an integer is also accepted
 
161
   and is converted to a string with that many spaces.
 
162
 
 
163
   .. versionchanged:: 2.1.0
 
164
      Changed *indent* from an integer number of spaces to a string.
 
165
 
 
166
   If specified, *separators* should be an ``(item_separator, dict_separator)``
 
167
   tuple.  By default, ``(', ', ': ')`` are used.  To get the most compact JSON
 
168
   representation, you should specify ``(',', ':')`` to eliminate whitespace.
 
169
 
 
170
   *encoding* is the character encoding for str instances, default is
 
171
   ``'utf-8'``.
 
172
 
 
173
   *default(obj)* is a function that should return a serializable version of
 
174
   *obj* or raise :exc:`TypeError`.  The default simply raises :exc:`TypeError`.
 
175
 
 
176
   To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
 
177
   :meth:`default` method to serialize additional types), specify it with the
 
178
   *cls* kwarg.
 
179
   
 
180
   If *use_decimal* is true (default: ``False``) then :class:`decimal.Decimal`
 
181
   will be natively serialized to JSON with full precision.
 
182
   
 
183
   .. versionchanged:: 2.1.0
 
184
      *use_decimal* is new in 2.1.0.
 
185
 
 
186
    .. note::
 
187
 
 
188
        JSON is not a framed protocol so unlike :mod:`pickle` or :mod:`marshal` it
 
189
        does not make sense to serialize more than one JSON document without some
 
190
        container protocol to delimit them.
 
191
 
 
192
 
 
193
.. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, use_decimal[, **kw]]]]]]]]]]])
 
194
 
 
195
   Serialize *obj* to a JSON formatted :class:`str`.
 
196
 
 
197
   If *ensure_ascii* is false, then the return value will be a
 
198
   :class:`unicode` instance.  The other arguments have the same meaning as in
 
199
   :func:`dump`. Note that the default *ensure_ascii* setting has much
 
200
   better performance.
 
201
 
 
202
 
 
203
.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, use_decimal[, **kw]]]]]]]]])
 
204
 
 
205
   Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
 
206
   document) to a Python object.
 
207
 
 
208
   If the contents of *fp* are encoded with an ASCII based encoding other than
 
209
   UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
 
210
   Encodings that are not ASCII based (such as UCS-2) are not allowed, and
 
211
   should be wrapped with ``codecs.getreader(fp)(encoding)``, or simply decoded
 
212
   to a :class:`unicode` object and passed to :func:`loads`. The default
 
213
   setting of ``'utf-8'`` is fastest and should be using whenever possible.
 
214
 
 
215
   If *fp.read()* returns :class:`str` then decoded JSON strings that contain
 
216
   only ASCII characters may be parsed as :class:`str` for performance and
 
217
   memory reasons. If your code expects only :class:`unicode` the appropriate
 
218
   solution is to wrap fp with a reader as demonstrated above.
 
219
 
 
220
   *object_hook* is an optional function that will be called with the result of
 
221
   any object literal decode (a :class:`dict`).  The return value of
 
222
   *object_hook* will be used instead of the :class:`dict`.  This feature can be used
 
223
   to implement custom decoders (e.g. JSON-RPC class hinting).
 
224
 
 
225
   *object_pairs_hook* is an optional function that will be called with the
 
226
   result of any object literal decode with an ordered list of pairs.  The
 
227
   return value of *object_pairs_hook* will be used instead of the
 
228
   :class:`dict`.  This feature can be used to implement custom decoders that
 
229
   rely on the order that the key and value pairs are decoded (for example,
 
230
   :class:`collections.OrderedDict` will remember the order of insertion). If
 
231
   *object_hook* is also defined, the *object_pairs_hook* takes priority.
 
232
 
 
233
   .. versionchanged:: 2.1.0
 
234
      Added support for *object_pairs_hook*.
 
235
 
 
236
   *parse_float*, if specified, will be called with the string of every JSON
 
237
   float to be decoded.  By default, this is equivalent to ``float(num_str)``.
 
238
   This can be used to use another datatype or parser for JSON floats
 
239
   (e.g. :class:`decimal.Decimal`).
 
240
 
 
241
   *parse_int*, if specified, will be called with the string of every JSON int
 
242
   to be decoded.  By default, this is equivalent to ``int(num_str)``.  This can
 
243
   be used to use another datatype or parser for JSON integers
 
244
   (e.g. :class:`float`).
 
245
 
 
246
   *parse_constant*, if specified, will be called with one of the following
 
247
   strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.  This can be used to
 
248
   raise an exception if invalid JSON numbers are encountered.
 
249
 
 
250
   If *use_decimal* is true (default: ``False``) then *parse_float* is set to
 
251
   :class:`decimal.Decimal`. This is a convenience for parity with the
 
252
   :func:`dump` parameter.
 
253
   
 
254
   .. versionchanged:: 2.1.0
 
255
      *use_decimal* is new in 2.1.0.
 
256
 
 
257
   To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
 
258
   kwarg.  Additional keyword arguments will be passed to the constructor of the
 
259
   class.
 
260
 
 
261
    .. note::
 
262
 
 
263
        :func:`load` will read the rest of the file-like object as a string and
 
264
        then call :func:`loads`. It does not stop at the end of the first valid
 
265
        JSON document it finds and it will raise an error if there is anything
 
266
        other than whitespace after the document. Except for files containing
 
267
        only one JSON document, it is recommended to use :func:`loads`.
 
268
 
 
269
 
 
270
.. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, use_decimal[, **kw]]]]]]]]])
 
271
 
 
272
   Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
 
273
   document) to a Python object.
 
274
 
 
275
   If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
 
276
   other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
 
277
   specified.  Encodings that are not ASCII based (such as UCS-2) are not
 
278
   allowed and should be decoded to :class:`unicode` first.
 
279
 
 
280
   If *s* is a :class:`str` then decoded JSON strings that contain
 
281
   only ASCII characters may be parsed as :class:`str` for performance and
 
282
   memory reasons. If your code expects only :class:`unicode` the appropriate
 
283
   solution is decode *s* to :class:`unicode` prior to calling loads.
 
284
 
 
285
   The other arguments have the same meaning as in :func:`load`.
 
286
 
 
287
 
 
288
Encoders and decoders
 
289
---------------------
 
290
 
 
291
.. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, strict]]]]]]])
 
292
 
 
293
   Simple JSON decoder.
 
294
 
 
295
   Performs the following translations in decoding by default:
 
296
 
 
297
   +---------------+-------------------+
 
298
   | JSON          | Python            |
 
299
   +===============+===================+
 
300
   | object        | dict              |
 
301
   +---------------+-------------------+
 
302
   | array         | list              |
 
303
   +---------------+-------------------+
 
304
   | string        | unicode           |
 
305
   +---------------+-------------------+
 
306
   | number (int)  | int, long         |
 
307
   +---------------+-------------------+
 
308
   | number (real) | float             |
 
309
   +---------------+-------------------+
 
310
   | true          | True              |
 
311
   +---------------+-------------------+
 
312
   | false         | False             |
 
313
   +---------------+-------------------+
 
314
   | null          | None              |
 
315
   +---------------+-------------------+
 
316
 
 
317
   It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
 
318
   corresponding ``float`` values, which is outside the JSON spec.
 
319
 
 
320
   *encoding* determines the encoding used to interpret any :class:`str` objects
 
321
   decoded by this instance (``'utf-8'`` by default).  It has no effect when decoding
 
322
   :class:`unicode` objects.
 
323
 
 
324
   Note that currently only encodings that are a superset of ASCII work, strings
 
325
   of other encodings should be passed in as :class:`unicode`.
 
326
 
 
327
   *object_hook* is an optional function that will be called with the result of
 
328
   every JSON object decoded and its return value will be used in place of the
 
329
   given :class:`dict`.  This can be used to provide custom deserializations
 
330
   (e.g. to support JSON-RPC class hinting).
 
331
 
 
332
   *object_pairs_hook* is an optional function that will be called with the
 
333
   result of any object literal decode with an ordered list of pairs.  The
 
334
   return value of *object_pairs_hook* will be used instead of the
 
335
   :class:`dict`.  This feature can be used to implement custom decoders that
 
336
   rely on the order that the key and value pairs are decoded (for example,
 
337
   :class:`collections.OrderedDict` will remember the order of insertion). If
 
338
   *object_hook* is also defined, the *object_pairs_hook* takes priority.
 
339
 
 
340
   .. versionchanged:: 2.1.0
 
341
      Added support for *object_pairs_hook*.
 
342
 
 
343
   *parse_float*, if specified, will be called with the string of every JSON
 
344
   float to be decoded.  By default, this is equivalent to ``float(num_str)``.
 
345
   This can be used to use another datatype or parser for JSON floats
 
346
   (e.g. :class:`decimal.Decimal`).
 
347
 
 
348
   *parse_int*, if specified, will be called with the string of every JSON int
 
349
   to be decoded.  By default, this is equivalent to ``int(num_str)``.  This can
 
350
   be used to use another datatype or parser for JSON integers
 
351
   (e.g. :class:`float`).
 
352
 
 
353
   *parse_constant*, if specified, will be called with one of the following
 
354
   strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.  This can be used to
 
355
   raise an exception if invalid JSON numbers are encountered.
 
356
 
 
357
   *strict* controls the parser's behavior when it encounters an invalid
 
358
   control character in a string. The default setting of ``True`` means that
 
359
   unescaped control characters are parse errors, if ``False`` then control
 
360
   characters will be allowed in strings.
 
361
 
 
362
   .. method:: decode(s)
 
363
 
 
364
      Return the Python representation of *s* (a :class:`str` or
 
365
      :class:`unicode` instance containing a JSON document)
 
366
 
 
367
      If *s* is a :class:`str` then decoded JSON strings that contain
 
368
      only ASCII characters may be parsed as :class:`str` for performance and
 
369
      memory reasons. If your code expects only :class:`unicode` the
 
370
      appropriate solution is decode *s* to :class:`unicode` prior to calling
 
371
      decode.
 
372
 
 
373
   .. method:: raw_decode(s)
 
374
 
 
375
      Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
 
376
      beginning with a JSON document) and return a 2-tuple of the Python
 
377
      representation and the index in *s* where the document ended.
 
378
 
 
379
      This can be used to decode a JSON document from a string that may have
 
380
      extraneous data at the end.
 
381
 
 
382
 
 
383
.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
 
384
 
 
385
   Extensible JSON encoder for Python data structures.
 
386
 
 
387
   Supports the following objects and types by default:
 
388
 
 
389
   +-------------------+---------------+
 
390
   | Python            | JSON          |
 
391
   +===================+===============+
 
392
   | dict              | object        |
 
393
   +-------------------+---------------+
 
394
   | list, tuple       | array         |
 
395
   +-------------------+---------------+
 
396
   | str, unicode      | string        |
 
397
   +-------------------+---------------+
 
398
   | int, long, float  | number        |
 
399
   +-------------------+---------------+
 
400
   | True              | true          |
 
401
   +-------------------+---------------+
 
402
   | False             | false         |
 
403
   +-------------------+---------------+
 
404
   | None              | null          |
 
405
   +-------------------+---------------+
 
406
 
 
407
   To extend this to recognize other objects, subclass and implement a
 
408
   :meth:`default` method with another method that returns a serializable object
 
409
   for ``o`` if possible, otherwise it should call the superclass implementation
 
410
   (to raise :exc:`TypeError`).
 
411
 
 
412
   If *skipkeys* is false (the default), then it is a :exc:`TypeError` to
 
413
   attempt encoding of keys that are not str, int, long, float or None.  If
 
414
   *skipkeys* is true, such items are simply skipped.
 
415
 
 
416
   If *ensure_ascii* is true (the default), the output is guaranteed to be
 
417
   :class:`str` objects with all incoming unicode characters escaped.  If
 
418
   *ensure_ascii* is false, the output will be a unicode object.
 
419
 
 
420
   If *check_circular* is false (the default), then lists, dicts, and custom
 
421
   encoded objects will be checked for circular references during encoding to
 
422
   prevent an infinite recursion (which would cause an :exc:`OverflowError`).
 
423
   Otherwise, no such check takes place.
 
424
 
 
425
   If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and
 
426
   ``-Infinity`` will be encoded as such.  This behavior is not JSON
 
427
   specification compliant, but is consistent with most JavaScript based
 
428
   encoders and decoders.  Otherwise, it will be a :exc:`ValueError` to encode
 
429
   such floats.
 
430
 
 
431
   If *sort_keys* is true (not the default), then the output of dictionaries
 
432
   will be sorted by key; this is useful for regression tests to ensure that
 
433
   JSON serializations can be compared on a day-to-day basis.
 
434
 
 
435
   If *indent* is a string, then JSON array elements and object members
 
436
   will be pretty-printed with a newline followed by that string repeated
 
437
   for each level of nesting. ``None`` (the default) selects the most compact
 
438
   representation without any newlines. For backwards compatibility with
 
439
   versions of simplejson earlier than 2.1.0, an integer is also accepted
 
440
   and is converted to a string with that many spaces.
 
441
 
 
442
   .. versionchanged:: 2.1.0
 
443
      Changed *indent* from an integer number of spaces to a string.
 
444
 
 
445
   If specified, *separators* should be an ``(item_separator, key_separator)``
 
446
   tuple.  By default, ``(', ', ': ')`` are used.  To get the most compact JSON
 
447
   representation, you should specify ``(',', ':')`` to eliminate whitespace.
 
448
 
 
449
   If specified, *default* should be a function that gets called for objects
 
450
   that can't otherwise be serialized.  It should return a JSON encodable
 
451
   version of the object or raise a :exc:`TypeError`.
 
452
 
 
453
   If *encoding* is not ``None``, then all input strings will be transformed
 
454
   into unicode using that encoding prior to JSON-encoding.  The default is
 
455
   ``'utf-8'``.
 
456
 
 
457
 
 
458
   .. method:: default(o)
 
459
 
 
460
      Implement this method in a subclass such that it returns a serializable
 
461
      object for *o*, or calls the base implementation (to raise a
 
462
      :exc:`TypeError`).
 
463
 
 
464
      For example, to support arbitrary iterators, you could implement default
 
465
      like this::
 
466
 
 
467
         def default(self, o):
 
468
            try:
 
469
                iterable = iter(o)
 
470
            except TypeError:
 
471
                pass
 
472
            else:
 
473
                return list(iterable)
 
474
            return JSONEncoder.default(self, o)
 
475
 
 
476
 
 
477
   .. method:: encode(o)
 
478
 
 
479
      Return a JSON string representation of a Python data structure, *o*.  For
 
480
      example::
 
481
 
 
482
        >>> import simplejson as json
 
483
        >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
 
484
        '{"foo": ["bar", "baz"]}'
 
485
 
 
486
 
 
487
   .. method:: iterencode(o)
 
488
 
 
489
      Encode the given object, *o*, and yield each string representation as
 
490
      available.  For example::
 
491
 
 
492
            for chunk in JSONEncoder().iterencode(bigobject):
 
493
                mysocket.write(chunk)
 
494
 
 
495
      Note that :meth:`encode` has much better performance than
 
496
      :meth:`iterencode`.
 
497
 
 
498
.. class:: JSONEncoderForHTML([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
 
499
 
 
500
   Subclass of :class:`JSONEncoder` that escapes &, <, and > for embedding in HTML.
 
501
 
 
502
   .. versionchanged:: 2.1.0
 
503
      New in 2.1.0