blob: 6a185188e79e530715c0ca3022f87d57512f3b7f [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
34 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
35 '[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
128 (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be
129 skipped instead of raising a ``TypeError``.
Christian Heimes90540002008-05-08 14:29:10 +0000130
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000131 If ``ensure_ascii`` is false, then the some chunks written to ``fp``
Christian Heimes90540002008-05-08 14:29:10 +0000132 may be ``unicode`` instances, subject to normal Python ``str`` to
133 ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
134 understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
135 to cause an error.
136
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000137 If ``check_circular`` is false, then the circular reference check
Christian Heimes90540002008-05-08 14:29:10 +0000138 for container types will be skipped and a circular reference will
139 result in an ``OverflowError`` (or worse).
140
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000141 If ``allow_nan`` is false, then it will be a ``ValueError`` to
Christian Heimes90540002008-05-08 14:29:10 +0000142 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
143 in strict compliance of the JSON specification, instead of using the
144 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
145
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000146 If ``indent`` is a non-negative integer, then JSON array elements and
147 object members will be pretty-printed with that indent level. An indent
148 level of 0 will only insert newlines. ``None`` is the most compact
149 representation.
Christian Heimes90540002008-05-08 14:29:10 +0000150
151 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
152 then it will be used instead of the default ``(', ', ': ')`` separators.
153 ``(',', ':')`` is the most compact JSON representation.
154
Christian Heimes90540002008-05-08 14:29:10 +0000155 ``default(obj)`` is a function that should return a serializable version
156 of obj or raise TypeError. The default simply raises TypeError.
157
158 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
159 ``.default()`` method to serialize additional types), specify it with
160 the ``cls`` kwarg.
161
162 """
163 # cached encoder
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000164 if (not skipkeys and ensure_ascii and
165 check_circular and allow_nan and
Christian Heimes90540002008-05-08 14:29:10 +0000166 cls is None and indent is None and separators is None and
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000167 default is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000168 iterable = _default_encoder.iterencode(obj)
169 else:
170 if cls is None:
171 cls = JSONEncoder
172 iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
173 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000174 separators=separators,
Christian Heimes90540002008-05-08 14:29:10 +0000175 default=default, **kw).iterencode(obj)
176 # could accelerate with writelines in some versions of Python, at
177 # a debuggability cost
178 for chunk in iterable:
179 fp.write(chunk)
180
181
182def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
183 allow_nan=True, cls=None, indent=None, separators=None,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000184 default=None, **kw):
Christian Heimes90540002008-05-08 14:29:10 +0000185 """Serialize ``obj`` to a JSON formatted ``str``.
186
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000187 If ``skipkeys`` is false then ``dict`` keys that are not basic types
188 (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be
189 skipped instead of raising a ``TypeError``.
Christian Heimes90540002008-05-08 14:29:10 +0000190
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000191 If ``ensure_ascii`` is false, then the return value will be a
Christian Heimes90540002008-05-08 14:29:10 +0000192 ``unicode`` instance subject to normal Python ``str`` to ``unicode``
193 coercion rules instead of being escaped to an ASCII ``str``.
194
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000195 If ``check_circular`` is false, then the circular reference check
Christian Heimes90540002008-05-08 14:29:10 +0000196 for container types will be skipped and a circular reference will
197 result in an ``OverflowError`` (or worse).
198
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000199 If ``allow_nan`` is false, then it will be a ``ValueError`` to
Christian Heimes90540002008-05-08 14:29:10 +0000200 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
201 strict compliance of the JSON specification, instead of using the
202 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
203
204 If ``indent`` is a non-negative integer, then JSON array elements and
205 object members will be pretty-printed with that indent level. An indent
206 level of 0 will only insert newlines. ``None`` is the most compact
207 representation.
208
209 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
210 then it will be used instead of the default ``(', ', ': ')`` separators.
211 ``(',', ':')`` is the most compact JSON representation.
212
Christian Heimes90540002008-05-08 14:29:10 +0000213 ``default(obj)`` is a function that should return a serializable version
214 of obj or raise TypeError. The default simply raises TypeError.
215
216 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
217 ``.default()`` method to serialize additional types), specify it with
218 the ``cls`` kwarg.
219
220 """
221 # cached encoder
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000222 if (not skipkeys and ensure_ascii and
223 check_circular and allow_nan and
Christian Heimes90540002008-05-08 14:29:10 +0000224 cls is None and indent is None and separators is None and
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000225 default is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000226 return _default_encoder.encode(obj)
227 if cls is None:
228 cls = JSONEncoder
229 return cls(
230 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
231 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000232 separators=separators, default=default,
Christian Heimes90540002008-05-08 14:29:10 +0000233 **kw).encode(obj)
234
235
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000236_default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)
Christian Heimes90540002008-05-08 14:29:10 +0000237
238
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000239def load(fp, cls=None, object_hook=None, parse_float=None,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000240 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000241 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
242 a JSON document) to a Python object.
Christian Heimes90540002008-05-08 14:29:10 +0000243
244 ``object_hook`` is an optional function that will be called with the
245 result of any object literal decode (a ``dict``). The return value of
246 ``object_hook`` will be used instead of the ``dict``. This feature
247 can be used to implement custom decoders (e.g. JSON-RPC class hinting).
248
249 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
250 kwarg.
251
252 """
253 return loads(fp.read(),
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000254 cls=cls, object_hook=object_hook,
Christian Heimes90540002008-05-08 14:29:10 +0000255 parse_float=parse_float, parse_int=parse_int,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000256 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
Christian Heimes90540002008-05-08 14:29:10 +0000257
258
259def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000260 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000261 """Deserialize ``s`` (a ``str`` instance containing a JSON
Christian Heimes90540002008-05-08 14:29:10 +0000262 document) to a Python object.
263
Christian Heimes90540002008-05-08 14:29:10 +0000264 ``object_hook`` is an optional function that will be called with the
265 result of any object literal decode (a ``dict``). The return value of
266 ``object_hook`` will be used instead of the ``dict``. This feature
267 can be used to implement custom decoders (e.g. JSON-RPC class hinting).
268
269 ``parse_float``, if specified, will be called with the string
270 of every JSON float to be decoded. By default this is equivalent to
271 float(num_str). This can be used to use another datatype or parser
272 for JSON floats (e.g. decimal.Decimal).
273
274 ``parse_int``, if specified, will be called with the string
275 of every JSON int to be decoded. By default this is equivalent to
276 int(num_str). This can be used to use another datatype or parser
277 for JSON integers (e.g. float).
278
279 ``parse_constant``, if specified, will be called with one of the
280 following strings: -Infinity, Infinity, NaN, null, true, false.
281 This can be used to raise an exception if invalid JSON numbers
282 are encountered.
283
284 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
285 kwarg.
286
287 """
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000288 if (cls is None and object_hook is None and
Christian Heimes90540002008-05-08 14:29:10 +0000289 parse_int is None and parse_float is None and
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000290 parse_constant is None and object_pairs_hook is None and not kw):
Christian Heimes90540002008-05-08 14:29:10 +0000291 return _default_decoder.decode(s)
292 if cls is None:
293 cls = JSONDecoder
294 if object_hook is not None:
295 kw['object_hook'] = object_hook
Raymond Hettinger0ad98d82009-04-21 03:09:17 +0000296 if object_pairs_hook is not None:
297 kw['object_pairs_hook'] = object_pairs_hook
Christian Heimes90540002008-05-08 14:29:10 +0000298 if parse_float is not None:
299 kw['parse_float'] = parse_float
300 if parse_int is not None:
301 kw['parse_int'] = parse_int
302 if parse_constant is not None:
303 kw['parse_constant'] = parse_constant
Benjamin Petersonc6b607d2009-05-02 12:36:44 +0000304 return cls(**kw).decode(s)