__init__.py 14 KB

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