OLD | NEW |
(Empty) | |
| 1 :mod:`simplejson` --- JSON encoder and decoder |
| 2 ============================================== |
| 3 |
| 4 .. module:: simplejson |
| 5 :synopsis: Encode and decode the JSON format. |
| 6 .. moduleauthor:: Bob Ippolito <bob@redivi.com> |
| 7 .. sectionauthor:: Bob Ippolito <bob@redivi.com> |
| 8 |
| 9 JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript |
| 10 syntax (ECMA-262 3rd edition) used as a lightweight data interchange format. |
| 11 |
| 12 :mod:`simplejson` exposes an API familiar to users of the standard library |
| 13 :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained |
| 14 version of the :mod:`json` library contained in Python 2.6, but maintains |
| 15 compatibility with Python 2.4 and Python 2.5 and (currently) has |
| 16 significant performance advantages, even without using the optional C |
| 17 extension for speedups. |
| 18 |
| 19 Encoding basic Python object hierarchies:: |
| 20 |
| 21 >>> import simplejson as json |
| 22 >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) |
| 23 '["foo", {"bar": ["baz", null, 1.0, 2]}]' |
| 24 >>> print json.dumps("\"foo\bar") |
| 25 "\"foo\bar" |
| 26 >>> print json.dumps(u'\u1234') |
| 27 "\u1234" |
| 28 >>> print json.dumps('\\') |
| 29 "\\" |
| 30 >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) |
| 31 {"a": 0, "b": 0, "c": 0} |
| 32 >>> from StringIO import StringIO |
| 33 >>> io = StringIO() |
| 34 >>> json.dump(['streaming API'], io) |
| 35 >>> io.getvalue() |
| 36 '["streaming API"]' |
| 37 |
| 38 Compact encoding:: |
| 39 |
| 40 >>> import simplejson as json |
| 41 >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) |
| 42 '[1,2,3,{"4":5,"6":7}]' |
| 43 |
| 44 Pretty printing:: |
| 45 |
| 46 >>> import simplejson as json |
| 47 >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) |
| 48 >>> print '\n'.join([l.rstrip() for l in s.splitlines()]) |
| 49 { |
| 50 "4": 5, |
| 51 "6": 7 |
| 52 } |
| 53 |
| 54 Decoding JSON:: |
| 55 |
| 56 >>> import simplejson as json |
| 57 >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] |
| 58 >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj |
| 59 True |
| 60 >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar' |
| 61 True |
| 62 >>> from StringIO import StringIO |
| 63 >>> io = StringIO('["streaming API"]') |
| 64 >>> json.load(io)[0] == 'streaming API' |
| 65 True |
| 66 |
| 67 Specializing JSON object decoding:: |
| 68 |
| 69 >>> import simplejson as json |
| 70 >>> def as_complex(dct): |
| 71 ... if '__complex__' in dct: |
| 72 ... return complex(dct['real'], dct['imag']) |
| 73 ... return dct |
| 74 ... |
| 75 >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', |
| 76 ... object_hook=as_complex) |
| 77 (1+2j) |
| 78 >>> import decimal |
| 79 >>> json.loads('1.1', parse_float=decimal.Decimal) == decimal.Decimal('1.1') |
| 80 True |
| 81 |
| 82 Specializing JSON object encoding:: |
| 83 |
| 84 >>> import simplejson as json |
| 85 >>> def encode_complex(obj): |
| 86 ... if isinstance(obj, complex): |
| 87 ... return [obj.real, obj.imag] |
| 88 ... raise TypeError(repr(o) + " is not JSON serializable") |
| 89 ... |
| 90 >>> json.dumps(2 + 1j, default=encode_complex) |
| 91 '[2.0, 1.0]' |
| 92 >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) |
| 93 '[2.0, 1.0]' |
| 94 >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) |
| 95 '[2.0, 1.0]' |
| 96 |
| 97 |
| 98 .. highlight:: none |
| 99 |
| 100 Using :mod:`simplejson.tool` from the shell to validate and pretty-print:: |
| 101 |
| 102 $ echo '{"json":"obj"}' | python -m simplejson.tool |
| 103 { |
| 104 "json": "obj" |
| 105 } |
| 106 $ echo '{ 1.2:3.4}' | python -m simplejson.tool |
| 107 Expecting property name: line 1 column 2 (char 2) |
| 108 |
| 109 .. highlight:: python |
| 110 |
| 111 .. note:: |
| 112 |
| 113 The JSON produced by this module's default settings is a subset of |
| 114 YAML, so it may be used as a serializer for that as well. |
| 115 |
| 116 |
| 117 Basic Usage |
| 118 ----------- |
| 119 |
| 120 .. function:: dump(obj, fp[, skipkeys[, ensure_ascii[, check_circular[, allow_na
n[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]]) |
| 121 |
| 122 Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting |
| 123 file-like object). |
| 124 |
| 125 If *skipkeys* is true (default: ``False``), then dict keys that are not |
| 126 of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`, |
| 127 :class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a |
| 128 :exc:`TypeError`. |
| 129 |
| 130 If *ensure_ascii* is false (default: ``True``), then some chunks written |
| 131 to *fp* may be :class:`unicode` instances, subject to normal Python |
| 132 :class:`str` to :class:`unicode` coercion rules. Unless ``fp.write()`` |
| 133 explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) this |
| 134 is likely to cause an error. It's best to leave the default settings, because |
| 135 they are safe and it is highly optimized. |
| 136 |
| 137 If *check_circular* is false (default: ``True``), then the circular |
| 138 reference check for container types will be skipped and a circular reference |
| 139 will result in an :exc:`OverflowError` (or worse). |
| 140 |
| 141 If *allow_nan* is false (default: ``True``), then it will be a |
| 142 :exc:`ValueError` to serialize out of range :class:`float` values (``nan``, |
| 143 ``inf``, ``-inf``) in strict compliance of the JSON specification. |
| 144 If *allow_nan* is true, their JavaScript equivalents will be used |
| 145 (``NaN``, ``Infinity``, ``-Infinity``). |
| 146 |
| 147 If *indent* is a non-negative integer, then JSON array elements and object |
| 148 members will be pretty-printed with that indent level. An indent level of 0 |
| 149 will only insert newlines. ``None`` (the default) selects the most compact |
| 150 representation. |
| 151 |
| 152 If specified, *separators* should be an ``(item_separator, dict_separator)`` |
| 153 tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON |
| 154 representation, you should specify ``(',', ':')`` to eliminate whitespace. |
| 155 |
| 156 *encoding* is the character encoding for str instances, default is |
| 157 ``'utf-8'``. |
| 158 |
| 159 If specified, *default* should be a function that gets called for objects |
| 160 that can't otherwise be serialized. It should return a JSON encodable |
| 161 version of the object or raise a :exc:`TypeError`. If not specified, |
| 162 :exc:`TypeError` is always raised in those cases. |
| 163 |
| 164 To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the |
| 165 :meth:`default` method to serialize additional types), specify it with the |
| 166 *cls* kwarg. |
| 167 |
| 168 .. note:: |
| 169 |
| 170 JSON is not a framed protocol so unlike :mod:`pickle` or :mod:`marshal`
it |
| 171 does not make sense to serialize more than one JSON document without som
e |
| 172 container protocol to delimit them. |
| 173 |
| 174 |
| 175 .. function:: dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[,
cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]]) |
| 176 |
| 177 Serialize *obj* to a JSON formatted :class:`str`. |
| 178 |
| 179 If *ensure_ascii* is false, then the return value will be a |
| 180 :class:`unicode` instance. The other arguments have the same meaning as in |
| 181 :func:`dump`. Note that the default *ensure_ascii* setting has much |
| 182 better performance. |
| 183 |
| 184 |
| 185 .. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[,
parse_constant[, **kw]]]]]]]) |
| 186 |
| 187 Deserialize *fp* (a ``.read()``-supporting file-like object containing a JSON |
| 188 document) to a Python object. |
| 189 |
| 190 If the contents of *fp* are encoded with an ASCII based encoding other than |
| 191 UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified. |
| 192 Encodings that are not ASCII based (such as UCS-2) are not allowed, and |
| 193 should be wrapped with ``codecs.getreader(fp)(encoding)``, or simply decoded |
| 194 to a :class:`unicode` object and passed to :func:`loads`. The default |
| 195 setting of ``'utf-8'`` is fastest and should be using whenever possible. |
| 196 |
| 197 *object_hook* is an optional function that will be called with the result of |
| 198 any object literal decode (a :class:`dict`). The return value of |
| 199 *object_hook* will be used instead of the :class:`dict`. This feature can be
used |
| 200 to implement custom decoders (e.g. JSON-RPC class hinting). |
| 201 |
| 202 *parse_float*, if specified, will be called with the string of every JSON |
| 203 float to be decoded. By default, this is equivalent to ``float(num_str)``. |
| 204 This can be used to use another datatype or parser for JSON floats |
| 205 (e.g. :class:`decimal.Decimal`). |
| 206 |
| 207 *parse_int*, if specified, will be called with the string of every JSON int |
| 208 to be decoded. By default, this is equivalent to ``int(num_str)``. This can |
| 209 be used to use another datatype or parser for JSON integers |
| 210 (e.g. :class:`float`). |
| 211 |
| 212 *parse_constant*, if specified, will be called with one of the following |
| 213 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This can be used to |
| 214 raise an exception if invalid JSON numbers are encountered. |
| 215 |
| 216 To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls`` |
| 217 kwarg. Additional keyword arguments will be passed to the constructor of the |
| 218 class. |
| 219 |
| 220 .. note:: |
| 221 |
| 222 :func:`load` will read the rest of the file-like object as a string and |
| 223 then call :func:`loads`. It does not stop at the end of the first valid |
| 224 JSON document it finds and it will raise an error if there is anything |
| 225 other than whitespace after the document. Except for files containing |
| 226 only one JSON document, it is recommended to use :func:`loads`. |
| 227 |
| 228 |
| 229 .. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[,
parse_constant[, **kw]]]]]]]) |
| 230 |
| 231 Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSO
N |
| 232 document) to a Python object. |
| 233 |
| 234 If *s* is a :class:`str` instance and is encoded with an ASCII based encoding |
| 235 other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be |
| 236 specified. Encodings that are not ASCII based (such as UCS-2) are not |
| 237 allowed and should be decoded to :class:`unicode` first. |
| 238 |
| 239 The other arguments have the same meaning as in :func:`load`. |
| 240 |
| 241 |
| 242 Encoders and decoders |
| 243 --------------------- |
| 244 |
| 245 .. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse
_constant[, strict]]]]]]) |
| 246 |
| 247 Simple JSON decoder. |
| 248 |
| 249 Performs the following translations in decoding by default: |
| 250 |
| 251 +---------------+-------------------+ |
| 252 | JSON | Python | |
| 253 +===============+===================+ |
| 254 | object | dict | |
| 255 +---------------+-------------------+ |
| 256 | array | list | |
| 257 +---------------+-------------------+ |
| 258 | string | unicode | |
| 259 +---------------+-------------------+ |
| 260 | number (int) | int, long | |
| 261 +---------------+-------------------+ |
| 262 | number (real) | float | |
| 263 +---------------+-------------------+ |
| 264 | true | True | |
| 265 +---------------+-------------------+ |
| 266 | false | False | |
| 267 +---------------+-------------------+ |
| 268 | null | None | |
| 269 +---------------+-------------------+ |
| 270 |
| 271 It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their |
| 272 corresponding ``float`` values, which is outside the JSON spec. |
| 273 |
| 274 *encoding* determines the encoding used to interpret any :class:`str` objects |
| 275 decoded by this instance (``'utf-8'`` by default). It has no effect when dec
oding |
| 276 :class:`unicode` objects. |
| 277 |
| 278 Note that currently only encodings that are a superset of ASCII work, strings |
| 279 of other encodings should be passed in as :class:`unicode`. |
| 280 |
| 281 *object_hook*, if specified, will be called with the result of every JSON |
| 282 object decoded and its return value will be used in place of the given |
| 283 :class:`dict`. This can be used to provide custom deserializations (e.g. to |
| 284 support JSON-RPC class hinting). |
| 285 |
| 286 *parse_float*, if specified, will be called with the string of every JSON |
| 287 float to be decoded. By default, this is equivalent to ``float(num_str)``. |
| 288 This can be used to use another datatype or parser for JSON floats |
| 289 (e.g. :class:`decimal.Decimal`). |
| 290 |
| 291 *parse_int*, if specified, will be called with the string of every JSON int |
| 292 to be decoded. By default, this is equivalent to ``int(num_str)``. This can |
| 293 be used to use another datatype or parser for JSON integers |
| 294 (e.g. :class:`float`). |
| 295 |
| 296 *parse_constant*, if specified, will be called with one of the following |
| 297 strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This can be used to |
| 298 raise an exception if invalid JSON numbers are encountered. |
| 299 |
| 300 *strict* controls the parser's behavior when it encounters an invalid |
| 301 control character in a string. The default setting of ``True`` means that |
| 302 unescaped control characters are parse errors, if ``False`` then control |
| 303 characters will be allowed in strings. |
| 304 |
| 305 .. method:: decode(s) |
| 306 |
| 307 Return the Python representation of *s* (a :class:`str` or |
| 308 :class:`unicode` instance containing a JSON document) |
| 309 |
| 310 .. method:: raw_decode(s) |
| 311 |
| 312 Decode a JSON document from *s* (a :class:`str` or :class:`unicode` |
| 313 beginning with a JSON document) and return a 2-tuple of the Python |
| 314 representation and the index in *s* where the document ended. |
| 315 |
| 316 This can be used to decode a JSON document from a string that may have |
| 317 extraneous data at the end. |
| 318 |
| 319 |
| 320 .. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, s
ort_keys[, indent[, separators[, encoding[, default]]]]]]]]]) |
| 321 |
| 322 Extensible JSON encoder for Python data structures. |
| 323 |
| 324 Supports the following objects and types by default: |
| 325 |
| 326 +-------------------+---------------+ |
| 327 | Python | JSON | |
| 328 +===================+===============+ |
| 329 | dict | object | |
| 330 +-------------------+---------------+ |
| 331 | list, tuple | array | |
| 332 +-------------------+---------------+ |
| 333 | str, unicode | string | |
| 334 +-------------------+---------------+ |
| 335 | int, long, float | number | |
| 336 +-------------------+---------------+ |
| 337 | True | true | |
| 338 +-------------------+---------------+ |
| 339 | False | false | |
| 340 +-------------------+---------------+ |
| 341 | None | null | |
| 342 +-------------------+---------------+ |
| 343 |
| 344 To extend this to recognize other objects, subclass and implement a |
| 345 :meth:`default` method with another method that returns a serializable object |
| 346 for ``o`` if possible, otherwise it should call the superclass implementation |
| 347 (to raise :exc:`TypeError`). |
| 348 |
| 349 If *skipkeys* is false (the default), then it is a :exc:`TypeError` to |
| 350 attempt encoding of keys that are not str, int, long, float or None. If |
| 351 *skipkeys* is true, such items are simply skipped. |
| 352 |
| 353 If *ensure_ascii* is true (the default), the output is guaranteed to be |
| 354 :class:`str` objects with all incoming unicode characters escaped. If |
| 355 *ensure_ascii* is false, the output will be a unicode object. |
| 356 |
| 357 If *check_circular* is false (the default), then lists, dicts, and custom |
| 358 encoded objects will be checked for circular references during encoding to |
| 359 prevent an infinite recursion (which would cause an :exc:`OverflowError`). |
| 360 Otherwise, no such check takes place. |
| 361 |
| 362 If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and |
| 363 ``-Infinity`` will be encoded as such. This behavior is not JSON |
| 364 specification compliant, but is consistent with most JavaScript based |
| 365 encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode |
| 366 such floats. |
| 367 |
| 368 If *sort_keys* is true (the default), then the output of dictionaries |
| 369 will be sorted by key; this is useful for regression tests to ensure that |
| 370 JSON serializations can be compared on a day-to-day basis. |
| 371 |
| 372 If *indent* is a non-negative integer (it is ``None`` by default), then JSON |
| 373 array elements and object members will be pretty-printed with that indent |
| 374 level. An indent level of 0 will only insert newlines. ``None`` is the most |
| 375 compact representation. |
| 376 |
| 377 If specified, *separators* should be an ``(item_separator, key_separator)`` |
| 378 tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON |
| 379 representation, you should specify ``(',', ':')`` to eliminate whitespace. |
| 380 |
| 381 If specified, *default* should be a function that gets called for objects |
| 382 that can't otherwise be serialized. It should return a JSON encodable |
| 383 version of the object or raise a :exc:`TypeError`. |
| 384 |
| 385 If *encoding* is not ``None``, then all input strings will be transformed |
| 386 into unicode using that encoding prior to JSON-encoding. The default is |
| 387 ``'utf-8'``. |
| 388 |
| 389 |
| 390 .. method:: default(o) |
| 391 |
| 392 Implement this method in a subclass such that it returns a serializable |
| 393 object for *o*, or calls the base implementation (to raise a |
| 394 :exc:`TypeError`). |
| 395 |
| 396 For example, to support arbitrary iterators, you could implement default |
| 397 like this:: |
| 398 |
| 399 def default(self, o): |
| 400 try: |
| 401 iterable = iter(o) |
| 402 except TypeError: |
| 403 pass |
| 404 else: |
| 405 return list(iterable) |
| 406 return JSONEncoder.default(self, o) |
| 407 |
| 408 |
| 409 .. method:: encode(o) |
| 410 |
| 411 Return a JSON string representation of a Python data structure, *o*. For |
| 412 example:: |
| 413 |
| 414 >>> import simplejson as json |
| 415 >>> json.JSONEncoder().encode({"foo": ["bar", "baz"]}) |
| 416 '{"foo": ["bar", "baz"]}' |
| 417 |
| 418 |
| 419 .. method:: iterencode(o) |
| 420 |
| 421 Encode the given object, *o*, and yield each string representation as |
| 422 available. For example:: |
| 423 |
| 424 for chunk in JSONEncoder().iterencode(bigobject): |
| 425 mysocket.write(chunk) |
| 426 |
| 427 Note that :meth:`encode` has much better performance than |
| 428 :meth:`iterencode`. |
OLD | NEW |