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