blob: f9547cb3587f99bb0262712cf6a9d0158565d4b0 [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
Antoine Pitrou331624b2012-08-24 19:37:23 +02009`JSON (JavaScript Object Notation) <http://json.org>`_, specified by
10:rfc:`4627`, is a lightweight data interchange format based on a subset of
11`JavaScript <http://en.wikipedia.org/wiki/JavaScript>`_ syntax (`ECMA-262 3rd
12edition <http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf>`_).
Christian Heimes90540002008-05-08 14:29:10 +000013
14:mod:`json` exposes an API familiar to users of the standard library
15:mod:`marshal` and :mod:`pickle` modules.
16
17Encoding basic Python object hierarchies::
Georg Brandl48310cd2009-01-03 21:18:54 +000018
Christian Heimes90540002008-05-08 14:29:10 +000019 >>> import json
20 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
21 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
Neal Norwitz752abd02008-05-13 04:55:24 +000022 >>> print(json.dumps("\"foo\bar"))
Christian Heimes90540002008-05-08 14:29:10 +000023 "\"foo\bar"
Benjamin Peterson2505bc62008-05-15 02:17:58 +000024 >>> print(json.dumps('\u1234'))
Christian Heimes90540002008-05-08 14:29:10 +000025 "\u1234"
Neal Norwitz752abd02008-05-13 04:55:24 +000026 >>> print(json.dumps('\\'))
Christian Heimes90540002008-05-08 14:29:10 +000027 "\\"
Neal Norwitz752abd02008-05-13 04:55:24 +000028 >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
Christian Heimes90540002008-05-08 14:29:10 +000029 {"a": 0, "b": 0, "c": 0}
Benjamin Peterson2505bc62008-05-15 02:17:58 +000030 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000031 >>> io = StringIO()
32 >>> json.dump(['streaming API'], io)
33 >>> io.getvalue()
34 '["streaming API"]'
35
36Compact encoding::
37
38 >>> import json
Éric Araujode579d42011-04-21 02:37:41 +020039 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))
Christian Heimes90540002008-05-08 14:29:10 +000040 '[1,2,3,{"4":5,"6":7}]'
41
42Pretty printing::
43
44 >>> import json
Neal Norwitz752abd02008-05-13 04:55:24 +000045 >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
Christian Heimes90540002008-05-08 14:29:10 +000046 {
Georg Brandl48310cd2009-01-03 21:18:54 +000047 "4": 5,
Christian Heimes90540002008-05-08 14:29:10 +000048 "6": 7
49 }
50
51Decoding JSON::
Georg Brandl48310cd2009-01-03 21:18:54 +000052
Christian Heimes90540002008-05-08 14:29:10 +000053 >>> import json
54 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000055 ['foo', {'bar': ['baz', None, 1.0, 2]}]
Christian Heimes90540002008-05-08 14:29:10 +000056 >>> json.loads('"\\"foo\\bar"')
Benjamin Peterson2505bc62008-05-15 02:17:58 +000057 '"foo\x08ar'
58 >>> from io import StringIO
Christian Heimes90540002008-05-08 14:29:10 +000059 >>> io = StringIO('["streaming API"]')
60 >>> json.load(io)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000061 ['streaming API']
Christian Heimes90540002008-05-08 14:29:10 +000062
63Specializing JSON object decoding::
64
65 >>> import json
66 >>> def as_complex(dct):
67 ... if '__complex__' in dct:
68 ... return complex(dct['real'], dct['imag'])
69 ... return dct
Benjamin Peterson2505bc62008-05-15 02:17:58 +000070 ...
Christian Heimes90540002008-05-08 14:29:10 +000071 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
72 ... object_hook=as_complex)
73 (1+2j)
74 >>> import decimal
75 >>> json.loads('1.1', parse_float=decimal.Decimal)
76 Decimal('1.1')
77
78Extending :class:`JSONEncoder`::
Georg Brandl48310cd2009-01-03 21:18:54 +000079
Christian Heimes90540002008-05-08 14:29:10 +000080 >>> import json
81 >>> class ComplexEncoder(json.JSONEncoder):
82 ... def default(self, obj):
83 ... if isinstance(obj, complex):
84 ... return [obj.real, obj.imag]
85 ... return json.JSONEncoder.default(self, obj)
Benjamin Peterson2505bc62008-05-15 02:17:58 +000086 ...
Georg Brandl0bb73b82010-09-03 22:36:22 +000087 >>> json.dumps(2 + 1j, cls=ComplexEncoder)
Christian Heimes90540002008-05-08 14:29:10 +000088 '[2.0, 1.0]'
89 >>> ComplexEncoder().encode(2 + 1j)
90 '[2.0, 1.0]'
91 >>> list(ComplexEncoder().iterencode(2 + 1j))
Georg Brandl0bb73b82010-09-03 22:36:22 +000092 ['[2.0', ', 1.0', ']']
Georg Brandl48310cd2009-01-03 21:18:54 +000093
Christian Heimes90540002008-05-08 14:29:10 +000094
Ezio Melotti84e59aa2012-04-13 21:02:18 -060095.. highlight:: bash
Christian Heimes90540002008-05-08 14:29:10 +000096
97Using json.tool from the shell to validate and pretty-print::
Georg Brandl48310cd2009-01-03 21:18:54 +000098
Christian Heimes90540002008-05-08 14:29:10 +000099 $ echo '{"json":"obj"}' | python -mjson.tool
100 {
101 "json": "obj"
102 }
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600103 $ echo '{1.2:3.4}' | python -mjson.tool
Antoine Pitrou2d24e942012-06-29 01:58:26 +0200104 Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
Christian Heimes90540002008-05-08 14:29:10 +0000105
Ezio Melotti84e59aa2012-04-13 21:02:18 -0600106.. highlight:: python3
Christian Heimes90540002008-05-08 14:29:10 +0000107
Georg Brandl48310cd2009-01-03 21:18:54 +0000108.. note::
Christian Heimes90540002008-05-08 14:29:10 +0000109
Antoine Pitrou331624b2012-08-24 19:37:23 +0200110 JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by
111 this module's default settings (in particular, the default *separators*
112 value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
113 used as a YAML serializer.
Christian Heimes90540002008-05-08 14:29:10 +0000114
115
116Basic Usage
117-----------
118
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200119.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
120 check_circular=True, allow_nan=True, cls=None, \
121 indent=None, separators=None, default=None, \
122 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000123
124 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Antoine Pitrou15251a92012-08-24 19:49:08 +0200125 :term:`file-like object`).
Christian Heimes90540002008-05-08 14:29:10 +0000126
127 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000128 of a basic type (:class:`str`, :class:`int`, :class:`float`, :class:`bool`,
129 ``None``) will be skipped instead of raising a :exc:`TypeError`.
Christian Heimes90540002008-05-08 14:29:10 +0000130
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000131 The :mod:`json` module always produces :class:`str` objects, not
132 :class:`bytes` objects. Therefore, ``fp.write()`` must support :class:`str`
133 input.
134
Éric Araujo6f7aa002012-01-16 10:09:20 +0100135 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
136 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
137 ``False``, these characters will be output as-is.
138
Christian Heimes90540002008-05-08 14:29:10 +0000139 If *check_circular* is ``False`` (default: ``True``), then the circular
140 reference check for container types will be skipped and a circular reference
141 will result in an :exc:`OverflowError` (or worse).
142
143 If *allow_nan* is ``False`` (default: ``True``), then it will be a
144 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
145 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
146 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
147
Raymond Hettingerb643ef82010-10-31 08:00:16 +0000148 If *indent* is a non-negative integer or string, then JSON array elements and
149 object members will be pretty-printed with that indent level. An indent level
R David Murrayd5315482011-04-12 21:09:18 -0400150 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
151 selects the most compact representation. Using a positive integer indent
Petri Lehtinen72c6eef2012-08-27 20:27:30 +0300152 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
R David Murrayd5315482011-04-12 21:09:18 -0400153 that string is used to indent each level.
Christian Heimes90540002008-05-08 14:29:10 +0000154
Petri Lehtinen72b14262012-08-28 07:08:44 +0300155 .. versionchanged:: 3.2
156 Allow strings for *indent* in addition to integers.
157
Christian Heimes90540002008-05-08 14:29:10 +0000158 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
159 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
160 ':')`` is the most compact JSON representation.
161
Christian Heimes90540002008-05-08 14:29:10 +0000162 *default(obj)* is a function that should return a serializable version of
163 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
164
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200165 If *sort_keys* is ``True`` (default: ``False``), then the output of
166 dictionaries will be sorted by key.
167
Georg Brandl1f01deb2009-01-03 22:47:39 +0000168 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Christian Heimes90540002008-05-08 14:29:10 +0000169 :meth:`default` method to serialize additional types), specify it with the
Georg Brandld4460aa2010-10-15 17:03:02 +0000170 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000171
172
Andrew Svetlov2ec53be2012-10-28 14:10:30 +0200173.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
174 check_circular=True, allow_nan=True, cls=None, \
175 indent=None, separators=None, default=None, \
176 sort_keys=False, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000177
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000178 Serialize *obj* to a JSON formatted :class:`str`. The arguments have the
179 same meaning as in :func:`dump`.
Christian Heimes90540002008-05-08 14:29:10 +0000180
Ezio Melotti60adf952011-04-15 07:37:00 +0300181 .. note::
182
Georg Brandl340d2692011-04-16 16:54:15 +0200183 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol,
184 so trying to serialize multiple objects with repeated calls to
185 :func:`dump` using the same *fp* will result in an invalid JSON file.
186
Senthil Kumaranf2123d22012-03-17 00:40:34 -0700187 .. note::
188
189 Keys in key/value pairs of JSON are always of the type :class:`str`. When
190 a dictionary is converted into JSON, all the keys of the dictionary are
191 coerced to strings. As a result of this, if a dictionary is convered
192 into JSON and then back into a dictionary, the dictionary may not equal
193 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
194 keys.
Christian Heimes90540002008-05-08 14:29:10 +0000195
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000196.. 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 +0000197
Antoine Pitrou15251a92012-08-24 19:49:08 +0200198 Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
199 containing a JSON document) to a Python object.
Christian Heimes90540002008-05-08 14:29:10 +0000200
Christian Heimes90540002008-05-08 14:29:10 +0000201 *object_hook* is an optional function that will be called with the result of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000202 any object literal decoded (a :class:`dict`). The return value of
Christian Heimes90540002008-05-08 14:29:10 +0000203 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrou331624b2012-08-24 19:37:23 +0200204 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
205 class hinting).
Christian Heimes90540002008-05-08 14:29:10 +0000206
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000207 *object_pairs_hook* is an optional function that will be called with the
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000208 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000209 return value of *object_pairs_hook* will be used instead of the
210 :class:`dict`. This feature can be used to implement custom decoders that
211 rely on the order that the key and value pairs are decoded (for example,
212 :func:`collections.OrderedDict` will remember the order of insertion). If
213 *object_hook* is also defined, the *object_pairs_hook* takes priority.
214
215 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000216 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000217
Christian Heimes90540002008-05-08 14:29:10 +0000218 *parse_float*, if specified, will be called with the string of every JSON
219 float to be decoded. By default, this is equivalent to ``float(num_str)``.
220 This can be used to use another datatype or parser for JSON floats
221 (e.g. :class:`decimal.Decimal`).
222
223 *parse_int*, if specified, will be called with the string of every JSON int
224 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
225 be used to use another datatype or parser for JSON integers
226 (e.g. :class:`float`).
227
228 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack9729fd42012-05-16 19:01:04 +0200229 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
230 This can be used to raise an exception if invalid JSON numbers
Christian Heimes90540002008-05-08 14:29:10 +0000231 are encountered.
232
Hynek Schlawackf54c0602012-05-20 18:32:53 +0200233 .. versionchanged:: 3.1
Hynek Schlawack1203e832012-05-20 12:03:17 +0200234 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
235
Christian Heimes90540002008-05-08 14:29:10 +0000236 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandld4460aa2010-10-15 17:03:02 +0000237 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
238 will be passed to the constructor of the class.
Christian Heimes90540002008-05-08 14:29:10 +0000239
240
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000241.. 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 +0000242
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000243 Deserialize *s* (a :class:`str` instance containing a JSON document) to a
244 Python object.
Christian Heimes90540002008-05-08 14:29:10 +0000245
Antoine Pitrou00d650b2011-01-21 21:37:32 +0000246 The other arguments have the same meaning as in :func:`load`, except
247 *encoding* which is ignored and deprecated.
Christian Heimes90540002008-05-08 14:29:10 +0000248
249
Antoine Pitrou331624b2012-08-24 19:37:23 +0200250Encoders and Decoders
Christian Heimes90540002008-05-08 14:29:10 +0000251---------------------
252
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000253.. 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 +0000254
255 Simple JSON decoder.
256
257 Performs the following translations in decoding by default:
258
259 +---------------+-------------------+
260 | JSON | Python |
261 +===============+===================+
262 | object | dict |
263 +---------------+-------------------+
264 | array | list |
265 +---------------+-------------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000266 | string | str |
Christian Heimes90540002008-05-08 14:29:10 +0000267 +---------------+-------------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000268 | number (int) | int |
Christian Heimes90540002008-05-08 14:29:10 +0000269 +---------------+-------------------+
270 | number (real) | float |
271 +---------------+-------------------+
272 | true | True |
273 +---------------+-------------------+
274 | false | False |
275 +---------------+-------------------+
276 | null | None |
277 +---------------+-------------------+
278
279 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
280 corresponding ``float`` values, which is outside the JSON spec.
281
Christian Heimes90540002008-05-08 14:29:10 +0000282 *object_hook*, if specified, will be called with the result of every JSON
283 object decoded and its return value will be used in place of the given
284 :class:`dict`. This can be used to provide custom deserializations (e.g. to
285 support JSON-RPC class hinting).
286
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000287 *object_pairs_hook*, if specified will be called with the result of every
288 JSON object decoded with an ordered list of pairs. The return value of
289 *object_pairs_hook* will be used instead of the :class:`dict`. This
290 feature can be used to implement custom decoders that rely on the order
291 that the key and value pairs are decoded (for example,
292 :func:`collections.OrderedDict` will remember the order of insertion). If
293 *object_hook* is also defined, the *object_pairs_hook* takes priority.
294
295 .. versionchanged:: 3.1
Hirokazu Yamamotoae9eb5c2009-04-26 03:34:06 +0000296 Added support for *object_pairs_hook*.
Raymond Hettinger9b8d0692009-04-21 03:27:12 +0000297
Christian Heimes90540002008-05-08 14:29:10 +0000298 *parse_float*, if specified, will be called with the string of every JSON
299 float to be decoded. By default, this is equivalent to ``float(num_str)``.
300 This can be used to use another datatype or parser for JSON floats
301 (e.g. :class:`decimal.Decimal`).
302
303 *parse_int*, if specified, will be called with the string of every JSON int
304 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
305 be used to use another datatype or parser for JSON integers
306 (e.g. :class:`float`).
307
308 *parse_constant*, if specified, will be called with one of the following
309 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
310 ``'false'``. This can be used to raise an exception if invalid JSON numbers
311 are encountered.
312
Georg Brandld4460aa2010-10-15 17:03:02 +0000313 If *strict* is ``False`` (``True`` is the default), then control characters
314 will be allowed inside strings. Control characters in this context are
315 those with character codes in the 0-31 range, including ``'\t'`` (tab),
316 ``'\n'``, ``'\r'`` and ``'\0'``.
317
Christian Heimes90540002008-05-08 14:29:10 +0000318
319 .. method:: decode(s)
320
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000321 Return the Python representation of *s* (a :class:`str` instance
322 containing a JSON document)
Christian Heimes90540002008-05-08 14:29:10 +0000323
324 .. method:: raw_decode(s)
325
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000326 Decode a JSON document from *s* (a :class:`str` beginning with a
327 JSON document) and return a 2-tuple of the Python representation
328 and the index in *s* where the document ended.
Christian Heimes90540002008-05-08 14:29:10 +0000329
330 This can be used to decode a JSON document from a string that may have
331 extraneous data at the end.
332
333
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000334.. 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 +0000335
336 Extensible JSON encoder for Python data structures.
337
338 Supports the following objects and types by default:
339
340 +-------------------+---------------+
341 | Python | JSON |
342 +===================+===============+
343 | dict | object |
344 +-------------------+---------------+
345 | list, tuple | array |
346 +-------------------+---------------+
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000347 | str | string |
Christian Heimes90540002008-05-08 14:29:10 +0000348 +-------------------+---------------+
Georg Brandl639ce962009-04-11 18:18:16 +0000349 | int, float | number |
Christian Heimes90540002008-05-08 14:29:10 +0000350 +-------------------+---------------+
351 | True | true |
352 +-------------------+---------------+
353 | False | false |
354 +-------------------+---------------+
355 | None | null |
356 +-------------------+---------------+
357
358 To extend this to recognize other objects, subclass and implement a
359 :meth:`default` method with another method that returns a serializable object
360 for ``o`` if possible, otherwise it should call the superclass implementation
361 (to raise :exc:`TypeError`).
362
363 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
Georg Brandl639ce962009-04-11 18:18:16 +0000364 attempt encoding of keys that are not str, int, float or None. If
Christian Heimes90540002008-05-08 14:29:10 +0000365 *skipkeys* is ``True``, such items are simply skipped.
366
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000367 If *ensure_ascii* is ``True`` (the default), the output is guaranteed to
368 have all incoming non-ASCII characters escaped. If *ensure_ascii* is
369 ``False``, these characters will be output as-is.
Christian Heimes90540002008-05-08 14:29:10 +0000370
371 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
372 encoded objects will be checked for circular references during encoding to
373 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
374 Otherwise, no such check takes place.
375
376 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
377 ``-Infinity`` will be encoded as such. This behavior is not JSON
378 specification compliant, but is consistent with most JavaScript based
379 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
380 such floats.
381
Georg Brandl6a74da32010-08-22 20:23:38 +0000382 If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
Christian Heimes90540002008-05-08 14:29:10 +0000383 will be sorted by key; this is useful for regression tests to ensure that
384 JSON serializations can be compared on a day-to-day basis.
385
Petri Lehtinen72b14262012-08-28 07:08:44 +0300386 If *indent* is a non-negative integer or string, then JSON array elements and
387 object members will be pretty-printed with that indent level. An indent level
388 of 0, negative, or ``""`` will only insert newlines. ``None`` (the default)
389 selects the most compact representation. Using a positive integer indent
390 indents that many spaces per level. If *indent* is a string (such as ``"\t"``),
391 that string is used to indent each level.
392
393 .. versionchanged:: 3.2
394 Allow strings for *indent* in addition to integers.
Christian Heimes90540002008-05-08 14:29:10 +0000395
396 If specified, *separators* should be an ``(item_separator, key_separator)``
397 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
398 representation, you should specify ``(',', ':')`` to eliminate whitespace.
399
400 If specified, *default* is a function that gets called for objects that can't
401 otherwise be serialized. It should return a JSON encodable version of the
402 object or raise a :exc:`TypeError`.
403
Christian Heimes90540002008-05-08 14:29:10 +0000404
405 .. method:: default(o)
406
407 Implement this method in a subclass such that it returns a serializable
408 object for *o*, or calls the base implementation (to raise a
409 :exc:`TypeError`).
410
411 For example, to support arbitrary iterators, you could implement default
412 like this::
Georg Brandl48310cd2009-01-03 21:18:54 +0000413
Christian Heimes90540002008-05-08 14:29:10 +0000414 def default(self, o):
415 try:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000416 iterable = iter(o)
Christian Heimes90540002008-05-08 14:29:10 +0000417 except TypeError:
Benjamin Petersone9bbc8b2008-09-28 02:06:32 +0000418 pass
Christian Heimes90540002008-05-08 14:29:10 +0000419 else:
420 return list(iterable)
Georg Brandl0bb73b82010-09-03 22:36:22 +0000421 return json.JSONEncoder.default(self, o)
Christian Heimes90540002008-05-08 14:29:10 +0000422
423
424 .. method:: encode(o)
425
426 Return a JSON string representation of a Python data structure, *o*. For
427 example::
428
Georg Brandl0bb73b82010-09-03 22:36:22 +0000429 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
Christian Heimes90540002008-05-08 14:29:10 +0000430 '{"foo": ["bar", "baz"]}'
431
432
433 .. method:: iterencode(o)
434
435 Encode the given object, *o*, and yield each string representation as
436 available. For example::
Georg Brandl48310cd2009-01-03 21:18:54 +0000437
Georg Brandl0bb73b82010-09-03 22:36:22 +0000438 for chunk in json.JSONEncoder().iterencode(bigobject):
Christian Heimes90540002008-05-08 14:29:10 +0000439 mysocket.write(chunk)
Antoine Pitrou331624b2012-08-24 19:37:23 +0200440
441
442Standard Compliance
443-------------------
444
445The JSON format is specified by :rfc:`4627`. This section details this
446module's level of compliance with the RFC. For simplicity,
447:class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and parameters other
448than those explicitly mentioned, are not considered.
449
450This module does not comply with the RFC in a strict fashion, implementing some
451extensions that are valid JavaScript but not valid JSON. In particular:
452
453- Top-level non-object, non-array values are accepted and output;
454- Infinite and NaN number values are accepted and output;
455- Repeated names within an object are accepted, and only the value of the last
456 name-value pair is used.
457
458Since the RFC permits RFC-compliant parsers to accept input texts that are not
459RFC-compliant, this module's deserializer is technically RFC-compliant under
460default settings.
461
462Character Encodings
463^^^^^^^^^^^^^^^^^^^
464
465The RFC recommends that JSON be represented using either UTF-8, UTF-16, or
466UTF-32, with UTF-8 being the default.
467
468As permitted, though not required, by the RFC, this module's serializer sets
469*ensure_ascii=True* by default, thus escaping the output so that the resulting
470strings only contain ASCII characters.
471
472Other than the *ensure_ascii* parameter, this module is defined strictly in
473terms of conversion between Python objects and
474:class:`Unicode strings <str>`, and thus does not otherwise address the issue
475of character encodings.
476
477
478Top-level Non-Object, Non-Array Values
479^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
480
481The RFC specifies that the top-level value of a JSON text must be either a
482JSON object or array (Python :class:`dict` or :class:`list`). This module's
483deserializer also accepts input texts consisting solely of a
484JSON null, boolean, number, or string value::
485
486 >>> just_a_json_string = '"spam and eggs"' # Not by itself a valid JSON text
487 >>> json.loads(just_a_json_string)
488 'spam and eggs'
489
490This module itself does not include a way to request that such input texts be
491regarded as illegal. Likewise, this module's serializer also accepts single
492Python :data:`None`, :class:`bool`, numeric, and :class:`str`
493values as input and will generate output texts consisting solely of a top-level
494JSON null, boolean, number, or string value without raising an exception::
495
496 >>> neither_a_list_nor_a_dict = "spam and eggs"
497 >>> json.dumps(neither_a_list_nor_a_dict) # The result is not a valid JSON text
498 '"spam and eggs"'
499
500This module's serializer does not itself include a way to enforce the
501aforementioned constraint.
502
503
504Infinite and NaN Number Values
505^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
506
507The RFC does not permit the representation of infinite or NaN number values.
508Despite that, by default, this module accepts and outputs ``Infinity``,
509``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
510
511 >>> # Neither of these calls raises an exception, but the results are not valid JSON
512 >>> json.dumps(float('-inf'))
513 '-Infinity'
514 >>> json.dumps(float('nan'))
515 'NaN'
516 >>> # Same when deserializing
517 >>> json.loads('-Infinity')
518 -inf
519 >>> json.loads('NaN')
520 nan
521
522In the serializer, the *allow_nan* parameter can be used to alter this
523behavior. In the deserializer, the *parse_constant* parameter can be used to
524alter this behavior.
525
526
527Repeated Names Within an Object
528^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
529
530The RFC specifies that the names within a JSON object should be unique, but
531does not specify how repeated names in JSON objects should be handled. By
532default, this module does not raise an exception; instead, it ignores all but
533the last name-value pair for a given name::
534
535 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
536 >>> json.loads(weird_json)
537 {'x': 3}
538
539The *object_pairs_hook* parameter can be used to alter this behavior.