1 | n/a | r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of |
---|
2 | n/a | JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data |
---|
3 | n/a | interchange format. |
---|
4 | n/a | |
---|
5 | n/a | :mod:`json` exposes an API familiar to users of the standard library |
---|
6 | n/a | :mod:`marshal` and :mod:`pickle` modules. It is derived from a |
---|
7 | n/a | version of the externally maintained simplejson library. |
---|
8 | n/a | |
---|
9 | n/a | Encoding basic Python object hierarchies:: |
---|
10 | n/a | |
---|
11 | n/a | >>> import json |
---|
12 | n/a | >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) |
---|
13 | n/a | '["foo", {"bar": ["baz", null, 1.0, 2]}]' |
---|
14 | n/a | >>> print(json.dumps("\"foo\bar")) |
---|
15 | n/a | "\"foo\bar" |
---|
16 | n/a | >>> print(json.dumps('\u1234')) |
---|
17 | n/a | "\u1234" |
---|
18 | n/a | >>> print(json.dumps('\\')) |
---|
19 | n/a | "\\" |
---|
20 | n/a | >>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)) |
---|
21 | n/a | {"a": 0, "b": 0, "c": 0} |
---|
22 | n/a | >>> from io import StringIO |
---|
23 | n/a | >>> io = StringIO() |
---|
24 | n/a | >>> json.dump(['streaming API'], io) |
---|
25 | n/a | >>> io.getvalue() |
---|
26 | n/a | '["streaming API"]' |
---|
27 | n/a | |
---|
28 | n/a | Compact encoding:: |
---|
29 | n/a | |
---|
30 | n/a | >>> import json |
---|
31 | n/a | >>> from collections import OrderedDict |
---|
32 | n/a | >>> mydict = OrderedDict([('4', 5), ('6', 7)]) |
---|
33 | n/a | >>> json.dumps([1,2,3,mydict], separators=(',', ':')) |
---|
34 | n/a | '[1,2,3,{"4":5,"6":7}]' |
---|
35 | n/a | |
---|
36 | n/a | Pretty printing:: |
---|
37 | n/a | |
---|
38 | n/a | >>> import json |
---|
39 | n/a | >>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)) |
---|
40 | n/a | { |
---|
41 | n/a | "4": 5, |
---|
42 | n/a | "6": 7 |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | Decoding JSON:: |
---|
46 | n/a | |
---|
47 | n/a | >>> import json |
---|
48 | n/a | >>> obj = ['foo', {'bar': ['baz', None, 1.0, 2]}] |
---|
49 | n/a | >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj |
---|
50 | n/a | True |
---|
51 | n/a | >>> json.loads('"\\"foo\\bar"') == '"foo\x08ar' |
---|
52 | n/a | True |
---|
53 | n/a | >>> from io import StringIO |
---|
54 | n/a | >>> io = StringIO('["streaming API"]') |
---|
55 | n/a | >>> json.load(io)[0] == 'streaming API' |
---|
56 | n/a | True |
---|
57 | n/a | |
---|
58 | n/a | Specializing JSON object decoding:: |
---|
59 | n/a | |
---|
60 | n/a | >>> import json |
---|
61 | n/a | >>> def as_complex(dct): |
---|
62 | n/a | ... if '__complex__' in dct: |
---|
63 | n/a | ... return complex(dct['real'], dct['imag']) |
---|
64 | n/a | ... return dct |
---|
65 | n/a | ... |
---|
66 | n/a | >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', |
---|
67 | n/a | ... object_hook=as_complex) |
---|
68 | n/a | (1+2j) |
---|
69 | n/a | >>> from decimal import Decimal |
---|
70 | n/a | >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1') |
---|
71 | n/a | True |
---|
72 | n/a | |
---|
73 | n/a | Specializing JSON object encoding:: |
---|
74 | n/a | |
---|
75 | n/a | >>> import json |
---|
76 | n/a | >>> def encode_complex(obj): |
---|
77 | n/a | ... if isinstance(obj, complex): |
---|
78 | n/a | ... return [obj.real, obj.imag] |
---|
79 | n/a | ... raise TypeError(repr(o) + " is not JSON serializable") |
---|
80 | n/a | ... |
---|
81 | n/a | >>> json.dumps(2 + 1j, default=encode_complex) |
---|
82 | n/a | '[2.0, 1.0]' |
---|
83 | n/a | >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) |
---|
84 | n/a | '[2.0, 1.0]' |
---|
85 | n/a | >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) |
---|
86 | n/a | '[2.0, 1.0]' |
---|
87 | n/a | |
---|
88 | n/a | |
---|
89 | n/a | Using json.tool from the shell to validate and pretty-print:: |
---|
90 | n/a | |
---|
91 | n/a | $ echo '{"json":"obj"}' | python -m json.tool |
---|
92 | n/a | { |
---|
93 | n/a | "json": "obj" |
---|
94 | n/a | } |
---|
95 | n/a | $ echo '{ 1.2:3.4}' | python -m json.tool |
---|
96 | n/a | Expecting property name enclosed in double quotes: line 1 column 3 (char 2) |
---|
97 | n/a | """ |
---|
98 | n/a | __version__ = '2.0.9' |
---|
99 | n/a | __all__ = [ |
---|
100 | n/a | 'dump', 'dumps', 'load', 'loads', |
---|
101 | n/a | 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', |
---|
102 | n/a | ] |
---|
103 | n/a | |
---|
104 | n/a | __author__ = 'Bob Ippolito <bob@redivi.com>' |
---|
105 | n/a | |
---|
106 | n/a | from .decoder import JSONDecoder, JSONDecodeError |
---|
107 | n/a | from .encoder import JSONEncoder |
---|
108 | n/a | import codecs |
---|
109 | n/a | |
---|
110 | n/a | _default_encoder = JSONEncoder( |
---|
111 | n/a | skipkeys=False, |
---|
112 | n/a | ensure_ascii=True, |
---|
113 | n/a | check_circular=True, |
---|
114 | n/a | allow_nan=True, |
---|
115 | n/a | indent=None, |
---|
116 | n/a | separators=None, |
---|
117 | n/a | default=None, |
---|
118 | n/a | ) |
---|
119 | n/a | |
---|
120 | n/a | def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, |
---|
121 | n/a | allow_nan=True, cls=None, indent=None, separators=None, |
---|
122 | n/a | default=None, sort_keys=False, **kw): |
---|
123 | n/a | """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a |
---|
124 | n/a | ``.write()``-supporting file-like object). |
---|
125 | n/a | |
---|
126 | n/a | If ``skipkeys`` is true then ``dict`` keys that are not basic types |
---|
127 | n/a | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
---|
128 | n/a | instead of raising a ``TypeError``. |
---|
129 | n/a | |
---|
130 | n/a | If ``ensure_ascii`` is false, then the strings written to ``fp`` can |
---|
131 | n/a | contain non-ASCII characters if they appear in strings contained in |
---|
132 | n/a | ``obj``. Otherwise, all such characters are escaped in JSON strings. |
---|
133 | n/a | |
---|
134 | n/a | If ``check_circular`` is false, then the circular reference check |
---|
135 | n/a | for container types will be skipped and a circular reference will |
---|
136 | n/a | result in an ``OverflowError`` (or worse). |
---|
137 | n/a | |
---|
138 | n/a | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
---|
139 | n/a | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) |
---|
140 | n/a | in strict compliance of the JSON specification, instead of using the |
---|
141 | n/a | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
---|
142 | n/a | |
---|
143 | n/a | If ``indent`` is a non-negative integer, then JSON array elements and |
---|
144 | n/a | object members will be pretty-printed with that indent level. An indent |
---|
145 | n/a | level of 0 will only insert newlines. ``None`` is the most compact |
---|
146 | n/a | representation. |
---|
147 | n/a | |
---|
148 | n/a | If specified, ``separators`` should be an ``(item_separator, key_separator)`` |
---|
149 | n/a | tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and |
---|
150 | n/a | ``(',', ': ')`` otherwise. To get the most compact JSON representation, |
---|
151 | n/a | you should specify ``(',', ':')`` to eliminate whitespace. |
---|
152 | n/a | |
---|
153 | n/a | ``default(obj)`` is a function that should return a serializable version |
---|
154 | n/a | of obj or raise TypeError. The default simply raises TypeError. |
---|
155 | n/a | |
---|
156 | n/a | If *sort_keys* is true (default: ``False``), then the output of |
---|
157 | n/a | dictionaries will be sorted by key. |
---|
158 | n/a | |
---|
159 | n/a | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
---|
160 | n/a | ``.default()`` method to serialize additional types), specify it with |
---|
161 | n/a | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
---|
162 | n/a | |
---|
163 | n/a | """ |
---|
164 | n/a | # cached encoder |
---|
165 | n/a | if (not skipkeys and ensure_ascii and |
---|
166 | n/a | check_circular and allow_nan and |
---|
167 | n/a | cls is None and indent is None and separators is None and |
---|
168 | n/a | default is None and not sort_keys and not kw): |
---|
169 | n/a | iterable = _default_encoder.iterencode(obj) |
---|
170 | n/a | else: |
---|
171 | n/a | if cls is None: |
---|
172 | n/a | cls = JSONEncoder |
---|
173 | n/a | iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
---|
174 | n/a | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
---|
175 | n/a | separators=separators, |
---|
176 | n/a | default=default, sort_keys=sort_keys, **kw).iterencode(obj) |
---|
177 | n/a | # could accelerate with writelines in some versions of Python, at |
---|
178 | n/a | # a debuggability cost |
---|
179 | n/a | for chunk in iterable: |
---|
180 | n/a | fp.write(chunk) |
---|
181 | n/a | |
---|
182 | n/a | |
---|
183 | n/a | def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, |
---|
184 | n/a | allow_nan=True, cls=None, indent=None, separators=None, |
---|
185 | n/a | default=None, sort_keys=False, **kw): |
---|
186 | n/a | """Serialize ``obj`` to a JSON formatted ``str``. |
---|
187 | n/a | |
---|
188 | n/a | If ``skipkeys`` is true then ``dict`` keys that are not basic types |
---|
189 | n/a | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
---|
190 | n/a | instead of raising a ``TypeError``. |
---|
191 | n/a | |
---|
192 | n/a | If ``ensure_ascii`` is false, then the return value can contain non-ASCII |
---|
193 | n/a | characters if they appear in strings contained in ``obj``. Otherwise, all |
---|
194 | n/a | such characters are escaped in JSON strings. |
---|
195 | n/a | |
---|
196 | n/a | If ``check_circular`` is false, then the circular reference check |
---|
197 | n/a | for container types will be skipped and a circular reference will |
---|
198 | n/a | result in an ``OverflowError`` (or worse). |
---|
199 | n/a | |
---|
200 | n/a | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
---|
201 | n/a | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in |
---|
202 | n/a | strict compliance of the JSON specification, instead of using the |
---|
203 | n/a | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
---|
204 | n/a | |
---|
205 | n/a | If ``indent`` is a non-negative integer, then JSON array elements and |
---|
206 | n/a | object members will be pretty-printed with that indent level. An indent |
---|
207 | n/a | level of 0 will only insert newlines. ``None`` is the most compact |
---|
208 | n/a | representation. |
---|
209 | n/a | |
---|
210 | n/a | If specified, ``separators`` should be an ``(item_separator, key_separator)`` |
---|
211 | n/a | tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and |
---|
212 | n/a | ``(',', ': ')`` otherwise. To get the most compact JSON representation, |
---|
213 | n/a | you should specify ``(',', ':')`` to eliminate whitespace. |
---|
214 | n/a | |
---|
215 | n/a | ``default(obj)`` is a function that should return a serializable version |
---|
216 | n/a | of obj or raise TypeError. The default simply raises TypeError. |
---|
217 | n/a | |
---|
218 | n/a | If *sort_keys* is true (default: ``False``), then the output of |
---|
219 | n/a | dictionaries will be sorted by key. |
---|
220 | n/a | |
---|
221 | n/a | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
---|
222 | n/a | ``.default()`` method to serialize additional types), specify it with |
---|
223 | n/a | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
---|
224 | n/a | |
---|
225 | n/a | """ |
---|
226 | n/a | # cached encoder |
---|
227 | n/a | if (not skipkeys and ensure_ascii and |
---|
228 | n/a | check_circular and allow_nan and |
---|
229 | n/a | cls is None and indent is None and separators is None and |
---|
230 | n/a | default is None and not sort_keys and not kw): |
---|
231 | n/a | return _default_encoder.encode(obj) |
---|
232 | n/a | if cls is None: |
---|
233 | n/a | cls = JSONEncoder |
---|
234 | n/a | return cls( |
---|
235 | n/a | skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
---|
236 | n/a | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
---|
237 | n/a | separators=separators, default=default, sort_keys=sort_keys, |
---|
238 | n/a | **kw).encode(obj) |
---|
239 | n/a | |
---|
240 | n/a | |
---|
241 | n/a | _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None) |
---|
242 | n/a | |
---|
243 | n/a | |
---|
244 | n/a | def detect_encoding(b): |
---|
245 | n/a | bstartswith = b.startswith |
---|
246 | n/a | if bstartswith((codecs.BOM_UTF32_BE, codecs.BOM_UTF32_LE)): |
---|
247 | n/a | return 'utf-32' |
---|
248 | n/a | if bstartswith((codecs.BOM_UTF16_BE, codecs.BOM_UTF16_LE)): |
---|
249 | n/a | return 'utf-16' |
---|
250 | n/a | if bstartswith(codecs.BOM_UTF8): |
---|
251 | n/a | return 'utf-8-sig' |
---|
252 | n/a | |
---|
253 | n/a | if len(b) >= 4: |
---|
254 | n/a | if not b[0]: |
---|
255 | n/a | # 00 00 -- -- - utf-32-be |
---|
256 | n/a | # 00 XX -- -- - utf-16-be |
---|
257 | n/a | return 'utf-16-be' if b[1] else 'utf-32-be' |
---|
258 | n/a | if not b[1]: |
---|
259 | n/a | # XX 00 00 00 - utf-32-le |
---|
260 | n/a | # XX 00 00 XX - utf-16-le |
---|
261 | n/a | # XX 00 XX -- - utf-16-le |
---|
262 | n/a | return 'utf-16-le' if b[2] or b[3] else 'utf-32-le' |
---|
263 | n/a | elif len(b) == 2: |
---|
264 | n/a | if not b[0]: |
---|
265 | n/a | # 00 XX - utf-16-be |
---|
266 | n/a | return 'utf-16-be' |
---|
267 | n/a | if not b[1]: |
---|
268 | n/a | # XX 00 - utf-16-le |
---|
269 | n/a | return 'utf-16-le' |
---|
270 | n/a | # default |
---|
271 | n/a | return 'utf-8' |
---|
272 | n/a | |
---|
273 | n/a | |
---|
274 | n/a | def load(fp, *, cls=None, object_hook=None, parse_float=None, |
---|
275 | n/a | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
---|
276 | n/a | """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing |
---|
277 | n/a | a JSON document) to a Python object. |
---|
278 | n/a | |
---|
279 | n/a | ``object_hook`` is an optional function that will be called with the |
---|
280 | n/a | result of any object literal decode (a ``dict``). The return value of |
---|
281 | n/a | ``object_hook`` will be used instead of the ``dict``. This feature |
---|
282 | n/a | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
---|
283 | n/a | |
---|
284 | n/a | ``object_pairs_hook`` is an optional function that will be called with the |
---|
285 | n/a | result of any object literal decoded with an ordered list of pairs. The |
---|
286 | n/a | return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
---|
287 | n/a | This feature can be used to implement custom decoders that rely on the |
---|
288 | n/a | order that the key and value pairs are decoded (for example, |
---|
289 | n/a | collections.OrderedDict will remember the order of insertion). If |
---|
290 | n/a | ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
---|
291 | n/a | |
---|
292 | n/a | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
---|
293 | n/a | kwarg; otherwise ``JSONDecoder`` is used. |
---|
294 | n/a | |
---|
295 | n/a | """ |
---|
296 | n/a | return loads(fp.read(), |
---|
297 | n/a | cls=cls, object_hook=object_hook, |
---|
298 | n/a | parse_float=parse_float, parse_int=parse_int, |
---|
299 | n/a | parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) |
---|
300 | n/a | |
---|
301 | n/a | |
---|
302 | n/a | def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, |
---|
303 | n/a | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
---|
304 | n/a | """Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance |
---|
305 | n/a | containing a JSON document) to a Python object. |
---|
306 | n/a | |
---|
307 | n/a | ``object_hook`` is an optional function that will be called with the |
---|
308 | n/a | result of any object literal decode (a ``dict``). The return value of |
---|
309 | n/a | ``object_hook`` will be used instead of the ``dict``. This feature |
---|
310 | n/a | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
---|
311 | n/a | |
---|
312 | n/a | ``object_pairs_hook`` is an optional function that will be called with the |
---|
313 | n/a | result of any object literal decoded with an ordered list of pairs. The |
---|
314 | n/a | return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
---|
315 | n/a | This feature can be used to implement custom decoders that rely on the |
---|
316 | n/a | order that the key and value pairs are decoded (for example, |
---|
317 | n/a | collections.OrderedDict will remember the order of insertion). If |
---|
318 | n/a | ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
---|
319 | n/a | |
---|
320 | n/a | ``parse_float``, if specified, will be called with the string |
---|
321 | n/a | of every JSON float to be decoded. By default this is equivalent to |
---|
322 | n/a | float(num_str). This can be used to use another datatype or parser |
---|
323 | n/a | for JSON floats (e.g. decimal.Decimal). |
---|
324 | n/a | |
---|
325 | n/a | ``parse_int``, if specified, will be called with the string |
---|
326 | n/a | of every JSON int to be decoded. By default this is equivalent to |
---|
327 | n/a | int(num_str). This can be used to use another datatype or parser |
---|
328 | n/a | for JSON integers (e.g. float). |
---|
329 | n/a | |
---|
330 | n/a | ``parse_constant``, if specified, will be called with one of the |
---|
331 | n/a | following strings: -Infinity, Infinity, NaN. |
---|
332 | n/a | This can be used to raise an exception if invalid JSON numbers |
---|
333 | n/a | are encountered. |
---|
334 | n/a | |
---|
335 | n/a | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
---|
336 | n/a | kwarg; otherwise ``JSONDecoder`` is used. |
---|
337 | n/a | |
---|
338 | n/a | The ``encoding`` argument is ignored and deprecated. |
---|
339 | n/a | |
---|
340 | n/a | """ |
---|
341 | n/a | if isinstance(s, str): |
---|
342 | n/a | if s.startswith('\ufeff'): |
---|
343 | n/a | raise JSONDecodeError("Unexpected UTF-8 BOM (decode using utf-8-sig)", |
---|
344 | n/a | s, 0) |
---|
345 | n/a | else: |
---|
346 | n/a | if not isinstance(s, (bytes, bytearray)): |
---|
347 | n/a | raise TypeError('the JSON object must be str, bytes or bytearray, ' |
---|
348 | n/a | 'not {!r}'.format(s.__class__.__name__)) |
---|
349 | n/a | s = s.decode(detect_encoding(s), 'surrogatepass') |
---|
350 | n/a | |
---|
351 | n/a | if (cls is None and object_hook is None and |
---|
352 | n/a | parse_int is None and parse_float is None and |
---|
353 | n/a | parse_constant is None and object_pairs_hook is None and not kw): |
---|
354 | n/a | return _default_decoder.decode(s) |
---|
355 | n/a | if cls is None: |
---|
356 | n/a | cls = JSONDecoder |
---|
357 | n/a | if object_hook is not None: |
---|
358 | n/a | kw['object_hook'] = object_hook |
---|
359 | n/a | if object_pairs_hook is not None: |
---|
360 | n/a | kw['object_pairs_hook'] = object_pairs_hook |
---|
361 | n/a | if parse_float is not None: |
---|
362 | n/a | kw['parse_float'] = parse_float |
---|
363 | n/a | if parse_int is not None: |
---|
364 | n/a | kw['parse_int'] = parse_int |
---|
365 | n/a | if parse_constant is not None: |
---|
366 | n/a | kw['parse_constant'] = parse_constant |
---|
367 | n/a | return cls(**kw).decode(s) |
---|