__init__.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. r"""
  2. A simple, fast, extensible JSON encoder and decoder
  3. JSON (JavaScript Object Notation) <http://json.org> is a subset of
  4. JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
  5. interchange format.
  6. simplejson exposes an API familiar to uses of the standard library
  7. marshal and pickle modules.
  8. Encoding basic Python object hierarchies::
  9. >>> import simplejson
  10. >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
  11. '["foo", {"bar": ["baz", null, 1.0, 2]}]'
  12. >>> print simplejson.dumps("\"foo\bar")
  13. "\"foo\bar"
  14. >>> print simplejson.dumps(u'\u1234')
  15. "\u1234"
  16. >>> print simplejson.dumps('\\')
  17. "\\"
  18. >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
  19. {"a": 0, "b": 0, "c": 0}
  20. >>> from StringIO import StringIO
  21. >>> io = StringIO()
  22. >>> simplejson.dump(['streaming API'], io)
  23. >>> io.getvalue()
  24. '["streaming API"]'
  25. Compact encoding::
  26. >>> import simplejson
  27. >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
  28. '[1,2,3,{"4":5,"6":7}]'
  29. Pretty printing::
  30. >>> import simplejson
  31. >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
  32. {
  33. "4": 5,
  34. "6": 7
  35. }
  36. Decoding JSON::
  37. >>> import simplejson
  38. >>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
  39. [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
  40. >>> simplejson.loads('"\\"foo\\bar"')
  41. u'"foo\x08ar'
  42. >>> from StringIO import StringIO
  43. >>> io = StringIO('["streaming API"]')
  44. >>> simplejson.load(io)
  45. [u'streaming API']
  46. Specializing JSON object decoding::
  47. >>> import simplejson
  48. >>> def as_complex(dct):
  49. ... if '__complex__' in dct:
  50. ... return complex(dct['real'], dct['imag'])
  51. ... return dct
  52. ...
  53. >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}',
  54. ... object_hook=as_complex)
  55. (1+2j)
  56. >>> import decimal
  57. >>> simplejson.loads('1.1', parse_float=decimal.Decimal)
  58. Decimal("1.1")
  59. Extending JSONEncoder::
  60. >>> import simplejson
  61. >>> class ComplexEncoder(simplejson.JSONEncoder):
  62. ... def default(self, obj):
  63. ... if isinstance(obj, complex):
  64. ... return [obj.real, obj.imag]
  65. ... return simplejson.JSONEncoder.default(self, obj)
  66. ...
  67. >>> dumps(2 + 1j, cls=ComplexEncoder)
  68. '[2.0, 1.0]'
  69. >>> ComplexEncoder().encode(2 + 1j)
  70. '[2.0, 1.0]'
  71. >>> list(ComplexEncoder().iterencode(2 + 1j))
  72. ['[', '2.0', ', ', '1.0', ']']
  73. Using simplejson from the shell to validate and
  74. pretty-print::
  75. $ echo '{"json":"obj"}' | python -msimplejson.tool
  76. {
  77. "json": "obj"
  78. }
  79. $ echo '{ 1.2:3.4}' | python -msimplejson.tool
  80. Expecting property name: line 1 column 2 (char 2)
  81. Note that the JSON produced by this module's default settings
  82. is a subset of YAML, so it may be used as a serializer for that as well.
  83. """
  84. __version__ = '1.9.2'
  85. __all__ = [
  86. 'dump', 'dumps', 'load', 'loads',
  87. 'JSONDecoder', 'JSONEncoder',
  88. ]
  89. if __name__ == '__main__':
  90. import warnings
  91. warnings.warn('python -msimplejson is deprecated, use python -msiplejson.tool', DeprecationWarning)
  92. from simplejson.decoder import JSONDecoder
  93. from simplejson.encoder import JSONEncoder
  94. else:
  95. from decoder import JSONDecoder
  96. from encoder import JSONEncoder
  97. _default_encoder = JSONEncoder(
  98. skipkeys=False,
  99. ensure_ascii=True,
  100. check_circular=True,
  101. allow_nan=True,
  102. indent=None,
  103. separators=None,
  104. encoding='utf-8',
  105. default=None,
  106. )
  107. def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
  108. allow_nan=True, cls=None, indent=None, separators=None,
  109. encoding='utf-8', default=None, **kw):
  110. """
  111. Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
  112. ``.write()``-supporting file-like object).
  113. If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
  114. (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
  115. will be skipped instead of raising a ``TypeError``.
  116. If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``
  117. may be ``unicode`` instances, subject to normal Python ``str`` to
  118. ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
  119. understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
  120. to cause an error.
  121. If ``check_circular`` is ``False``, then the circular reference check
  122. for container types will be skipped and a circular reference will
  123. result in an ``OverflowError`` (or worse).
  124. If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
  125. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
  126. in strict compliance of the JSON specification, instead of using the
  127. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  128. If ``indent`` is a non-negative integer, then JSON array elements and object
  129. members will be pretty-printed with that indent level. An indent level
  130. of 0 will only insert newlines. ``None`` is the most compact representation.
  131. If ``separators`` is an ``(item_separator, dict_separator)`` tuple
  132. then it will be used instead of the default ``(', ', ': ')`` separators.
  133. ``(',', ':')`` is the most compact JSON representation.
  134. ``encoding`` is the character encoding for str instances, default is UTF-8.
  135. ``default(obj)`` is a function that should return a serializable version
  136. of obj or raise TypeError. The default simply raises TypeError.
  137. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  138. ``.default()`` method to serialize additional types), specify it with
  139. the ``cls`` kwarg.
  140. """
  141. # cached encoder
  142. if (skipkeys is False and ensure_ascii is True and
  143. check_circular is True and allow_nan is True and
  144. cls is None and indent is None and separators is None and
  145. encoding == 'utf-8' and default is None and not kw):
  146. iterable = _default_encoder.iterencode(obj)
  147. else:
  148. if cls is None:
  149. cls = JSONEncoder
  150. iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
  151. check_circular=check_circular, allow_nan=allow_nan, indent=indent,
  152. separators=separators, encoding=encoding,
  153. default=default, **kw).iterencode(obj)
  154. # could accelerate with writelines in some versions of Python, at
  155. # a debuggability cost
  156. for chunk in iterable:
  157. fp.write(chunk)
  158. def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
  159. allow_nan=True, cls=None, indent=None, separators=None,
  160. encoding='utf-8', default=None, **kw):
  161. """
  162. Serialize ``obj`` to a JSON formatted ``str``.
  163. If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
  164. (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
  165. will be skipped instead of raising a ``TypeError``.
  166. If ``ensure_ascii`` is ``False``, then the return value will be a
  167. ``unicode`` instance subject to normal Python ``str`` to ``unicode``
  168. coercion rules instead of being escaped to an ASCII ``str``.
  169. If ``check_circular`` is ``False``, then the circular reference check
  170. for container types will be skipped and a circular reference will
  171. result in an ``OverflowError`` (or worse).
  172. If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
  173. serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
  174. strict compliance of the JSON specification, instead of using the
  175. JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
  176. If ``indent`` is a non-negative integer, then JSON array elements and
  177. object members will be pretty-printed with that indent level. An indent
  178. level of 0 will only insert newlines. ``None`` is the most compact
  179. representation.
  180. If ``separators`` is an ``(item_separator, dict_separator)`` tuple
  181. then it will be used instead of the default ``(', ', ': ')`` separators.
  182. ``(',', ':')`` is the most compact JSON representation.
  183. ``encoding`` is the character encoding for str instances, default is UTF-8.
  184. ``default(obj)`` is a function that should return a serializable version
  185. of obj or raise TypeError. The default simply raises TypeError.
  186. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
  187. ``.default()`` method to serialize additional types), specify it with
  188. the ``cls`` kwarg.
  189. """
  190. # cached encoder
  191. if (skipkeys is False and ensure_ascii is True and
  192. check_circular is True and allow_nan is True and
  193. cls is None and indent is None and separators is None and
  194. encoding == 'utf-8' and default is None and not kw):
  195. return _default_encoder.encode(obj)
  196. if cls is None:
  197. cls = JSONEncoder
  198. return cls(
  199. skipkeys=skipkeys, ensure_ascii=ensure_ascii,
  200. check_circular=check_circular, allow_nan=allow_nan, indent=indent,
  201. separators=separators, encoding=encoding, default=default,
  202. **kw).encode(obj)
  203. _default_decoder = JSONDecoder(encoding=None, object_hook=None)
  204. def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
  205. parse_int=None, parse_constant=None, **kw):
  206. """
  207. Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
  208. a JSON document) to a Python object.
  209. If the contents of ``fp`` is encoded with an ASCII based encoding other
  210. than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must
  211. be specified. Encodings that are not ASCII based (such as UCS-2) are
  212. not allowed, and should be wrapped with
  213. ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``
  214. object and passed to ``loads()``
  215. ``object_hook`` is an optional function that will be called with the
  216. result of any object literal decode (a ``dict``). The return value of
  217. ``object_hook`` will be used instead of the ``dict``. This feature
  218. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  219. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  220. kwarg.
  221. """
  222. return loads(fp.read(),
  223. encoding=encoding, cls=cls, object_hook=object_hook,
  224. parse_float=parse_float, parse_int=parse_int,
  225. parse_constant=parse_constant, **kw)
  226. def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
  227. parse_int=None, parse_constant=None, **kw):
  228. """
  229. Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
  230. document) to a Python object.
  231. If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding
  232. other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name
  233. must be specified. Encodings that are not ASCII based (such as UCS-2)
  234. are not allowed and should be decoded to ``unicode`` first.
  235. ``object_hook`` is an optional function that will be called with the
  236. result of any object literal decode (a ``dict``). The return value of
  237. ``object_hook`` will be used instead of the ``dict``. This feature
  238. can be used to implement custom decoders (e.g. JSON-RPC class hinting).
  239. ``parse_float``, if specified, will be called with the string
  240. of every JSON float to be decoded. By default this is equivalent to
  241. float(num_str). This can be used to use another datatype or parser
  242. for JSON floats (e.g. decimal.Decimal).
  243. ``parse_int``, if specified, will be called with the string
  244. of every JSON int to be decoded. By default this is equivalent to
  245. int(num_str). This can be used to use another datatype or parser
  246. for JSON integers (e.g. float).
  247. ``parse_constant``, if specified, will be called with one of the
  248. following strings: -Infinity, Infinity, NaN, null, true, false.
  249. This can be used to raise an exception if invalid JSON numbers
  250. are encountered.
  251. To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
  252. kwarg.
  253. """
  254. if (cls is None and encoding is None and object_hook is None and
  255. parse_int is None and parse_float is None and
  256. parse_constant is None and not kw):
  257. return _default_decoder.decode(s)
  258. if cls is None:
  259. cls = JSONDecoder
  260. if object_hook is not None:
  261. kw['object_hook'] = object_hook
  262. if parse_float is not None:
  263. kw['parse_float'] = parse_float
  264. if parse_int is not None:
  265. kw['parse_int'] = parse_int
  266. if parse_constant is not None:
  267. kw['parse_constant'] = parse_constant
  268. return cls(encoding=encoding, **kw).decode(s)
  269. #
  270. # Compatibility cruft from other libraries
  271. #
  272. def decode(s):
  273. """
  274. demjson, python-cjson API compatibility hook. Use loads(s) instead.
  275. """
  276. import warnings
  277. warnings.warn("simplejson.loads(s) should be used instead of decode(s)",
  278. DeprecationWarning)
  279. return loads(s)
  280. def encode(obj):
  281. """
  282. demjson, python-cjson compatibility hook. Use dumps(s) instead.
  283. """
  284. import warnings
  285. warnings.warn("simplejson.dumps(s) should be used instead of encode(s)",
  286. DeprecationWarning)
  287. return dumps(obj)
  288. def read(s):
  289. """
  290. jsonlib, JsonUtils, python-json, json-py API compatibility hook.
  291. Use loads(s) instead.
  292. """
  293. import warnings
  294. warnings.warn("simplejson.loads(s) should be used instead of read(s)",
  295. DeprecationWarning)
  296. return loads(s)
  297. def write(obj):
  298. """
  299. jsonlib, JsonUtils, python-json, json-py API compatibility hook.
  300. Use dumps(s) instead.
  301. """
  302. import warnings
  303. warnings.warn("simplejson.dumps(s) should be used instead of write(s)",
  304. DeprecationWarning)
  305. return dumps(obj)
  306. if __name__ == '__main__':
  307. import simplejson.tool
  308. simplejson.tool.main()