blob: c4d5ee6ec175be2f936f38a43d9538990c0b203f [file] [log] [blame]
Georg Brandl3961f182008-05-05 20:53:39 +00001:mod:`json` --- JSON encoder and decoder
2========================================
Brett Cannon4b964f92008-05-05 20:21:38 +00003
4.. module:: json
Georg Brandl3961f182008-05-05 20:53:39 +00005 :synopsis: Encode and decode the JSON format.
Brett Cannon4b964f92008-05-05 20:21:38 +00006.. moduleauthor:: Bob Ippolito <bob@redivi.com>
7.. sectionauthor:: Bob Ippolito <bob@redivi.com>
8.. versionadded:: 2.6
9
Antoine Pitrouf3e0a692012-08-24 19:46:17 +020010`JSON (JavaScript Object Notation) <http://json.org>`_, specified by
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +020011:rfc:`7159` (which obsoletes :rfc:`4627`) and by
12`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_,
13is a lightweight data interchange format inspired by
Georg Brandl6e0b44e2016-02-26 19:37:12 +010014`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +020015(although it is not a strict subset of JavaScript [#rfc-errata]_ ).
Brett Cannon4b964f92008-05-05 20:21:38 +000016
Georg Brandl3961f182008-05-05 20:53:39 +000017:mod:`json` exposes an API familiar to users of the standard library
18:mod:`marshal` and :mod:`pickle` modules.
Brett Cannon4b964f92008-05-05 20:21:38 +000019
20Encoding basic Python object hierarchies::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000021
Brett Cannon4b964f92008-05-05 20:21:38 +000022 >>> import json
23 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
24 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
25 >>> print json.dumps("\"foo\bar")
26 "\"foo\bar"
27 >>> print json.dumps(u'\u1234')
28 "\u1234"
29 >>> print json.dumps('\\')
30 "\\"
31 >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
32 {"a": 0, "b": 0, "c": 0}
33 >>> from StringIO import StringIO
34 >>> io = StringIO()
35 >>> json.dump(['streaming API'], io)
36 >>> io.getvalue()
37 '["streaming API"]'
38
39Compact encoding::
40
41 >>> import json
42 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
43 '[1,2,3,{"4":5,"6":7}]'
44
45Pretty printing::
46
47 >>> import json
Ezio Melotti3a237eb2012-11-29 00:22:30 +020048 >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,
49 ... indent=4, separators=(',', ': '))
Brett Cannon4b964f92008-05-05 20:21:38 +000050 {
Georg Brandlc62ef8b2009-01-03 20:55:06 +000051 "4": 5,
Brett Cannon4b964f92008-05-05 20:21:38 +000052 "6": 7
53 }
54
55Decoding JSON::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000056
Brett Cannon4b964f92008-05-05 20:21:38 +000057 >>> import json
58 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
59 [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
60 >>> json.loads('"\\"foo\\bar"')
61 u'"foo\x08ar'
62 >>> from StringIO import StringIO
63 >>> io = StringIO('["streaming API"]')
64 >>> json.load(io)
65 [u'streaming API']
66
67Specializing JSON object decoding::
68
69 >>> import json
70 >>> def as_complex(dct):
71 ... if '__complex__' in dct:
72 ... return complex(dct['real'], dct['imag'])
73 ... return dct
Georg Brandlc62ef8b2009-01-03 20:55:06 +000074 ...
Brett Cannon4b964f92008-05-05 20:21:38 +000075 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
76 ... object_hook=as_complex)
77 (1+2j)
78 >>> import decimal
79 >>> json.loads('1.1', parse_float=decimal.Decimal)
80 Decimal('1.1')
81
Georg Brandl3961f182008-05-05 20:53:39 +000082Extending :class:`JSONEncoder`::
Georg Brandlc62ef8b2009-01-03 20:55:06 +000083
Brett Cannon4b964f92008-05-05 20:21:38 +000084 >>> import json
85 >>> class ComplexEncoder(json.JSONEncoder):
86 ... def default(self, obj):
87 ... if isinstance(obj, complex):
88 ... return [obj.real, obj.imag]
R David Murray35893b72013-03-17 22:06:18 -040089 ... # Let the base class default method raise the TypeError
Brett Cannon4b964f92008-05-05 20:21:38 +000090 ... return json.JSONEncoder.default(self, obj)
Georg Brandlc62ef8b2009-01-03 20:55:06 +000091 ...
Brett Cannon4b964f92008-05-05 20:21:38 +000092 >>> dumps(2 + 1j, cls=ComplexEncoder)
93 '[2.0, 1.0]'
94 >>> ComplexEncoder().encode(2 + 1j)
95 '[2.0, 1.0]'
96 >>> list(ComplexEncoder().iterencode(2 + 1j))
97 ['[', '2.0', ', ', '1.0', ']']
Georg Brandlc62ef8b2009-01-03 20:55:06 +000098
Brett Cannon4b964f92008-05-05 20:21:38 +000099
100.. highlight:: none
101
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300102Using :mod:`json.tool` from the shell to validate and pretty-print::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000103
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300104 $ echo '{"json":"obj"}' | python -m json.tool
Brett Cannon4b964f92008-05-05 20:21:38 +0000105 {
106 "json": "obj"
107 }
Antoine Pitroud9a51372012-06-29 01:58:26 +0200108 $ echo '{1.2:3.4}' | python -mjson.tool
Serhiy Storchaka49d40222013-02-21 20:17:54 +0200109 Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Brett Cannon4b964f92008-05-05 20:21:38 +0000110
111.. highlight:: python
112
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000113.. note::
Brett Cannon4b964f92008-05-05 20:21:38 +0000114
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200115 JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by
116 this module's default settings (in particular, the default *separators*
117 value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
118 used as a YAML serializer.
Brett Cannon4b964f92008-05-05 20:21:38 +0000119
120
121Basic Usage
122-----------
123
Andrew Svetlov41c25ba2012-10-28 14:58:52 +0200124.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
125 check_circular=True, allow_nan=True, cls=None, \
126 indent=None, separators=None, encoding="utf-8", \
127 default=None, sort_keys=False, **kw)
Brett Cannon4b964f92008-05-05 20:21:38 +0000128
Georg Brandl3961f182008-05-05 20:53:39 +0000129 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
Ezio Melottid5cdc942013-03-29 03:59:29 +0200130 :term:`file-like object`) using this :ref:`conversion table
131 <py-to-json-table>`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000132
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300133 If *skipkeys* is true (default: ``False``), then dict keys that are not
Georg Brandl3961f182008-05-05 20:53:39 +0000134 of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`,
135 :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
136 :exc:`TypeError`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000137
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300138 If *ensure_ascii* is true (the default), all non-ASCII characters in the
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300139 output are escaped with ``\uXXXX`` sequences, and the result is a
140 :class:`str` instance consisting of ASCII characters only. If
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300141 *ensure_ascii* is false, some chunks written to *fp* may be
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300142 :class:`unicode` instances. This usually happens because the input contains
143 unicode strings or the *encoding* parameter is used. Unless ``fp.write()``
144 explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`)
145 this is likely to cause an error.
Brett Cannon4b964f92008-05-05 20:21:38 +0000146
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300147 If *check_circular* is false (default: ``True``), then the circular
Georg Brandl3961f182008-05-05 20:53:39 +0000148 reference check for container types will be skipped and a circular reference
149 will result in an :exc:`OverflowError` (or worse).
Brett Cannon4b964f92008-05-05 20:21:38 +0000150
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300151 If *allow_nan* is false (default: ``True``), then it will be a
Georg Brandl3961f182008-05-05 20:53:39 +0000152 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300153 ``inf``, ``-inf``) in strict compliance of the JSON specification.
154 If *allow_nan* is true, their JavaScript equivalents (``NaN``,
155 ``Infinity``, ``-Infinity``) will be used.
Brett Cannon4b964f92008-05-05 20:21:38 +0000156
Georg Brandl3961f182008-05-05 20:53:39 +0000157 If *indent* is a non-negative integer, then JSON array elements and object
R David Murrayea8b6ef2011-04-12 21:00:26 -0400158 members will be pretty-printed with that indent level. An indent level of 0,
159 or negative, will only insert newlines. ``None`` (the default) selects the
160 most compact representation.
Brett Cannon4b964f92008-05-05 20:21:38 +0000161
Ezio Melotti3a237eb2012-11-29 00:22:30 +0200162 .. note::
163
164 Since the default item separator is ``', '``, the output might include
165 trailing whitespace when *indent* is specified. You can use
166 ``separators=(',', ': ')`` to avoid this.
167
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300168 If specified, *separators* should be an ``(item_separator, key_separator)``
169 tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON
170 representation, you should specify ``(',', ':')`` to eliminate whitespace.
Brett Cannon4b964f92008-05-05 20:21:38 +0000171
Georg Brandl3961f182008-05-05 20:53:39 +0000172 *encoding* is the character encoding for str instances, default is UTF-8.
Brett Cannon4b964f92008-05-05 20:21:38 +0000173
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300174 If specified, *default* should be a function that gets called for objects that
175 can't otherwise be serialized. It should return a JSON encodable version of
176 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
177 is raised.
Brett Cannon4b964f92008-05-05 20:21:38 +0000178
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300179 If *sort_keys* is true (default: ``False``), then the output of
Andrew Svetlov41c25ba2012-10-28 14:58:52 +0200180 dictionaries will be sorted by key.
181
Georg Brandlfc29f272009-01-02 20:25:14 +0000182 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Georg Brandl3961f182008-05-05 20:53:39 +0000183 :meth:`default` method to serialize additional types), specify it with the
Georg Brandldb949b82010-10-15 17:04:45 +0000184 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Brett Cannon4b964f92008-05-05 20:21:38 +0000185
Ezio Melotti6033d262011-04-15 07:37:00 +0300186 .. note::
187
188 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol so
189 trying to serialize more objects with repeated calls to :func:`dump` and
190 the same *fp* will result in an invalid JSON file.
Brett Cannon4b964f92008-05-05 20:21:38 +0000191
Andrew Svetlov41c25ba2012-10-28 14:58:52 +0200192.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
193 check_circular=True, allow_nan=True, cls=None, \
194 indent=None, separators=None, encoding="utf-8", \
195 default=None, sort_keys=False, **kw)
Brett Cannon4b964f92008-05-05 20:21:38 +0000196
Ezio Melottid5cdc942013-03-29 03:59:29 +0200197 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300198 table <py-to-json-table>`. If *ensure_ascii* is false, the result may
Ezio Melottid5cdc942013-03-29 03:59:29 +0200199 contain non-ASCII characters and the return value may be a :class:`unicode`
200 instance.
Brett Cannon4b964f92008-05-05 20:21:38 +0000201
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300202 The arguments have the same meaning as in :func:`dump`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000203
Senthil Kumarane3d73542012-03-17 00:37:38 -0700204 .. note::
205
206 Keys in key/value pairs of JSON are always of the type :class:`str`. When
207 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy3d08f252013-03-08 19:35:15 -0500208 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumarane3d73542012-03-17 00:37:38 -0700209 into JSON and then back into a dictionary, the dictionary may not equal
210 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
211 keys.
Brett Cannon4b964f92008-05-05 20:21:38 +0000212
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000213.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000214
Antoine Pitrou85ede8d2012-08-24 19:49:08 +0200215 Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
Ezio Melottid5cdc942013-03-29 03:59:29 +0200216 containing a JSON document) to a Python object using this :ref:`conversion
217 table <json-to-py-table>`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000218
Georg Brandl3961f182008-05-05 20:53:39 +0000219 If the contents of *fp* are encoded with an ASCII based encoding other than
220 UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
221 Encodings that are not ASCII based (such as UCS-2) are not allowed, and
Georg Brandl49cc4ea2009-04-23 08:44:57 +0000222 should be wrapped with ``codecs.getreader(encoding)(fp)``, or simply decoded
Georg Brandl3961f182008-05-05 20:53:39 +0000223 to a :class:`unicode` object and passed to :func:`loads`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000224
225 *object_hook* is an optional function that will be called with the result of
Andrew M. Kuchling19672002009-03-30 22:29:15 +0000226 any object literal decoded (a :class:`dict`). The return value of
Georg Brandl3961f182008-05-05 20:53:39 +0000227 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200228 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
229 class hinting).
Georg Brandl3961f182008-05-05 20:53:39 +0000230
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000231 *object_pairs_hook* is an optional function that will be called with the
Andrew M. Kuchling19672002009-03-30 22:29:15 +0000232 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000233 return value of *object_pairs_hook* will be used instead of the
234 :class:`dict`. This feature can be used to implement custom decoders that
235 rely on the order that the key and value pairs are decoded (for example,
236 :func:`collections.OrderedDict` will remember the order of insertion). If
237 *object_hook* is also defined, the *object_pairs_hook* takes priority.
238
239 .. versionchanged:: 2.7
240 Added support for *object_pairs_hook*.
241
Georg Brandl3961f182008-05-05 20:53:39 +0000242 *parse_float*, if specified, will be called with the string of every JSON
243 float to be decoded. By default, this is equivalent to ``float(num_str)``.
244 This can be used to use another datatype or parser for JSON floats
245 (e.g. :class:`decimal.Decimal`).
246
247 *parse_int*, if specified, will be called with the string of every JSON int
248 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
249 be used to use another datatype or parser for JSON integers
250 (e.g. :class:`float`).
251
252 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack019935f2012-05-16 18:02:54 +0200253 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
254 This can be used to raise an exception if invalid JSON numbers
Georg Brandl3961f182008-05-05 20:53:39 +0000255 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000256
Hynek Schlawack897b2782012-05-20 11:50:41 +0200257 .. versionchanged:: 2.7
258 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
259
Brett Cannon4b964f92008-05-05 20:21:38 +0000260 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandldb949b82010-10-15 17:04:45 +0000261 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
262 will be passed to the constructor of the class.
Brett Cannon4b964f92008-05-05 20:21:38 +0000263
264
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000265.. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Georg Brandl3961f182008-05-05 20:53:39 +0000266
267 Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
Ezio Melottid5cdc942013-03-29 03:59:29 +0200268 document) to a Python object using this :ref:`conversion table
269 <json-to-py-table>`.
Georg Brandl3961f182008-05-05 20:53:39 +0000270
271 If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
272 other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
273 specified. Encodings that are not ASCII based (such as UCS-2) are not
274 allowed and should be decoded to :class:`unicode` first.
275
Georg Brandlc6301952010-05-10 21:02:51 +0000276 The other arguments have the same meaning as in :func:`load`.
Georg Brandl3961f182008-05-05 20:53:39 +0000277
278
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200279Encoders and Decoders
Brett Cannon4b964f92008-05-05 20:21:38 +0000280---------------------
281
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000282.. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict[, object_pairs_hook]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000283
Georg Brandl3961f182008-05-05 20:53:39 +0000284 Simple JSON decoder.
Brett Cannon4b964f92008-05-05 20:21:38 +0000285
286 Performs the following translations in decoding by default:
287
Ezio Melottid5cdc942013-03-29 03:59:29 +0200288 .. _json-to-py-table:
289
Brett Cannon4b964f92008-05-05 20:21:38 +0000290 +---------------+-------------------+
291 | JSON | Python |
292 +===============+===================+
293 | object | dict |
294 +---------------+-------------------+
295 | array | list |
296 +---------------+-------------------+
297 | string | unicode |
298 +---------------+-------------------+
299 | number (int) | int, long |
300 +---------------+-------------------+
301 | number (real) | float |
302 +---------------+-------------------+
303 | true | True |
304 +---------------+-------------------+
305 | false | False |
306 +---------------+-------------------+
307 | null | None |
308 +---------------+-------------------+
309
310 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
311 corresponding ``float`` values, which is outside the JSON spec.
312
Georg Brandl3961f182008-05-05 20:53:39 +0000313 *encoding* determines the encoding used to interpret any :class:`str` objects
314 decoded by this instance (UTF-8 by default). It has no effect when decoding
315 :class:`unicode` objects.
Brett Cannon4b964f92008-05-05 20:21:38 +0000316
Georg Brandl3961f182008-05-05 20:53:39 +0000317 Note that currently only encodings that are a superset of ASCII work, strings
318 of other encodings should be passed in as :class:`unicode`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000319
320 *object_hook*, if specified, will be called with the result of every JSON
321 object decoded and its return value will be used in place of the given
Georg Brandl3961f182008-05-05 20:53:39 +0000322 :class:`dict`. This can be used to provide custom deserializations (e.g. to
Brett Cannon4b964f92008-05-05 20:21:38 +0000323 support JSON-RPC class hinting).
324
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000325 *object_pairs_hook*, if specified will be called with the result of every
326 JSON object decoded with an ordered list of pairs. The return value of
327 *object_pairs_hook* will be used instead of the :class:`dict`. This
328 feature can be used to implement custom decoders that rely on the order
329 that the key and value pairs are decoded (for example,
330 :func:`collections.OrderedDict` will remember the order of insertion). If
331 *object_hook* is also defined, the *object_pairs_hook* takes priority.
332
333 .. versionchanged:: 2.7
334 Added support for *object_pairs_hook*.
335
Brett Cannon4b964f92008-05-05 20:21:38 +0000336 *parse_float*, if specified, will be called with the string of every JSON
Georg Brandl3961f182008-05-05 20:53:39 +0000337 float to be decoded. By default, this is equivalent to ``float(num_str)``.
338 This can be used to use another datatype or parser for JSON floats
339 (e.g. :class:`decimal.Decimal`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000340
341 *parse_int*, if specified, will be called with the string of every JSON int
Georg Brandl3961f182008-05-05 20:53:39 +0000342 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
343 be used to use another datatype or parser for JSON integers
344 (e.g. :class:`float`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000345
346 *parse_constant*, if specified, will be called with one of the following
Serhiy Storchaka58b6b982016-11-12 22:47:16 +0200347 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
348 This can be used to raise an exception if invalid JSON numbers
Georg Brandl3961f182008-05-05 20:53:39 +0000349 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000350
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300351 If *strict* is false (``True`` is the default), then control characters
Georg Brandldb949b82010-10-15 17:04:45 +0000352 will be allowed inside strings. Control characters in this context are
Serhiy Storchaka0092bc72016-11-26 13:43:39 +0200353 those with character codes in the 0--31 range, including ``'\t'`` (tab),
Georg Brandldb949b82010-10-15 17:04:45 +0000354 ``'\n'``, ``'\r'`` and ``'\0'``.
355
Felix Cruxb4e186c2013-08-12 17:39:51 -0400356 If the data being deserialized is not a valid JSON document, a
357 :exc:`ValueError` will be raised.
Brett Cannon4b964f92008-05-05 20:21:38 +0000358
359 .. method:: decode(s)
360
Georg Brandl3961f182008-05-05 20:53:39 +0000361 Return the Python representation of *s* (a :class:`str` or
Martin Panter4ed35fc2015-10-10 10:52:35 +0000362 :class:`unicode` instance containing a JSON document).
Brett Cannon4b964f92008-05-05 20:21:38 +0000363
364 .. method:: raw_decode(s)
365
Georg Brandl3961f182008-05-05 20:53:39 +0000366 Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
367 beginning with a JSON document) and return a 2-tuple of the Python
368 representation and the index in *s* where the document ended.
Brett Cannon4b964f92008-05-05 20:21:38 +0000369
Georg Brandl3961f182008-05-05 20:53:39 +0000370 This can be used to decode a JSON document from a string that may have
371 extraneous data at the end.
Brett Cannon4b964f92008-05-05 20:21:38 +0000372
373
374.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
375
Georg Brandl3961f182008-05-05 20:53:39 +0000376 Extensible JSON encoder for Python data structures.
Brett Cannon4b964f92008-05-05 20:21:38 +0000377
378 Supports the following objects and types by default:
379
Ezio Melottid5cdc942013-03-29 03:59:29 +0200380 .. _py-to-json-table:
381
Brett Cannon4b964f92008-05-05 20:21:38 +0000382 +-------------------+---------------+
383 | Python | JSON |
384 +===================+===============+
385 | dict | object |
386 +-------------------+---------------+
387 | list, tuple | array |
388 +-------------------+---------------+
389 | str, unicode | string |
390 +-------------------+---------------+
391 | int, long, float | number |
392 +-------------------+---------------+
393 | True | true |
394 +-------------------+---------------+
395 | False | false |
396 +-------------------+---------------+
397 | None | null |
398 +-------------------+---------------+
399
400 To extend this to recognize other objects, subclass and implement a
Georg Brandl3961f182008-05-05 20:53:39 +0000401 :meth:`default` method with another method that returns a serializable object
Brett Cannon4b964f92008-05-05 20:21:38 +0000402 for ``o`` if possible, otherwise it should call the superclass implementation
403 (to raise :exc:`TypeError`).
404
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300405 If *skipkeys* is false (the default), then it is a :exc:`TypeError` to
Serhiy Storchakaad13f332016-10-19 16:29:10 +0300406 attempt encoding of keys that are not str, int, long, float or ``None``. If
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300407 *skipkeys* is true, such items are simply skipped.
Brett Cannon4b964f92008-05-05 20:21:38 +0000408
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300409 If *ensure_ascii* is true (the default), all non-ASCII characters in the
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300410 output are escaped with ``\uXXXX`` sequences, and the results are
411 :class:`str` instances consisting of ASCII characters only. If
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300412 *ensure_ascii* is false, a result may be a :class:`unicode`
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300413 instance. This usually happens if the input contains unicode strings or the
414 *encoding* parameter is used.
Brett Cannon4b964f92008-05-05 20:21:38 +0000415
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300416 If *check_circular* is true (the default), then lists, dicts, and custom
Brett Cannon4b964f92008-05-05 20:21:38 +0000417 encoded objects will be checked for circular references during encoding to
418 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
419 Otherwise, no such check takes place.
420
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300421 If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and
Georg Brandl3961f182008-05-05 20:53:39 +0000422 ``-Infinity`` will be encoded as such. This behavior is not JSON
423 specification compliant, but is consistent with most JavaScript based
424 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
425 such floats.
Brett Cannon4b964f92008-05-05 20:21:38 +0000426
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300427 If *sort_keys* is true (default: ``False``), then the output of dictionaries
Brett Cannon4b964f92008-05-05 20:21:38 +0000428 will be sorted by key; this is useful for regression tests to ensure that
429 JSON serializations can be compared on a day-to-day basis.
430
Georg Brandl3961f182008-05-05 20:53:39 +0000431 If *indent* is a non-negative integer (it is ``None`` by default), then JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000432 array elements and object members will be pretty-printed with that indent
433 level. An indent level of 0 will only insert newlines. ``None`` is the most
434 compact representation.
435
Ezio Melotti3a237eb2012-11-29 00:22:30 +0200436 .. note::
437
438 Since the default item separator is ``', '``, the output might include
439 trailing whitespace when *indent* is specified. You can use
440 ``separators=(',', ': ')`` to avoid this.
441
Georg Brandl3961f182008-05-05 20:53:39 +0000442 If specified, *separators* should be an ``(item_separator, key_separator)``
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300443 tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000444 representation, you should specify ``(',', ':')`` to eliminate whitespace.
445
Serhiy Storchaka3626e5e2016-06-30 13:58:58 +0300446 If specified, *default* should be a function that gets called for objects that
447 can't otherwise be serialized. It should return a JSON encodable version of
448 the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError`
449 is raised.
Brett Cannon4b964f92008-05-05 20:21:38 +0000450
451 If *encoding* is not ``None``, then all input strings will be transformed
452 into unicode using that encoding prior to JSON-encoding. The default is
453 UTF-8.
454
455
456 .. method:: default(o)
457
458 Implement this method in a subclass such that it returns a serializable
459 object for *o*, or calls the base implementation (to raise a
460 :exc:`TypeError`).
461
462 For example, to support arbitrary iterators, you could implement default
463 like this::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000464
Brett Cannon4b964f92008-05-05 20:21:38 +0000465 def default(self, o):
466 try:
Georg Brandl1379ae02008-09-24 09:47:55 +0000467 iterable = iter(o)
Brett Cannon4b964f92008-05-05 20:21:38 +0000468 except TypeError:
Georg Brandl1379ae02008-09-24 09:47:55 +0000469 pass
Brett Cannon4b964f92008-05-05 20:21:38 +0000470 else:
471 return list(iterable)
R David Murray35893b72013-03-17 22:06:18 -0400472 # Let the base class default method raise the TypeError
Brett Cannon4b964f92008-05-05 20:21:38 +0000473 return JSONEncoder.default(self, o)
474
475
476 .. method:: encode(o)
477
Georg Brandl3961f182008-05-05 20:53:39 +0000478 Return a JSON string representation of a Python data structure, *o*. For
Brett Cannon4b964f92008-05-05 20:21:38 +0000479 example::
480
481 >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
482 '{"foo": ["bar", "baz"]}'
483
484
485 .. method:: iterencode(o)
486
487 Encode the given object, *o*, and yield each string representation as
Georg Brandl3961f182008-05-05 20:53:39 +0000488 available. For example::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000489
Brett Cannon4b964f92008-05-05 20:21:38 +0000490 for chunk in JSONEncoder().iterencode(bigobject):
491 mysocket.write(chunk)
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200492
493
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200494Standard Compliance and Interoperability
495----------------------------------------
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200496
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200497The JSON format is specified by :rfc:`7159` and by
498`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
499This section details this module's level of compliance with the RFC.
500For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
501parameters other than those explicitly mentioned, are not considered.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200502
503This module does not comply with the RFC in a strict fashion, implementing some
504extensions that are valid JavaScript but not valid JSON. In particular:
505
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200506- Infinite and NaN number values are accepted and output;
507- Repeated names within an object are accepted, and only the value of the last
508 name-value pair is used.
509
510Since the RFC permits RFC-compliant parsers to accept input texts that are not
511RFC-compliant, this module's deserializer is technically RFC-compliant under
512default settings.
513
514Character Encodings
515^^^^^^^^^^^^^^^^^^^
516
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200517The RFC requires that JSON be represented using either UTF-8, UTF-16, or
518UTF-32, with UTF-8 being the recommended default for maximum interoperability.
519Accordingly, this module uses UTF-8 as the default for its *encoding* parameter.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200520
521This module's deserializer only directly works with ASCII-compatible encodings;
522UTF-16, UTF-32, and other ASCII-incompatible encodings require the use of
523workarounds described in the documentation for the deserializer's *encoding*
524parameter.
525
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200526As permitted, though not required, by the RFC, this module's serializer sets
527*ensure_ascii=True* by default, thus escaping the output so that the resulting
528strings only contain ASCII characters.
529
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200530The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
531and this module's serializer does not add a BOM to its output.
532The RFC permits, but does not require, JSON deserializers to ignore an initial
533BOM in their input. This module's deserializer raises a :exc:`ValueError`
534when an initial BOM is present.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200535
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200536The RFC does not explicitly forbid JSON strings which contain byte sequences
537that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
538surrogates), but it does note that they may cause interoperability problems.
539By default, this module accepts and outputs (when present in the original
Georg Brandla44ec3f2015-01-14 08:26:30 +0100540:class:`str`) code points for such sequences.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200541
542
543Infinite and NaN Number Values
544^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
545
546The RFC does not permit the representation of infinite or NaN number values.
547Despite that, by default, this module accepts and outputs ``Infinity``,
548``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
549
550 >>> # Neither of these calls raises an exception, but the results are not valid JSON
551 >>> json.dumps(float('-inf'))
552 '-Infinity'
553 >>> json.dumps(float('nan'))
554 'NaN'
555 >>> # Same when deserializing
556 >>> json.loads('-Infinity')
557 -inf
558 >>> json.loads('NaN')
559 nan
560
561In the serializer, the *allow_nan* parameter can be used to alter this
562behavior. In the deserializer, the *parse_constant* parameter can be used to
563alter this behavior.
564
565
566Repeated Names Within an Object
567^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
568
569The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200570does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200571default, this module does not raise an exception; instead, it ignores all but
572the last name-value pair for a given name::
573
574 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
575 >>> json.loads(weird_json)
576 {u'x': 3}
577
578The *object_pairs_hook* parameter can be used to alter this behavior.
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200579
580
581Top-level Non-Object, Non-Array Values
582^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
583
584The old version of JSON specified by the obsolete :rfc:`4627` required that
585the top-level value of a JSON text must be either a JSON object or array
586(Python :class:`dict` or :class:`list`), and could not be a JSON null,
587boolean, number, or string value. :rfc:`7159` removed that restriction, and
588this module does not and has never implemented that restriction in either its
589serializer or its deserializer.
590
591Regardless, for maximum interoperability, you may wish to voluntarily adhere
592to the restriction yourself.
593
594
595Implementation Limitations
596^^^^^^^^^^^^^^^^^^^^^^^^^^
597
598Some JSON deserializer implementations may set limits on:
599
600* the size of accepted JSON texts
601* the maximum level of nesting of JSON objects and arrays
602* the range and precision of JSON numbers
603* the content and maximum length of JSON strings
604
605This module does not impose any such limits beyond those of the relevant
606Python datatypes themselves or the Python interpreter itself.
607
608When serializing to JSON, beware any such limitations in applications that may
609consume your JSON. In particular, it is common for JSON numbers to be
610deserialized into IEEE 754 double precision numbers and thus subject to that
611representation's range and precision limitations. This is especially relevant
612when serializing Python :class:`int` values of extremely large magnitude, or
613when serializing instances of "exotic" numerical types such as
614:class:`decimal.Decimal`.
615
616
617.. rubric:: Footnotes
618
619.. [#rfc-errata] As noted in `the errata for RFC 7159
Serhiy Storchakab4905ef2016-05-07 10:50:12 +0300620 <https://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200621 JSON permits literal U+2028 (LINE SEPARATOR) and
622 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
623 (as of ECMAScript Edition 5.1) does not.