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