blob: db9df0a247c073fc3486bf07f7fee45fa8517fe3 [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
14`JavaScript <http://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
15(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
102Using json.tool from the shell to validate and pretty-print::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000103
Brett Cannon4b964f92008-05-05 20:21:38 +0000104 $ echo '{"json":"obj"}' | python -mjson.tool
105 {
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
Georg Brandl3961f182008-05-05 20:53:39 +0000133 If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
134 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
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300138 If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
139 output are escaped with ``\uXXXX`` sequences, and the result is a
140 :class:`str` instance consisting of ASCII characters only. If
141 *ensure_ascii* is ``False``, some chunks written to *fp* may be
142 :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
Georg Brandl3961f182008-05-05 20:53:39 +0000147 If *check_circular* is ``False`` (default: ``True``), then the circular
148 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
Georg Brandl3961f182008-05-05 20:53:39 +0000151 If *allow_nan* is ``False`` (default: ``True``), then it will be a
152 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
153 ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
154 using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
Brett Cannon4b964f92008-05-05 20:21:38 +0000155
Georg Brandl3961f182008-05-05 20:53:39 +0000156 If *indent* is a non-negative integer, then JSON array elements and object
R David Murrayea8b6ef2011-04-12 21:00:26 -0400157 members will be pretty-printed with that indent level. An indent level of 0,
158 or negative, will only insert newlines. ``None`` (the default) selects the
159 most compact representation.
Brett Cannon4b964f92008-05-05 20:21:38 +0000160
Ezio Melotti3a237eb2012-11-29 00:22:30 +0200161 .. note::
162
163 Since the default item separator is ``', '``, the output might include
164 trailing whitespace when *indent* is specified. You can use
165 ``separators=(',', ': ')`` to avoid this.
166
Georg Brandl3961f182008-05-05 20:53:39 +0000167 If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
168 will be used instead of the default ``(', ', ': ')`` separators. ``(',',
169 ':')`` is the most compact JSON representation.
Brett Cannon4b964f92008-05-05 20:21:38 +0000170
Georg Brandl3961f182008-05-05 20:53:39 +0000171 *encoding* is the character encoding for str instances, default is UTF-8.
Brett Cannon4b964f92008-05-05 20:21:38 +0000172
Georg Brandl3961f182008-05-05 20:53:39 +0000173 *default(obj)* is a function that should return a serializable version of
174 *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000175
Andrew Svetlov41c25ba2012-10-28 14:58:52 +0200176 If *sort_keys* is ``True`` (default: ``False``), then the output of
177 dictionaries will be sorted by key.
178
Georg Brandlfc29f272009-01-02 20:25:14 +0000179 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
Georg Brandl3961f182008-05-05 20:53:39 +0000180 :meth:`default` method to serialize additional types), specify it with the
Georg Brandldb949b82010-10-15 17:04:45 +0000181 *cls* kwarg; otherwise :class:`JSONEncoder` is used.
Brett Cannon4b964f92008-05-05 20:21:38 +0000182
Ezio Melotti6033d262011-04-15 07:37:00 +0300183 .. note::
184
185 Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol so
186 trying to serialize more objects with repeated calls to :func:`dump` and
187 the same *fp* will result in an invalid JSON file.
Brett Cannon4b964f92008-05-05 20:21:38 +0000188
Andrew Svetlov41c25ba2012-10-28 14:58:52 +0200189.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
190 check_circular=True, allow_nan=True, cls=None, \
191 indent=None, separators=None, encoding="utf-8", \
192 default=None, sort_keys=False, **kw)
Brett Cannon4b964f92008-05-05 20:21:38 +0000193
Ezio Melottid5cdc942013-03-29 03:59:29 +0200194 Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion
195 table <py-to-json-table>`. If *ensure_ascii* is ``False``, the result may
196 contain non-ASCII characters and the return value may be a :class:`unicode`
197 instance.
Brett Cannon4b964f92008-05-05 20:21:38 +0000198
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300199 The arguments have the same meaning as in :func:`dump`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000200
Senthil Kumarane3d73542012-03-17 00:37:38 -0700201 .. note::
202
203 Keys in key/value pairs of JSON are always of the type :class:`str`. When
204 a dictionary is converted into JSON, all the keys of the dictionary are
Terry Jan Reedy3d08f252013-03-08 19:35:15 -0500205 coerced to strings. As a result of this, if a dictionary is converted
Senthil Kumarane3d73542012-03-17 00:37:38 -0700206 into JSON and then back into a dictionary, the dictionary may not equal
207 the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
208 keys.
Brett Cannon4b964f92008-05-05 20:21:38 +0000209
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000210.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000211
Antoine Pitrou85ede8d2012-08-24 19:49:08 +0200212 Deserialize *fp* (a ``.read()``-supporting :term:`file-like object`
Ezio Melottid5cdc942013-03-29 03:59:29 +0200213 containing a JSON document) to a Python object using this :ref:`conversion
214 table <json-to-py-table>`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000215
Georg Brandl3961f182008-05-05 20:53:39 +0000216 If the contents of *fp* are encoded with an ASCII based encoding other than
217 UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
218 Encodings that are not ASCII based (such as UCS-2) are not allowed, and
Georg Brandl49cc4ea2009-04-23 08:44:57 +0000219 should be wrapped with ``codecs.getreader(encoding)(fp)``, or simply decoded
Georg Brandl3961f182008-05-05 20:53:39 +0000220 to a :class:`unicode` object and passed to :func:`loads`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000221
222 *object_hook* is an optional function that will be called with the result of
Andrew M. Kuchling19672002009-03-30 22:29:15 +0000223 any object literal decoded (a :class:`dict`). The return value of
Georg Brandl3961f182008-05-05 20:53:39 +0000224 *object_hook* will be used instead of the :class:`dict`. This feature can be used
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200225 to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_
226 class hinting).
Georg Brandl3961f182008-05-05 20:53:39 +0000227
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000228 *object_pairs_hook* is an optional function that will be called with the
Andrew M. Kuchling19672002009-03-30 22:29:15 +0000229 result of any object literal decoded with an ordered list of pairs. The
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000230 return value of *object_pairs_hook* will be used instead of the
231 :class:`dict`. This feature can be used to implement custom decoders that
232 rely on the order that the key and value pairs are decoded (for example,
233 :func:`collections.OrderedDict` will remember the order of insertion). If
234 *object_hook* is also defined, the *object_pairs_hook* takes priority.
235
236 .. versionchanged:: 2.7
237 Added support for *object_pairs_hook*.
238
Georg Brandl3961f182008-05-05 20:53:39 +0000239 *parse_float*, if specified, will be called with the string of every JSON
240 float to be decoded. By default, this is equivalent to ``float(num_str)``.
241 This can be used to use another datatype or parser for JSON floats
242 (e.g. :class:`decimal.Decimal`).
243
244 *parse_int*, if specified, will be called with the string of every JSON int
245 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
246 be used to use another datatype or parser for JSON integers
247 (e.g. :class:`float`).
248
249 *parse_constant*, if specified, will be called with one of the following
Hynek Schlawack019935f2012-05-16 18:02:54 +0200250 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``.
251 This can be used to raise an exception if invalid JSON numbers
Georg Brandl3961f182008-05-05 20:53:39 +0000252 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000253
Hynek Schlawack897b2782012-05-20 11:50:41 +0200254 .. versionchanged:: 2.7
255 *parse_constant* doesn't get called on 'null', 'true', 'false' anymore.
256
Brett Cannon4b964f92008-05-05 20:21:38 +0000257 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
Georg Brandldb949b82010-10-15 17:04:45 +0000258 kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments
259 will be passed to the constructor of the class.
Brett Cannon4b964f92008-05-05 20:21:38 +0000260
261
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000262.. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
Georg Brandl3961f182008-05-05 20:53:39 +0000263
264 Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
Ezio Melottid5cdc942013-03-29 03:59:29 +0200265 document) to a Python object using this :ref:`conversion table
266 <json-to-py-table>`.
Georg Brandl3961f182008-05-05 20:53:39 +0000267
268 If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
269 other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
270 specified. Encodings that are not ASCII based (such as UCS-2) are not
271 allowed and should be decoded to :class:`unicode` first.
272
Georg Brandlc6301952010-05-10 21:02:51 +0000273 The other arguments have the same meaning as in :func:`load`.
Georg Brandl3961f182008-05-05 20:53:39 +0000274
275
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200276Encoders and Decoders
Brett Cannon4b964f92008-05-05 20:21:38 +0000277---------------------
278
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000279.. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict[, object_pairs_hook]]]]]]])
Brett Cannon4b964f92008-05-05 20:21:38 +0000280
Georg Brandl3961f182008-05-05 20:53:39 +0000281 Simple JSON decoder.
Brett Cannon4b964f92008-05-05 20:21:38 +0000282
283 Performs the following translations in decoding by default:
284
Ezio Melottid5cdc942013-03-29 03:59:29 +0200285 .. _json-to-py-table:
286
Brett Cannon4b964f92008-05-05 20:21:38 +0000287 +---------------+-------------------+
288 | JSON | Python |
289 +===============+===================+
290 | object | dict |
291 +---------------+-------------------+
292 | array | list |
293 +---------------+-------------------+
294 | string | unicode |
295 +---------------+-------------------+
296 | number (int) | int, long |
297 +---------------+-------------------+
298 | number (real) | float |
299 +---------------+-------------------+
300 | true | True |
301 +---------------+-------------------+
302 | false | False |
303 +---------------+-------------------+
304 | null | None |
305 +---------------+-------------------+
306
307 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
308 corresponding ``float`` values, which is outside the JSON spec.
309
Georg Brandl3961f182008-05-05 20:53:39 +0000310 *encoding* determines the encoding used to interpret any :class:`str` objects
311 decoded by this instance (UTF-8 by default). It has no effect when decoding
312 :class:`unicode` objects.
Brett Cannon4b964f92008-05-05 20:21:38 +0000313
Georg Brandl3961f182008-05-05 20:53:39 +0000314 Note that currently only encodings that are a superset of ASCII work, strings
315 of other encodings should be passed in as :class:`unicode`.
Brett Cannon4b964f92008-05-05 20:21:38 +0000316
317 *object_hook*, if specified, will be called with the result of every JSON
318 object decoded and its return value will be used in place of the given
Georg Brandl3961f182008-05-05 20:53:39 +0000319 :class:`dict`. This can be used to provide custom deserializations (e.g. to
Brett Cannon4b964f92008-05-05 20:21:38 +0000320 support JSON-RPC class hinting).
321
Raymond Hettinger91852ca2009-03-19 19:19:03 +0000322 *object_pairs_hook*, if specified will be called with the result of every
323 JSON object decoded with an ordered list of pairs. The return value of
324 *object_pairs_hook* will be used instead of the :class:`dict`. This
325 feature can be used to implement custom decoders that rely on the order
326 that the key and value pairs are decoded (for example,
327 :func:`collections.OrderedDict` will remember the order of insertion). If
328 *object_hook* is also defined, the *object_pairs_hook* takes priority.
329
330 .. versionchanged:: 2.7
331 Added support for *object_pairs_hook*.
332
Brett Cannon4b964f92008-05-05 20:21:38 +0000333 *parse_float*, if specified, will be called with the string of every JSON
Georg Brandl3961f182008-05-05 20:53:39 +0000334 float to be decoded. By default, this is equivalent to ``float(num_str)``.
335 This can be used to use another datatype or parser for JSON floats
336 (e.g. :class:`decimal.Decimal`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000337
338 *parse_int*, if specified, will be called with the string of every JSON int
Georg Brandl3961f182008-05-05 20:53:39 +0000339 to be decoded. By default, this is equivalent to ``int(num_str)``. This can
340 be used to use another datatype or parser for JSON integers
341 (e.g. :class:`float`).
Brett Cannon4b964f92008-05-05 20:21:38 +0000342
343 *parse_constant*, if specified, will be called with one of the following
Georg Brandl3961f182008-05-05 20:53:39 +0000344 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
345 ``'false'``. This can be used to raise an exception if invalid JSON numbers
346 are encountered.
Brett Cannon4b964f92008-05-05 20:21:38 +0000347
Georg Brandldb949b82010-10-15 17:04:45 +0000348 If *strict* is ``False`` (``True`` is the default), then control characters
349 will be allowed inside strings. Control characters in this context are
350 those with character codes in the 0-31 range, including ``'\t'`` (tab),
351 ``'\n'``, ``'\r'`` and ``'\0'``.
352
Felix Cruxb4e186c2013-08-12 17:39:51 -0400353 If the data being deserialized is not a valid JSON document, a
354 :exc:`ValueError` will be raised.
Brett Cannon4b964f92008-05-05 20:21:38 +0000355
356 .. method:: decode(s)
357
Georg Brandl3961f182008-05-05 20:53:39 +0000358 Return the Python representation of *s* (a :class:`str` or
359 :class:`unicode` instance containing a JSON document)
Brett Cannon4b964f92008-05-05 20:21:38 +0000360
361 .. method:: raw_decode(s)
362
Georg Brandl3961f182008-05-05 20:53:39 +0000363 Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
364 beginning with a JSON document) and return a 2-tuple of the Python
365 representation and the index in *s* where the document ended.
Brett Cannon4b964f92008-05-05 20:21:38 +0000366
Georg Brandl3961f182008-05-05 20:53:39 +0000367 This can be used to decode a JSON document from a string that may have
368 extraneous data at the end.
Brett Cannon4b964f92008-05-05 20:21:38 +0000369
370
371.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
372
Georg Brandl3961f182008-05-05 20:53:39 +0000373 Extensible JSON encoder for Python data structures.
Brett Cannon4b964f92008-05-05 20:21:38 +0000374
375 Supports the following objects and types by default:
376
Ezio Melottid5cdc942013-03-29 03:59:29 +0200377 .. _py-to-json-table:
378
Brett Cannon4b964f92008-05-05 20:21:38 +0000379 +-------------------+---------------+
380 | Python | JSON |
381 +===================+===============+
382 | dict | object |
383 +-------------------+---------------+
384 | list, tuple | array |
385 +-------------------+---------------+
386 | str, unicode | string |
387 +-------------------+---------------+
388 | int, long, float | number |
389 +-------------------+---------------+
390 | True | true |
391 +-------------------+---------------+
392 | False | false |
393 +-------------------+---------------+
394 | None | null |
395 +-------------------+---------------+
396
397 To extend this to recognize other objects, subclass and implement a
Georg Brandl3961f182008-05-05 20:53:39 +0000398 :meth:`default` method with another method that returns a serializable object
Brett Cannon4b964f92008-05-05 20:21:38 +0000399 for ``o`` if possible, otherwise it should call the superclass implementation
400 (to raise :exc:`TypeError`).
401
402 If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
403 attempt encoding of keys that are not str, int, long, float or None. If
404 *skipkeys* is ``True``, such items are simply skipped.
405
Petri Lehtinenf9e1f112012-09-01 07:27:58 +0300406 If *ensure_ascii* is ``True`` (the default), all non-ASCII characters in the
407 output are escaped with ``\uXXXX`` sequences, and the results are
408 :class:`str` instances consisting of ASCII characters only. If
409 *ensure_ascii* is ``False``, a result may be a :class:`unicode`
410 instance. This usually happens if the input contains unicode strings or the
411 *encoding* parameter is used.
Brett Cannon4b964f92008-05-05 20:21:38 +0000412
413 If *check_circular* is ``True`` (the default), then lists, dicts, and custom
414 encoded objects will be checked for circular references during encoding to
415 prevent an infinite recursion (which would cause an :exc:`OverflowError`).
416 Otherwise, no such check takes place.
417
Georg Brandl3961f182008-05-05 20:53:39 +0000418 If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
419 ``-Infinity`` will be encoded as such. This behavior is not JSON
420 specification compliant, but is consistent with most JavaScript based
421 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
422 such floats.
Brett Cannon4b964f92008-05-05 20:21:38 +0000423
Georg Brandl21946af2010-10-06 09:28:45 +0000424 If *sort_keys* is ``True`` (default ``False``), then the output of dictionaries
Brett Cannon4b964f92008-05-05 20:21:38 +0000425 will be sorted by key; this is useful for regression tests to ensure that
426 JSON serializations can be compared on a day-to-day basis.
427
Georg Brandl3961f182008-05-05 20:53:39 +0000428 If *indent* is a non-negative integer (it is ``None`` by default), then JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000429 array elements and object members will be pretty-printed with that indent
430 level. An indent level of 0 will only insert newlines. ``None`` is the most
431 compact representation.
432
Ezio Melotti3a237eb2012-11-29 00:22:30 +0200433 .. note::
434
435 Since the default item separator is ``', '``, the output might include
436 trailing whitespace when *indent* is specified. You can use
437 ``separators=(',', ': ')`` to avoid this.
438
Georg Brandl3961f182008-05-05 20:53:39 +0000439 If specified, *separators* should be an ``(item_separator, key_separator)``
440 tuple. The default is ``(', ', ': ')``. To get the most compact JSON
Brett Cannon4b964f92008-05-05 20:21:38 +0000441 representation, you should specify ``(',', ':')`` to eliminate whitespace.
442
443 If specified, *default* is a function that gets called for objects that can't
444 otherwise be serialized. It should return a JSON encodable version of the
445 object or raise a :exc:`TypeError`.
446
447 If *encoding* is not ``None``, then all input strings will be transformed
448 into unicode using that encoding prior to JSON-encoding. The default is
449 UTF-8.
450
451
452 .. method:: default(o)
453
454 Implement this method in a subclass such that it returns a serializable
455 object for *o*, or calls the base implementation (to raise a
456 :exc:`TypeError`).
457
458 For example, to support arbitrary iterators, you could implement default
459 like this::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000460
Brett Cannon4b964f92008-05-05 20:21:38 +0000461 def default(self, o):
462 try:
Georg Brandl1379ae02008-09-24 09:47:55 +0000463 iterable = iter(o)
Brett Cannon4b964f92008-05-05 20:21:38 +0000464 except TypeError:
Georg Brandl1379ae02008-09-24 09:47:55 +0000465 pass
Brett Cannon4b964f92008-05-05 20:21:38 +0000466 else:
467 return list(iterable)
R David Murray35893b72013-03-17 22:06:18 -0400468 # Let the base class default method raise the TypeError
Brett Cannon4b964f92008-05-05 20:21:38 +0000469 return JSONEncoder.default(self, o)
470
471
472 .. method:: encode(o)
473
Georg Brandl3961f182008-05-05 20:53:39 +0000474 Return a JSON string representation of a Python data structure, *o*. For
Brett Cannon4b964f92008-05-05 20:21:38 +0000475 example::
476
477 >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
478 '{"foo": ["bar", "baz"]}'
479
480
481 .. method:: iterencode(o)
482
483 Encode the given object, *o*, and yield each string representation as
Georg Brandl3961f182008-05-05 20:53:39 +0000484 available. For example::
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000485
Brett Cannon4b964f92008-05-05 20:21:38 +0000486 for chunk in JSONEncoder().iterencode(bigobject):
487 mysocket.write(chunk)
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200488
489
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200490Standard Compliance and Interoperability
491----------------------------------------
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200492
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200493The JSON format is specified by :rfc:`7159` and by
494`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_.
495This section details this module's level of compliance with the RFC.
496For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and
497parameters other than those explicitly mentioned, are not considered.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200498
499This module does not comply with the RFC in a strict fashion, implementing some
500extensions that are valid JavaScript but not valid JSON. In particular:
501
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200502- Infinite and NaN number values are accepted and output;
503- Repeated names within an object are accepted, and only the value of the last
504 name-value pair is used.
505
506Since the RFC permits RFC-compliant parsers to accept input texts that are not
507RFC-compliant, this module's deserializer is technically RFC-compliant under
508default settings.
509
510Character Encodings
511^^^^^^^^^^^^^^^^^^^
512
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200513The RFC requires that JSON be represented using either UTF-8, UTF-16, or
514UTF-32, with UTF-8 being the recommended default for maximum interoperability.
515Accordingly, this module uses UTF-8 as the default for its *encoding* parameter.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200516
517This module's deserializer only directly works with ASCII-compatible encodings;
518UTF-16, UTF-32, and other ASCII-incompatible encodings require the use of
519workarounds described in the documentation for the deserializer's *encoding*
520parameter.
521
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200522As permitted, though not required, by the RFC, this module's serializer sets
523*ensure_ascii=True* by default, thus escaping the output so that the resulting
524strings only contain ASCII characters.
525
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200526The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
527and this module's serializer does not add a BOM to its output.
528The RFC permits, but does not require, JSON deserializers to ignore an initial
529BOM in their input. This module's deserializer raises a :exc:`ValueError`
530when an initial BOM is present.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200531
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200532The RFC does not explicitly forbid JSON strings which contain byte sequences
533that don't correspond to valid Unicode characters (e.g. unpaired UTF-16
534surrogates), but it does note that they may cause interoperability problems.
535By default, this module accepts and outputs (when present in the original
536:class:`str`) codepoints for such sequences.
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200537
538
539Infinite and NaN Number Values
540^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
541
542The RFC does not permit the representation of infinite or NaN number values.
543Despite that, by default, this module accepts and outputs ``Infinity``,
544``-Infinity``, and ``NaN`` as if they were valid JSON number literal values::
545
546 >>> # Neither of these calls raises an exception, but the results are not valid JSON
547 >>> json.dumps(float('-inf'))
548 '-Infinity'
549 >>> json.dumps(float('nan'))
550 'NaN'
551 >>> # Same when deserializing
552 >>> json.loads('-Infinity')
553 -inf
554 >>> json.loads('NaN')
555 nan
556
557In the serializer, the *allow_nan* parameter can be used to alter this
558behavior. In the deserializer, the *parse_constant* parameter can be used to
559alter this behavior.
560
561
562Repeated Names Within an Object
563^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
564
565The RFC specifies that the names within a JSON object should be unique, but
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200566does not mandate how repeated names in JSON objects should be handled. By
Antoine Pitrouf3e0a692012-08-24 19:46:17 +0200567default, this module does not raise an exception; instead, it ignores all but
568the last name-value pair for a given name::
569
570 >>> weird_json = '{"x": 1, "x": 2, "x": 3}'
571 >>> json.loads(weird_json)
572 {u'x': 3}
573
574The *object_pairs_hook* parameter can be used to alter this behavior.
Serhiy Storchaka2c1f3762014-11-27 19:41:34 +0200575
576
577Top-level Non-Object, Non-Array Values
578^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
579
580The old version of JSON specified by the obsolete :rfc:`4627` required that
581the top-level value of a JSON text must be either a JSON object or array
582(Python :class:`dict` or :class:`list`), and could not be a JSON null,
583boolean, number, or string value. :rfc:`7159` removed that restriction, and
584this module does not and has never implemented that restriction in either its
585serializer or its deserializer.
586
587Regardless, for maximum interoperability, you may wish to voluntarily adhere
588to the restriction yourself.
589
590
591Implementation Limitations
592^^^^^^^^^^^^^^^^^^^^^^^^^^
593
594Some JSON deserializer implementations may set limits on:
595
596* the size of accepted JSON texts
597* the maximum level of nesting of JSON objects and arrays
598* the range and precision of JSON numbers
599* the content and maximum length of JSON strings
600
601This module does not impose any such limits beyond those of the relevant
602Python datatypes themselves or the Python interpreter itself.
603
604When serializing to JSON, beware any such limitations in applications that may
605consume your JSON. In particular, it is common for JSON numbers to be
606deserialized into IEEE 754 double precision numbers and thus subject to that
607representation's range and precision limitations. This is especially relevant
608when serializing Python :class:`int` values of extremely large magnitude, or
609when serializing instances of "exotic" numerical types such as
610:class:`decimal.Decimal`.
611
612
613.. rubric:: Footnotes
614
615.. [#rfc-errata] As noted in `the errata for RFC 7159
616 <http://www.rfc-editor.org/errata_search.php?rfc=7159>`_,
617 JSON permits literal U+2028 (LINE SEPARATOR) and
618 U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
619 (as of ECMAScript Edition 5.1) does not.