/Doc/library/json.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 397 lines · 297 code · 100 blank · 0 comment · 0 complexity · ffdebdb463ffd473739a8d62684b22b3 MD5 · raw file

  1. :mod:`json` --- JSON encoder and decoder
  2. ========================================
  3. .. module:: json
  4. :synopsis: Encode and decode the JSON format.
  5. .. moduleauthor:: Bob Ippolito <bob@redivi.com>
  6. .. sectionauthor:: Bob Ippolito <bob@redivi.com>
  7. .. versionadded:: 2.6
  8. JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript
  9. syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
  10. :mod:`json` exposes an API familiar to users of the standard library
  11. :mod:`marshal` and :mod:`pickle` modules.
  12. Encoding basic Python object hierarchies::
  13. >>> import json
  14. >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
  15. '["foo", {"bar": ["baz", null, 1.0, 2]}]'
  16. >>> print json.dumps("\"foo\bar")
  17. "\"foo\bar"
  18. >>> print json.dumps(u'\u1234')
  19. "\u1234"
  20. >>> print json.dumps('\\')
  21. "\\"
  22. >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
  23. {"a": 0, "b": 0, "c": 0}
  24. >>> from StringIO import StringIO
  25. >>> io = StringIO()
  26. >>> json.dump(['streaming API'], io)
  27. >>> io.getvalue()
  28. '["streaming API"]'
  29. Compact encoding::
  30. >>> import json
  31. >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
  32. '[1,2,3,{"4":5,"6":7}]'
  33. Pretty printing::
  34. >>> import json
  35. >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
  36. {
  37. "4": 5,
  38. "6": 7
  39. }
  40. Decoding JSON::
  41. >>> import json
  42. >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
  43. [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
  44. >>> json.loads('"\\"foo\\bar"')
  45. u'"foo\x08ar'
  46. >>> from StringIO import StringIO
  47. >>> io = StringIO('["streaming API"]')
  48. >>> json.load(io)
  49. [u'streaming API']
  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. >>> import decimal
  61. >>> json.loads('1.1', parse_float=decimal.Decimal)
  62. Decimal('1.1')
  63. Extending :class:`JSONEncoder`::
  64. >>> import json
  65. >>> class ComplexEncoder(json.JSONEncoder):
  66. ... def default(self, obj):
  67. ... if isinstance(obj, complex):
  68. ... return [obj.real, obj.imag]
  69. ... return json.JSONEncoder.default(self, obj)
  70. ...
  71. >>> dumps(2 + 1j, cls=ComplexEncoder)
  72. '[2.0, 1.0]'
  73. >>> ComplexEncoder().encode(2 + 1j)
  74. '[2.0, 1.0]'
  75. >>> list(ComplexEncoder().iterencode(2 + 1j))
  76. ['[', '2.0', ', ', '1.0', ']']
  77. .. highlight:: none
  78. Using json.tool from the shell to validate and pretty-print::
  79. $ echo '{"json":"obj"}' | python -mjson.tool
  80. {
  81. "json": "obj"
  82. }
  83. $ echo '{ 1.2:3.4}' | python -mjson.tool
  84. Expecting property name: line 1 column 2 (char 2)
  85. .. highlight:: python
  86. .. note::
  87. The JSON produced by this module's default settings is a subset of
  88. YAML, so it may be used as a serializer for that as well.
  89. Basic Usage
  90. -----------
  91. .. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
  92. Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
  93. file-like object).
  94. If *skipkeys* is ``True`` (default: ``False``), then dict keys that are not
  95. of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`,
  96. :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a
  97. :exc:`TypeError`.
  98. If *ensure_ascii* is ``False`` (default: ``True``), then some chunks written
  99. to *fp* may be :class:`unicode` instances, subject to normal Python
  100. :class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()``
  101. explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this
  102. is likely to cause an error.
  103. If *check_circular* is ``False`` (default: ``True``), then the circular
  104. reference check for container types will be skipped and a circular reference
  105. will result in an :exc:`OverflowError` (or worse).
  106. If *allow_nan* is ``False`` (default: ``True``), then it will be a
  107. :exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
  108. ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of
  109. using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  110. If *indent* is a non-negative integer, then JSON array elements and object
  111. members will be pretty-printed with that indent level. An indent level of 0
  112. will only insert newlines. ``None`` (the default) selects the most compact
  113. representation.
  114. If *separators* is an ``(item_separator, dict_separator)`` tuple, then it
  115. will be used instead of the default ``(', ', ': ')`` separators. ``(',',
  116. ':')`` is the most compact JSON representation.
  117. *encoding* is the character encoding for str instances, default is UTF-8.
  118. *default(obj)* is a function that should return a serializable version of
  119. *obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
  120. To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
  121. :meth:`default` method to serialize additional types), specify it with the
  122. *cls* kwarg.
  123. .. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])
  124. Serialize *obj* to a JSON formatted :class:`str`.
  125. If *ensure_ascii* is ``False``, then the return value will be a
  126. :class:`unicode` instance. The other arguments have the same meaning as in
  127. :func:`dump`.
  128. .. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, **kw]]]]]]])
  129. Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON
  130. document) to a Python object.
  131. If the contents of *fp* are encoded with an ASCII based encoding other than
  132. UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified.
  133. Encodings that are not ASCII based (such as UCS-2) are not allowed, and
  134. should be wrapped with ``codecs.getreader(encoding)(fp)``, or simply decoded
  135. to a :class:`unicode` object and passed to :func:`loads`.
  136. *object_hook* is an optional function that will be called with the result of
  137. any object literal decode (a :class:`dict`). The return value of
  138. *object_hook* will be used instead of the :class:`dict`. This feature can be used
  139. to implement custom decoders (e.g. JSON-RPC class hinting).
  140. *parse_float*, if specified, will be called with the string of every JSON
  141. float to be decoded. By default, this is equivalent to ``float(num_str)``.
  142. This can be used to use another datatype or parser for JSON floats
  143. (e.g. :class:`decimal.Decimal`).
  144. *parse_int*, if specified, will be called with the string of every JSON int
  145. to be decoded. By default, this is equivalent to ``int(num_str)``. This can
  146. be used to use another datatype or parser for JSON integers
  147. (e.g. :class:`float`).
  148. *parse_constant*, if specified, will be called with one of the following
  149. strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
  150. ``'false'``. This can be used to raise an exception if invalid JSON numbers
  151. are encountered.
  152. To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls``
  153. kwarg. Additional keyword arguments will be passed to the constructor of the
  154. class.
  155. .. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, **kw]]]]]]])
  156. Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON
  157. document) to a Python object.
  158. If *s* is a :class:`str` instance and is encoded with an ASCII based encoding
  159. other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be
  160. specified. Encodings that are not ASCII based (such as UCS-2) are not
  161. allowed and should be decoded to :class:`unicode` first.
  162. The other arguments have the same meaning as in :func:`dump`.
  163. Encoders and decoders
  164. ---------------------
  165. .. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict]]]]]])
  166. Simple JSON decoder.
  167. Performs the following translations in decoding by default:
  168. +---------------+-------------------+
  169. | JSON | Python |
  170. +===============+===================+
  171. | object | dict |
  172. +---------------+-------------------+
  173. | array | list |
  174. +---------------+-------------------+
  175. | string | unicode |
  176. +---------------+-------------------+
  177. | number (int) | int, long |
  178. +---------------+-------------------+
  179. | number (real) | float |
  180. +---------------+-------------------+
  181. | true | True |
  182. +---------------+-------------------+
  183. | false | False |
  184. +---------------+-------------------+
  185. | null | None |
  186. +---------------+-------------------+
  187. It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their
  188. corresponding ``float`` values, which is outside the JSON spec.
  189. *encoding* determines the encoding used to interpret any :class:`str` objects
  190. decoded by this instance (UTF-8 by default). It has no effect when decoding
  191. :class:`unicode` objects.
  192. Note that currently only encodings that are a superset of ASCII work, strings
  193. of other encodings should be passed in as :class:`unicode`.
  194. *object_hook*, if specified, will be called with the result of every JSON
  195. object decoded and its return value will be used in place of the given
  196. :class:`dict`. This can be used to provide custom deserializations (e.g. to
  197. support JSON-RPC class hinting).
  198. *parse_float*, if specified, will be called with the string of every JSON
  199. float to be decoded. By default, this is equivalent to ``float(num_str)``.
  200. This can be used to use another datatype or parser for JSON floats
  201. (e.g. :class:`decimal.Decimal`).
  202. *parse_int*, if specified, will be called with the string of every JSON int
  203. to be decoded. By default, this is equivalent to ``int(num_str)``. This can
  204. be used to use another datatype or parser for JSON integers
  205. (e.g. :class:`float`).
  206. *parse_constant*, if specified, will be called with one of the following
  207. strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``, ``'null'``, ``'true'``,
  208. ``'false'``. This can be used to raise an exception if invalid JSON numbers
  209. are encountered.
  210. .. method:: decode(s)
  211. Return the Python representation of *s* (a :class:`str` or
  212. :class:`unicode` instance containing a JSON document)
  213. .. method:: raw_decode(s)
  214. Decode a JSON document from *s* (a :class:`str` or :class:`unicode`
  215. beginning with a JSON document) and return a 2-tuple of the Python
  216. representation and the index in *s* where the document ended.
  217. This can be used to decode a JSON document from a string that may have
  218. extraneous data at the end.
  219. .. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]])
  220. Extensible JSON encoder for Python data structures.
  221. Supports the following objects and types by default:
  222. +-------------------+---------------+
  223. | Python | JSON |
  224. +===================+===============+
  225. | dict | object |
  226. +-------------------+---------------+
  227. | list, tuple | array |
  228. +-------------------+---------------+
  229. | str, unicode | string |
  230. +-------------------+---------------+
  231. | int, long, float | number |
  232. +-------------------+---------------+
  233. | True | true |
  234. +-------------------+---------------+
  235. | False | false |
  236. +-------------------+---------------+
  237. | None | null |
  238. +-------------------+---------------+
  239. To extend this to recognize other objects, subclass and implement a
  240. :meth:`default` method with another method that returns a serializable object
  241. for ``o`` if possible, otherwise it should call the superclass implementation
  242. (to raise :exc:`TypeError`).
  243. If *skipkeys* is ``False`` (the default), then it is a :exc:`TypeError` to
  244. attempt encoding of keys that are not str, int, long, float or None. If
  245. *skipkeys* is ``True``, such items are simply skipped.
  246. If *ensure_ascii* is ``True`` (the default), the output is guaranteed to be
  247. :class:`str` objects with all incoming unicode characters escaped. If
  248. *ensure_ascii* is ``False``, the output will be a unicode object.
  249. If *check_circular* is ``True`` (the default), then lists, dicts, and custom
  250. encoded objects will be checked for circular references during encoding to
  251. prevent an infinite recursion (which would cause an :exc:`OverflowError`).
  252. Otherwise, no such check takes place.
  253. If *allow_nan* is ``True`` (the default), then ``NaN``, ``Infinity``, and
  254. ``-Infinity`` will be encoded as such. This behavior is not JSON
  255. specification compliant, but is consistent with most JavaScript based
  256. encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
  257. such floats.
  258. If *sort_keys* is ``True`` (the default), then the output of dictionaries
  259. will be sorted by key; this is useful for regression tests to ensure that
  260. JSON serializations can be compared on a day-to-day basis.
  261. If *indent* is a non-negative integer (it is ``None`` by default), then JSON
  262. array elements and object members will be pretty-printed with that indent
  263. level. An indent level of 0 will only insert newlines. ``None`` is the most
  264. compact representation.
  265. If specified, *separators* should be an ``(item_separator, key_separator)``
  266. tuple. The default is ``(', ', ': ')``. To get the most compact JSON
  267. representation, you should specify ``(',', ':')`` to eliminate whitespace.
  268. If specified, *default* is a function that gets called for objects that can't
  269. otherwise be serialized. It should return a JSON encodable version of the
  270. object or raise a :exc:`TypeError`.
  271. If *encoding* is not ``None``, then all input strings will be transformed
  272. into unicode using that encoding prior to JSON-encoding. The default is
  273. UTF-8.
  274. .. method:: default(o)
  275. Implement this method in a subclass such that it returns a serializable
  276. object for *o*, or calls the base implementation (to raise a
  277. :exc:`TypeError`).
  278. For example, to support arbitrary iterators, you could implement default
  279. like this::
  280. def default(self, o):
  281. try:
  282. iterable = iter(o)
  283. except TypeError:
  284. pass
  285. else:
  286. return list(iterable)
  287. return JSONEncoder.default(self, o)
  288. .. method:: encode(o)
  289. Return a JSON string representation of a Python data structure, *o*. For
  290. example::
  291. >>> JSONEncoder().encode({"foo": ["bar", "baz"]})
  292. '{"foo": ["bar", "baz"]}'
  293. .. method:: iterencode(o)
  294. Encode the given object, *o*, and yield each string representation as
  295. available. For example::
  296. for chunk in JSONEncoder().iterencode(bigobject):
  297. mysocket.write(chunk)