blob: 6d88931dcbdce308adbf9e4894d1e64f51db168c [file] [log] [blame]
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00001r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of
Christian Heimes90540002008-05-08 14:29:10 +00002JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
3interchange format.
4
Benjamin Petersonc6b607d2009-05-02 12:36:44 +00005:mod:`json` exposes an API familiar to users of the standard library
6:mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
7version of the :mod:`json` library contained in Python 2.6, but maintains
8compatibility with Python 2.4 and Python 2.5 and (currently) has
9significant performance advantages, even without using the optional C
10extension for speedups.
Christian Heimes90540002008-05-08 14:29:10 +000011
12Encoding basic Python object hierarchies::
13
14 >>> import json
15 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
16 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
17 >>> print(json.dumps("\"foo\bar"))
18 "\"foo\bar"
19 >>> print(json.dumps('\u1234'))
20 "\u1234"
21 >>> print(json.dumps('\\'))
22 "\\"
23 >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
24 {"a": 0, "b": 0, "c": 0}
25 >>> from io import StringIO
26 >>> io = StringIO()
27 >>> json.dump(['streaming API'], io)
28 >>> io.getvalue()
29 '["streaming API"]'
30
31Compact encoding::
32
33 >>> import json
Éric Araujode579d42011-04-21 02:37:41 +020034 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':'))
Christian Heimes90540002008-05-08 14:29:10 +000035 '[1,2,3,{"4":5,"6":7}]'
36
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000037Pretty printing::
Christian Heimes90540002008-05-08 14:29:10 +000038
39 >>> import json
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000040 >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
41 >>> print('\n'.join([l.rstrip() for l in s.splitlines()]))
42 {
43 "4": 5,
44 "6": 7
45 }
Christian Heimes90540002008-05-08 14:29:10 +000046
47Decoding JSON::
48
49 >>> import json
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000050 >>> obj = ['foo', {'bar': ['baz', None, 1.0, 2]}]
51 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
52 True
53 >>> json.loads('"\\"foo\\bar"') == '"foo\x08ar'
54 True
Christian Heimes90540002008-05-08 14:29:10 +000055 >>> from io import StringIO
56 >>> io = StringIO('["streaming API"]')
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000057 >>> json.load(io)[0] == 'streaming API'
58 True
Christian Heimes90540002008-05-08 14:29:10 +000059
60Specializing JSON object decoding::
61
62 >>> import json
63 >>> def as_complex(dct):
64 ... if '__complex__' in dct:
65 ... return complex(dct['real'], dct['imag'])
66 ... return dct
67 ...
68 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
69 ... object_hook=as_complex)
70 (1+2j)
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000071 >>> from decimal import Decimal
72 >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
73 True
Christian Heimes90540002008-05-08 14:29:10 +000074
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000075Specializing JSON object encoding::
Christian Heimes90540002008-05-08 14:29:10 +000076
77 >>> import json
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000078 >>> def encode_complex(obj):
79 ... if isinstance(obj, complex):
80 ... return [obj.real, obj.imag]
81 ... raise TypeError(repr(o) + " is not JSON serializable")
Christian Heimes90540002008-05-08 14:29:10 +000082 ...
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000083 >>> json.dumps(2 + 1j, default=encode_complex)
Christian Heimes90540002008-05-08 14:29:10 +000084 '[2.0, 1.0]'
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000085 >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
Christian Heimes90540002008-05-08 14:29:10 +000086 '[2.0, 1.0]'
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000087 >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
88 '[2.0, 1.0]'
Christian Heimes90540002008-05-08 14:29:10 +000089
90
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000091Using json.tool from the shell to validate and pretty-print::
Christian Heimes90540002008-05-08 14:29:10 +000092
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000093 $ echo '{"json":"obj"}' | python -m json.tool
Christian Heimes90540002008-05-08 14:29:10 +000094 {
95 "json": "obj"
96 }
Benjamin Petersonc6b607d2009-05-02 12:36:44 +000097 $ echo '{ 1.2:3.4}' | python -m json.tool
Christian Heimes90540002008-05-08 14:29:10 +000098 Expecting property name: line 1 column 2 (char 2)
Christian Heimes90540002008-05-08 14:29:10 +000099"""
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000100__version__ = '2.0.9'
Christian Heimes90540002008-05-08 14:29:10 +0000101__all__ = [
102 'dump', 'dumps', 'load', 'loads',
103 'JSONDecoder', 'JSONEncoder',
104]
105
106__author__ = 'Bob Ippolito <bob@redivi.com>'
107
108from .decoder import JSONDecoder
109from .encoder import JSONEncoder
110
111_default_encoder = JSONEncoder(
112 skipkeys=False,
113 ensure_ascii=True,
114 check_circular=True,
115 allow_nan=True,
116 indent=None,
117 separators=None,
Christian Heimes90540002008-05-08 14:29:10 +0000118 default=None,
119)
120
121def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
122 allow_nan=True, cls=None, indent=None, separators=None,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000123 default=None, **kw):
Christian Heimes90540002008-05-08 14:29:10 +0000124 """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
125 ``.write()``-supporting file-like object).
126
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000127 If ``skipkeys`` is true then ``dict`` keys that are not basic types
Georg Brandl4009c9e2010-10-06 08:26:09 +0000128 (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
129 instead of raising a ``TypeError``.
Christian Heimes90540002008-05-08 14:29:10 +0000130
Georg Brandl4009c9e2010-10-06 08:26:09 +0000131 If ``ensure_ascii`` is false, then the strings written to ``fp`` can
132 contain non-ASCII characters if they appear in strings contained in
133 ``obj``. Otherwise, all such characters are escaped in JSON strings.
Christian Heimes90540002008-05-08 14:29:10 +0000134
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000135 If ``check_circular`` is false, then the circular reference check
Christian Heimes90540002008-05-08 14:29:10 +0000136 for container types will be skipped and a circular reference will
137 result in an ``OverflowError`` (or worse).
138
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000139 If ``allow_nan`` is false, then it will be a ``ValueError`` to
Christian Heimes90540002008-05-08 14:29:10 +0000140 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
141 in strict compliance of the JSON specification, instead of using the
142 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
143
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000144 If ``indent`` is a non-negative integer, then JSON array elements and
145 object members will be pretty-printed with that indent level. An indent
146 level of 0 will only insert newlines. ``None`` is the most compact
147 representation.
Christian Heimes90540002008-05-08 14:29:10 +0000148
149 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
150 then it will be used instead of the default ``(', ', ': ')`` separators.
151 ``(',', ':')`` is the most compact JSON representation.
152
Christian Heimes90540002008-05-08 14:29:10 +0000153 ``default(obj)`` is a function that should return a serializable version
154 of obj or raise TypeError. The default simply raises TypeError.
155
156 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
157 ``.default()`` method to serialize additional types), specify it with
Georg Brandlc524cff2010-11-26 08:42:45 +0000158 the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000159
160 """
161 # cached encoder
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000162 if (not skipkeys and ensure_ascii and
163 check_circular and allow_nan and
Christian Heimes90540002008-05-08 14:29:10 +0000164 cls is None and indent is None and separators is None and
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000165 default is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000166 iterable = _default_encoder.iterencode(obj)
167 else:
168 if cls is None:
169 cls = JSONEncoder
170 iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
171 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000172 separators=separators,
Christian Heimes90540002008-05-08 14:29:10 +0000173 default=default, **kw).iterencode(obj)
174 # could accelerate with writelines in some versions of Python, at
175 # a debuggability cost
176 for chunk in iterable:
177 fp.write(chunk)
178
179
180def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
181 allow_nan=True, cls=None, indent=None, separators=None,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000182 default=None, **kw):
Christian Heimes90540002008-05-08 14:29:10 +0000183 """Serialize ``obj`` to a JSON formatted ``str``.
184
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000185 If ``skipkeys`` is false then ``dict`` keys that are not basic types
Georg Brandl4009c9e2010-10-06 08:26:09 +0000186 (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
187 instead of raising a ``TypeError``.
Christian Heimes90540002008-05-08 14:29:10 +0000188
Georg Brandl4009c9e2010-10-06 08:26:09 +0000189 If ``ensure_ascii`` is false, then the return value can contain non-ASCII
190 characters if they appear in strings contained in ``obj``. Otherwise, all
191 such characters are escaped in JSON strings.
Christian Heimes90540002008-05-08 14:29:10 +0000192
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000193 If ``check_circular`` is false, then the circular reference check
Christian Heimes90540002008-05-08 14:29:10 +0000194 for container types will be skipped and a circular reference will
195 result in an ``OverflowError`` (or worse).
196
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000197 If ``allow_nan`` is false, then it will be a ``ValueError`` to
Christian Heimes90540002008-05-08 14:29:10 +0000198 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
199 strict compliance of the JSON specification, instead of using the
200 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
201
202 If ``indent`` is a non-negative integer, then JSON array elements and
203 object members will be pretty-printed with that indent level. An indent
204 level of 0 will only insert newlines. ``None`` is the most compact
205 representation.
206
207 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
208 then it will be used instead of the default ``(', ', ': ')`` separators.
209 ``(',', ':')`` is the most compact JSON representation.
210
Christian Heimes90540002008-05-08 14:29:10 +0000211 ``default(obj)`` is a function that should return a serializable version
212 of obj or raise TypeError. The default simply raises TypeError.
213
214 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
215 ``.default()`` method to serialize additional types), specify it with
Georg Brandlc524cff2010-11-26 08:42:45 +0000216 the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000217
218 """
219 # cached encoder
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000220 if (not skipkeys and ensure_ascii and
221 check_circular and allow_nan and
Christian Heimes90540002008-05-08 14:29:10 +0000222 cls is None and indent is None and separators is None and
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000223 default is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000224 return _default_encoder.encode(obj)
225 if cls is None:
226 cls = JSONEncoder
227 return cls(
228 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
229 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000230 separators=separators, default=default,
Christian Heimes90540002008-05-08 14:29:10 +0000231 **kw).encode(obj)
232
233
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000234_default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
Christian Heimes90540002008-05-08 14:29:10 +0000235
236
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000237def load(fp, cls=None, object_hook=None, parse_float=None,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000238 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000239 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
240 a JSON document) to a Python object.
Christian Heimes90540002008-05-08 14:29:10 +0000241
242 ``object_hook`` is an optional function that will be called with the
243 result of any object literal decode (a ``dict``). The return value of
244 ``object_hook`` will be used instead of the ``dict``. This feature
245 can be used to implement custom decoders (e.g. JSON-RPC class hinting).
246
Georg Brandlc524cff2010-11-26 08:42:45 +0000247 ``object_pairs_hook`` is an optional function that will be called with the
248 result of any object literal decoded with an ordered list of pairs. The
249 return value of ``object_pairs_hook`` will be used instead of the ``dict``.
250 This feature can be used to implement custom decoders that rely on the
251 order that the key and value pairs are decoded (for example,
252 collections.OrderedDict will remember the order of insertion). If
253 ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
254
Christian Heimes90540002008-05-08 14:29:10 +0000255 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
Georg Brandlc524cff2010-11-26 08:42:45 +0000256 kwarg; otherwise ``JSONDecoder`` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000257
258 """
259 return loads(fp.read(),
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000260 cls=cls, object_hook=object_hook,
Christian Heimes90540002008-05-08 14:29:10 +0000261 parse_float=parse_float, parse_int=parse_int,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000262 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000263
264
265def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000266 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000267 """Deserialize ``s`` (a ``str`` instance containing a JSON
Christian Heimes90540002008-05-08 14:29:10 +0000268 document) to a Python object.
269
Christian Heimes90540002008-05-08 14:29:10 +0000270 ``object_hook`` is an optional function that will be called with the
271 result of any object literal decode (a ``dict``). The return value of
272 ``object_hook`` will be used instead of the ``dict``. This feature
273 can be used to implement custom decoders (e.g. JSON-RPC class hinting).
274
Georg Brandlc524cff2010-11-26 08:42:45 +0000275 ``object_pairs_hook`` is an optional function that will be called with the
276 result of any object literal decoded with an ordered list of pairs. The
277 return value of ``object_pairs_hook`` will be used instead of the ``dict``.
278 This feature can be used to implement custom decoders that rely on the
279 order that the key and value pairs are decoded (for example,
280 collections.OrderedDict will remember the order of insertion). If
281 ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
282
Christian Heimes90540002008-05-08 14:29:10 +0000283 ``parse_float``, if specified, will be called with the string
284 of every JSON float to be decoded. By default this is equivalent to
285 float(num_str). This can be used to use another datatype or parser
286 for JSON floats (e.g. decimal.Decimal).
287
288 ``parse_int``, if specified, will be called with the string
289 of every JSON int to be decoded. By default this is equivalent to
290 int(num_str). This can be used to use another datatype or parser
291 for JSON integers (e.g. float).
292
293 ``parse_constant``, if specified, will be called with one of the
294 following strings: -Infinity, Infinity, NaN, null, true, false.
295 This can be used to raise an exception if invalid JSON numbers
296 are encountered.
297
298 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
Georg Brandlc524cff2010-11-26 08:42:45 +0000299 kwarg; otherwise ``JSONDecoder`` is used.
Christian Heimes90540002008-05-08 14:29:10 +0000300
Antoine Pitroua970e622011-01-21 21:39:56 +0000301 The ``encoding`` argument is ignored and deprecated.
302
Christian Heimes90540002008-05-08 14:29:10 +0000303 """
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000304 if (cls is None and object_hook is None and
Christian Heimes90540002008-05-08 14:29:10 +0000305 parse_int is None and parse_float is None and
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000306 parse_constant is None and object_pairs_hook is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000307 return _default_decoder.decode(s)
308 if cls is None:
309 cls = JSONDecoder
310 if object_hook is not None:
311 kw['object_hook'] = object_hook
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000312 if object_pairs_hook is not None:
313 kw['object_pairs_hook'] = object_pairs_hook
Christian Heimes90540002008-05-08 14:29:10 +0000314 if parse_float is not None:
315 kw['parse_float'] = parse_float
316 if parse_int is not None:
317 kw['parse_int'] = parse_int
318 if parse_constant is not None:
319 kw['parse_constant'] = parse_constant
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000320 return cls(**kw).decode(s)