You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
14 KiB

  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 the externally maintained
  6. version of the :mod:`json` library contained in Python 2.6, but maintains
  7. compatibility with Python 2.4 and Python 2.5 and (currently) has
  8. significant performance advantages, even without using the optional C
  9. extension for speedups.
  10. Encoding basic Python object hierarchies::
  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(u'\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 StringIO import StringIO
  23. >>> io = StringIO()
  24. >>> json.dump(['streaming API'], io)
  25. >>> io.getvalue()
  26. '["streaming API"]'
  27. Compact encoding::
  28. >>> import json
  29. >>> json.dumps([1,2,3,{'4': 5, '6': 7}], sort_keys=True, separators=(',',':'))
  30. '[1,2,3,{"4":5,"6":7}]'
  31. Pretty printing::
  32. >>> import json
  33. >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
  34. >>> print '\n'.join([l.rstrip() for l in s.splitlines()])
  35. {
  36. "4": 5,
  37. "6": 7
  38. }
  39. Decoding JSON::
  40. >>> import json
  41. >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
  42. >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
  43. True
  44. >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
  45. True
  46. >>> from StringIO import StringIO
  47. >>> io = StringIO('["streaming API"]')
  48. >>> json.load(io)[0] == 'streaming API'
  49. True
  50. Specializing JSON object decoding::
  51. >>> import json
  52. >>> def as_complex(dct):
  53. ... if '__complex__' in dct:
  54. ... return complex(dct['real'], dct['imag'])
  55. ... return dct
  56. ...
  57. >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
  58. ... object_hook=as_complex)
  59. (1+2j)
  60. >>> from decimal import Decimal
  61. >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
  62. True
  63. Specializing JSON object encoding::
  64. >>> import json
  65. >>> def encode_complex(obj):
  66. ... if isinstance(obj, complex):
  67. ... return [obj.real, obj.imag]
  68. ... raise TypeError(repr(o) + " is not JSON serializable")
  69. ...
  70. >>> json.dumps(2 + 1j, default=encode_complex)
  71. '[2.0, 1.0]'
  72. >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
  73. '[2.0, 1.0]'
  74. >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
  75. '[2.0, 1.0]'
  76. Using json.tool from the shell to validate and pretty-print::
  77. $ echo '{"json":"obj"}' | python -m json.tool
  78. {
  79. "json": "obj"
  80. }
  81. $ echo '{ 1.2:3.4}' | python -m json.tool
  82. Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
  83. """
  84. __version__ = '2.0.9'
  85. __all__ = [
  86. 'dump', 'dumps', 'load', 'loads',
  87. 'JSONDecoder', 'JSONEncoder',
  88. ]
  89. __author__ = 'Bob Ippolito <bob@redivi.com>'
  90. from .decoder import JSONDecoder
  91. from .encoder import JSONEncoder
  92. _default_encoder = JSONEncoder(
  93. skipkeys=False,
  94. ensure_ascii=True,
  95. check_circular=True,
  96. allow_nan=True,
  97. indent=None,
  98. separators=None,
  99. encoding='utf-8',
  100. default=None,
  101. )
  102. def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
  103. allow_nan=True, cls=None, indent=None, separators=None,
  104. encoding='utf-8', default=None, **kw):
  105. """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
  106. ``.write()``-supporting file-like object).
  107. If ``skipkeys`` is true then ``dict`` keys that are not basic types
  108. (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
  109. will be skipped instead of raising a ``TypeError``.
  110. If ``ensure_ascii`` is false, then the some chunks written to ``fp``
  111. may be ``unicode`` instances, subject to normal Python ``str`` to
  112. ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
  113. understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
  114. to cause an error.
  115. If ``check_circular`` is false, then the circular reference check
  116. for container types will be skipped and a circular reference will
  117. result in an ``OverflowError`` (or worse).
  118. If ``allow_nan`` is false, then it will be a ``ValueError`` to
  119. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
  120. in strict compliance of the JSON specification, instead of using the
  121. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  122. If ``indent`` is a non-negative integer, then JSON array elements and
  123. object members will be pretty-printed with that indent level. An indent
  124. level of 0 will only insert newlines. ``None`` is the most compact
  125. representation.
  126. If ``separators`` is an ``(item_separator, dict_separator)`` tuple
  127. then it will be used instead of the default ``(', ', ': ')`` separators.
  128. ``(',', ':')`` is the most compact JSON representation.
  129. ``encoding`` is the character encoding for str instances, default is UTF-8.
  130. ``default(obj)`` is a function that should return a serializable version
  131. of obj or raise TypeError. The default simply raises TypeError.
  132. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  133. ``.default()`` method to serialize additional types), specify it with
  134. the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
  135. """
  136. # cached encoder
  137. if (not skipkeys and ensure_ascii and
  138. check_circular and allow_nan and
  139. cls is None and indent is None and separators is None and
  140. encoding == 'utf-8' and default is None and not kw):
  141. iterable = _default_encoder.iterencode(obj)
  142. else:
  143. if cls is None:
  144. cls = JSONEncoder
  145. iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
  146. check_circular=check_circular, allow_nan=allow_nan, indent=indent,
  147. separators=separators, encoding=encoding,
  148. default=default, **kw).iterencode(obj)
  149. # could accelerate with writelines in some versions of Python, at
  150. # a debuggability cost
  151. for chunk in iterable:
  152. fp.write(chunk)
  153. def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
  154. allow_nan=True, cls=None, indent=None, separators=None,
  155. encoding='utf-8', default=None, **kw):
  156. """Serialize ``obj`` to a JSON formatted ``str``.
  157. If ``skipkeys`` is false then ``dict`` keys that are not basic types
  158. (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
  159. will be skipped instead of raising a ``TypeError``.
  160. If ``ensure_ascii`` is false, then the return value will be a
  161. ``unicode`` instance subject to normal Python ``str`` to ``unicode``
  162. coercion rules instead of being escaped to an ASCII ``str``.
  163. If ``check_circular`` is false, then the circular reference check
  164. for container types will be skipped and a circular reference will
  165. result in an ``OverflowError`` (or worse).
  166. If ``allow_nan`` is false, then it will be a ``ValueError`` to
  167. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
  168. strict compliance of the JSON specification, instead of using the
  169. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  170. If ``indent`` is a non-negative integer, then JSON array elements and
  171. object members will be pretty-printed with that indent level. An indent
  172. level of 0 will only insert newlines. ``None`` is the most compact
  173. representation.
  174. If ``separators`` is an ``(item_separator, dict_separator)`` tuple
  175. then it will be used instead of the default ``(', ', ': ')`` separators.
  176. ``(',', ':')`` is the most compact JSON representation.
  177. ``encoding`` is the character encoding for str instances, default is UTF-8.
  178. ``default(obj)`` is a function that should return a serializable version
  179. of obj or raise TypeError. The default simply raises TypeError.
  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. encoding == 'utf-8' and default is None 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, encoding=encoding, default=default,
  196. **kw).encode(obj)
  197. _default_decoder = JSONDecoder(encoding=None, object_hook=None,
  198. object_pairs_hook=None)
  199. def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
  200. parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
  201. """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
  202. a JSON document) to a Python object.
  203. If the contents of ``fp`` is encoded with an ASCII based encoding other
  204. than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must
  205. be specified. Encodings that are not ASCII based (such as UCS-2) are
  206. not allowed, and should be wrapped with
  207. ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``
  208. object and passed to ``loads()``
  209. ``object_hook`` is an optional function that will be called with the
  210. result of any object literal decode (a ``dict``). The return value of
  211. ``object_hook`` will be used instead of the ``dict``. This feature
  212. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  213. ``object_pairs_hook`` is an optional function that will be called with the
  214. result of any object literal decoded with an ordered list of pairs. The
  215. return value of ``object_pairs_hook`` will be used instead of the ``dict``.
  216. This feature can be used to implement custom decoders that rely on the
  217. order that the key and value pairs are decoded (for example,
  218. collections.OrderedDict will remember the order of insertion). If
  219. ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
  220. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  221. kwarg; otherwise ``JSONDecoder`` is used.
  222. """
  223. return loads(fp.read(),
  224. encoding=encoding, cls=cls, object_hook=object_hook,
  225. parse_float=parse_float, parse_int=parse_int,
  226. parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
  227. **kw)
  228. def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
  229. parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
  230. """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
  231. document) to a Python object.
  232. If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding
  233. other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name
  234. must be specified. Encodings that are not ASCII based (such as UCS-2)
  235. are not allowed and should be decoded to ``unicode`` first.
  236. ``object_hook`` is an optional function that will be called with the
  237. result of any object literal decode (a ``dict``). The return value of
  238. ``object_hook`` will be used instead of the ``dict``. This feature
  239. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  240. ``object_pairs_hook`` is an optional function that will be called with the
  241. result of any object literal decoded with an ordered list of pairs. The
  242. return value of ``object_pairs_hook`` will be used instead of the ``dict``.
  243. This feature can be used to implement custom decoders that rely on the
  244. order that the key and value pairs are decoded (for example,
  245. collections.OrderedDict will remember the order of insertion). If
  246. ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
  247. ``parse_float``, if specified, will be called with the string
  248. of every JSON float to be decoded. By default this is equivalent to
  249. float(num_str). This can be used to use another datatype or parser
  250. for JSON floats (e.g. decimal.Decimal).
  251. ``parse_int``, if specified, will be called with the string
  252. of every JSON int to be decoded. By default this is equivalent to
  253. int(num_str). This can be used to use another datatype or parser
  254. for JSON integers (e.g. float).
  255. ``parse_constant``, if specified, will be called with one of the
  256. following strings: -Infinity, Infinity, NaN, null, true, false.
  257. This can be used to raise an exception if invalid JSON numbers
  258. are encountered.
  259. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  260. kwarg; otherwise ``JSONDecoder`` is used.
  261. """
  262. if (cls is None and encoding is None and object_hook is None and
  263. parse_int is None and parse_float is None and
  264. parse_constant is None and object_pairs_hook is None and not kw):
  265. return _default_decoder.decode(s)
  266. if cls is None:
  267. cls = JSONDecoder
  268. if object_hook is not None:
  269. kw['object_hook'] = object_hook
  270. if object_pairs_hook is not None:
  271. kw['object_pairs_hook'] = object_pairs_hook
  272. if parse_float is not None:
  273. kw['parse_float'] = parse_float
  274. if parse_int is not None:
  275. kw['parse_int'] = parse_int
  276. if parse_constant is not None:
  277. kw['parse_constant'] = parse_constant
  278. return cls(encoding=encoding, **kw).decode(s)