commands: Fix a bug in the amend interaction
[git-cola.git] / simplejson / __init__.py
blobb67e77f987e339e47e157580797365f2e014ca24
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.
5 :mod:`simplejson` exposes an API familiar to users of the standard library
6 :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
7 version of the :mod:`json` library contained in Python 2.6, but maintains
8 compatibility with Python 2.4 and Python 2.5 and (currently) has
9 significant performance advantages, even without using the optional C
10 extension for speedups.
12 Encoding basic Python object hierarchies::
14 >>> import simplejson as json
15 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
16 '["foo", {"bar": ["baz", null, 1.0, 2]}]'
17 >>> print json.dumps("\"foo\bar")
18 "\"foo\bar"
19 >>> print json.dumps(u'\u1234')
20 "\u1234"
21 >>> print json.dumps('\\')
22 "\\"
23 >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
24 {"a": 0, "b": 0, "c": 0}
25 >>> from StringIO import StringIO
26 >>> io = StringIO()
27 >>> json.dump(['streaming API'], io)
28 >>> io.getvalue()
29 '["streaming API"]'
31 Compact encoding::
33 >>> import simplejson as json
34 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
35 '[1,2,3,{"4":5,"6":7}]'
37 Pretty printing::
39 >>> import simplejson as json
40 >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
41 >>> print '\n'.join([l.rstrip() for l in s.splitlines()])
43 "4": 5,
44 "6": 7
47 Decoding JSON::
49 >>> import simplejson as json
50 >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
51 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
52 True
53 >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
54 True
55 >>> from StringIO import StringIO
56 >>> io = StringIO('["streaming API"]')
57 >>> json.load(io)[0] == 'streaming API'
58 True
60 Specializing JSON object decoding::
62 >>> import simplejson as json
63 >>> def as_complex(dct):
64 ... if '__complex__' in dct:
65 ... return complex(dct['real'], dct['imag'])
66 ... return dct
67 ...
68 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
69 ... object_hook=as_complex)
70 (1+2j)
71 >>> from decimal import Decimal
72 >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1')
73 True
75 Specializing JSON object encoding::
77 >>> import simplejson as json
78 >>> def encode_complex(obj):
79 ... if isinstance(obj, complex):
80 ... return [obj.real, obj.imag]
81 ... raise TypeError(repr(o) + " is not JSON serializable")
82 ...
83 >>> json.dumps(2 + 1j, default=encode_complex)
84 '[2.0, 1.0]'
85 >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
86 '[2.0, 1.0]'
87 >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
88 '[2.0, 1.0]'
91 Using simplejson.tool from the shell to validate and pretty-print::
93 $ echo '{"json":"obj"}' | python -m simplejson.tool
95 "json": "obj"
97 $ echo '{ 1.2:3.4}' | python -m simplejson.tool
98 Expecting property name: line 1 column 2 (char 2)
99 """
100 __version__ = '2.1.0'
101 __all__ = [
102 'dump', 'dumps', 'load', 'loads',
103 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
104 'OrderedDict',
107 __author__ = 'Bob Ippolito <bob@redivi.com>'
109 from decoder import JSONDecoder, JSONDecodeError
110 from encoder import JSONEncoder
111 try:
112 from collections import OrderedDict
113 except ImportError:
114 from ordered_dict import OrderedDict
116 _default_encoder = JSONEncoder(
117 skipkeys=False,
118 ensure_ascii=True,
119 check_circular=True,
120 allow_nan=True,
121 indent=None,
122 separators=None,
123 encoding='utf-8',
124 default=None,
127 def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
128 allow_nan=True, cls=None, indent=None, separators=None,
129 encoding='utf-8', default=None, **kw):
130 """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
131 ``.write()``-supporting file-like object).
133 If ``skipkeys`` is true then ``dict`` keys that are not basic types
134 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
135 will be skipped instead of raising a ``TypeError``.
137 If ``ensure_ascii`` is false, then the some chunks written to ``fp``
138 may be ``unicode`` instances, subject to normal Python ``str`` to
139 ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
140 understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
141 to cause an error.
143 If ``check_circular`` is false, then the circular reference check
144 for container types will be skipped and a circular reference will
145 result in an ``OverflowError`` (or worse).
147 If ``allow_nan`` is false, then it will be a ``ValueError`` to
148 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
149 in strict compliance of the JSON specification, instead of using the
150 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
152 If ``indent`` is a non-negative integer, then JSON array elements and
153 object members will be pretty-printed with that indent level. An indent
154 level of 0 will only insert newlines. ``None`` is the most compact
155 representation.
157 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
158 then it will be used instead of the default ``(', ', ': ')`` separators.
159 ``(',', ':')`` is the most compact JSON representation.
161 ``encoding`` is the character encoding for str instances, default is UTF-8.
163 ``default(obj)`` is a function that should return a serializable version
164 of obj or raise TypeError. The default simply raises TypeError.
166 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
167 ``.default()`` method to serialize additional types), specify it with
168 the ``cls`` kwarg.
171 # cached encoder
172 if (not skipkeys and ensure_ascii and
173 check_circular and allow_nan and
174 cls is None and indent is None and separators is None and
175 encoding == 'utf-8' and default is None and not kw):
176 iterable = _default_encoder.iterencode(obj)
177 else:
178 if cls is None:
179 cls = JSONEncoder
180 iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
181 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
182 separators=separators, encoding=encoding,
183 default=default, **kw).iterencode(obj)
184 # could accelerate with writelines in some versions of Python, at
185 # a debuggability cost
186 for chunk in iterable:
187 fp.write(chunk)
190 def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
191 allow_nan=True, cls=None, indent=None, separators=None,
192 encoding='utf-8', default=None, **kw):
193 """Serialize ``obj`` to a JSON formatted ``str``.
195 If ``skipkeys`` is false then ``dict`` keys that are not basic types
196 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
197 will be skipped instead of raising a ``TypeError``.
199 If ``ensure_ascii`` is false, then the return value will be a
200 ``unicode`` instance subject to normal Python ``str`` to ``unicode``
201 coercion rules instead of being escaped to an ASCII ``str``.
203 If ``check_circular`` is false, then the circular reference check
204 for container types will be skipped and a circular reference will
205 result in an ``OverflowError`` (or worse).
207 If ``allow_nan`` is false, then it will be a ``ValueError`` to
208 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
209 strict compliance of the JSON specification, instead of using the
210 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
212 If ``indent`` is a non-negative integer, then JSON array elements and
213 object members will be pretty-printed with that indent level. An indent
214 level of 0 will only insert newlines. ``None`` is the most compact
215 representation.
217 If ``separators`` is an ``(item_separator, dict_separator)`` tuple
218 then it will be used instead of the default ``(', ', ': ')`` separators.
219 ``(',', ':')`` is the most compact JSON representation.
221 ``encoding`` is the character encoding for str instances, default is UTF-8.
223 ``default(obj)`` is a function that should return a serializable version
224 of obj or raise TypeError. The default simply raises TypeError.
226 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
227 ``.default()`` method to serialize additional types), specify it with
228 the ``cls`` kwarg.
231 # cached encoder
232 if (not skipkeys and ensure_ascii and
233 check_circular and allow_nan and
234 cls is None and indent is None and separators is None and
235 encoding == 'utf-8' and default is None and not kw):
236 return _default_encoder.encode(obj)
237 if cls is None:
238 cls = JSONEncoder
239 return cls(
240 skipkeys=skipkeys, ensure_ascii=ensure_ascii,
241 check_circular=check_circular, allow_nan=allow_nan, indent=indent,
242 separators=separators, encoding=encoding, default=default,
243 **kw).encode(obj)
246 _default_decoder = JSONDecoder(encoding=None, object_hook=None,
247 object_pairs_hook=None)
250 def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
251 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
252 """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
253 a JSON document) to a Python object.
255 *encoding* determines the encoding used to interpret any
256 :class:`str` objects decoded by this instance (``'utf-8'`` by
257 default). It has no effect when decoding :class:`unicode` objects.
259 Note that currently only encodings that are a superset of ASCII work,
260 strings of other encodings should be passed in as :class:`unicode`.
262 *object_hook*, if specified, will be called with the result of every
263 JSON object decoded and its return value will be used in place of the
264 given :class:`dict`. This can be used to provide custom
265 deserializations (e.g. to support JSON-RPC class hinting).
267 *object_pairs_hook* is an optional function that will be called with
268 the result of any object literal decode with an ordered list of pairs.
269 The return value of *object_pairs_hook* will be used instead of the
270 :class:`dict`. This feature can be used to implement custom decoders
271 that rely on the order that the key and value pairs are decoded (for
272 example, :func:`collections.OrderedDict` will remember the order of
273 insertion). If *object_hook* is also defined, the *object_pairs_hook*
274 takes priority.
276 *parse_float*, if specified, will be called with the string of every
277 JSON float to be decoded. By default, this is equivalent to
278 ``float(num_str)``. This can be used to use another datatype or parser
279 for JSON floats (e.g. :class:`decimal.Decimal`).
281 *parse_int*, if specified, will be called with the string of every
282 JSON int to be decoded. By default, this is equivalent to
283 ``int(num_str)``. This can be used to use another datatype or parser
284 for JSON integers (e.g. :class:`float`).
286 *parse_constant*, if specified, will be called with one of the
287 following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This
288 can be used to raise an exception if invalid JSON numbers are
289 encountered.
291 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
292 kwarg.
295 return loads(fp.read(),
296 encoding=encoding, cls=cls, object_hook=object_hook,
297 parse_float=parse_float, parse_int=parse_int,
298 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
299 **kw)
302 def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
303 parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
304 """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
305 document) to a Python object.
307 *encoding* determines the encoding used to interpret any
308 :class:`str` objects decoded by this instance (``'utf-8'`` by
309 default). It has no effect when decoding :class:`unicode` objects.
311 Note that currently only encodings that are a superset of ASCII work,
312 strings of other encodings should be passed in as :class:`unicode`.
314 *object_hook*, if specified, will be called with the result of every
315 JSON object decoded and its return value will be used in place of the
316 given :class:`dict`. This can be used to provide custom
317 deserializations (e.g. to support JSON-RPC class hinting).
319 *object_pairs_hook* is an optional function that will be called with
320 the result of any object literal decode with an ordered list of pairs.
321 The return value of *object_pairs_hook* will be used instead of the
322 :class:`dict`. This feature can be used to implement custom decoders
323 that rely on the order that the key and value pairs are decoded (for
324 example, :func:`collections.OrderedDict` will remember the order of
325 insertion). If *object_hook* is also defined, the *object_pairs_hook*
326 takes priority.
328 *parse_float*, if specified, will be called with the string of every
329 JSON float to be decoded. By default, this is equivalent to
330 ``float(num_str)``. This can be used to use another datatype or parser
331 for JSON floats (e.g. :class:`decimal.Decimal`).
333 *parse_int*, if specified, will be called with the string of every
334 JSON int to be decoded. By default, this is equivalent to
335 ``int(num_str)``. This can be used to use another datatype or parser
336 for JSON integers (e.g. :class:`float`).
338 *parse_constant*, if specified, will be called with one of the
339 following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This
340 can be used to raise an exception if invalid JSON numbers are
341 encountered.
343 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
344 kwarg.
347 if (cls is None and encoding is None and object_hook is None and
348 parse_int is None and parse_float is None and
349 parse_constant is None and object_pairs_hook is None and not kw):
350 return _default_decoder.decode(s)
351 if cls is None:
352 cls = JSONDecoder
353 if object_hook is not None:
354 kw['object_hook'] = object_hook
355 if object_pairs_hook is not None:
356 kw['object_pairs_hook'] = object_pairs_hook
357 if parse_float is not None:
358 kw['parse_float'] = parse_float
359 if parse_int is not None:
360 kw['parse_int'] = parse_int
361 if parse_constant is not None:
362 kw['parse_constant'] = parse_constant
363 return cls(encoding=encoding, **kw).decode(s)