| 1 |
:mod:`json` --- JSON encoder and decoder |
| 2 |
======================================== |
| 3 |
|
| 4 |
.. module:: json |
| 5 |
:synopsis: Encode and decode the JSON format. |
| 6 |
.. moduleauthor:: Bob Ippolito <[email protected]> |
| 7 |
.. sectionauthor:: Bob Ippolito <[email protected]> |
| 8 |
.. versionadded:: 2.6 |
| 9 |
|
| 10 |
`JSON (JavaScript Object Notation) <http://json.org>`_, specified by |
| 11 |
:rfc:`7159` (which obsoletes :rfc:`4627`) and by |
| 12 |
`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_, |
| 13 |
is a lightweight data interchange format inspired by |
| 14 |
`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax |
| 15 |
(although it is not a strict subset of JavaScript [#rfc-errata]_ ). |
| 16 |
|
| 17 |
:mod:`json` exposes an API familiar to users of the standard library |
| 18 |
:mod:`marshal` and :mod:`pickle` modules. |
| 19 |
|
| 20 |
Encoding basic Python object hierarchies:: |
| 21 |
|
| 22 |
>>> import json |
| 23 |
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) |
| 24 |
'["foo", {"bar": ["baz", null, 1.0, 2]}]' |
| 25 |
>>> print json.dumps("\"foo\bar") |
| 26 |
"\"foo\bar" |
| 27 |
>>> print json.dumps(u'\u1234') |
| 28 |
"\u1234" |
| 29 |
>>> print json.dumps('\\') |
| 30 |
"\\" |
| 31 |
>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) |
| 32 |
{"a": 0, "b": 0, "c": 0} |
| 33 |
>>> from StringIO import StringIO |
| 34 |
>>> io = StringIO() |
| 35 |
>>> json.dump(['streaming API'], io) |
| 36 |
>>> io.getvalue() |
| 37 |
'["streaming API"]' |
| 38 |
|
| 39 |
Compact encoding:: |
| 40 |
|
| 41 |
>>> import json |
| 42 |
>>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) |
| 43 |
'[1,2,3,{"4":5,"6":7}]' |
| 44 |
|
| 45 |
Pretty printing:: |
| 46 |
|
| 47 |
>>> import json |
| 48 |
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, |
| 49 |
... indent=4, separators=(',', ': ')) |
| 50 |
{ |
| 51 |
"4": 5, |
| 52 |
"6": 7 |
| 53 |
} |
| 54 |
|
| 55 |
Decoding JSON:: |
| 56 |
|
| 57 |
>>> import json |
| 58 |
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') |
| 59 |
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}] |
| 60 |
>>> json.loads('"\\"foo\\bar"') |
| 61 |
u'"foo\x08ar' |
| 62 |
>>> from StringIO import StringIO |
| 63 |
>>> io = StringIO('["streaming API"]') |
| 64 |
>>> json.load(io) |
| 65 |
[u'streaming API'] |
| 66 |
|
| 67 |
Specializing JSON object decoding:: |
| 68 |
|
| 69 |
>>> import 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) |
| 80 |
Decimal('1.1') |
| 81 |
|
| 82 |
Extending :class:`JSONEncoder`:: |
| 83 |
|
| 84 |
>>> import json |
| 85 |
>>> class ComplexEncoder(json.JSONEncoder): |
| 86 |
... def default(self, obj): |
| 87 |
... if isinstance(obj, complex): |
| 88 |
... return [obj.real, obj.imag] |
| 89 |
... # Let the base class default method raise the TypeError |
| 90 |
... return json.JSONEncoder.default(self, obj) |
| 91 |
... |
| 92 |
>>> json.dumps(2 + 1j, cls=ComplexEncoder) |
| 93 |
'[2.0, 1.0]' |
| 94 |
>>> ComplexEncoder().encode(2 + 1j) |
| 95 |
'[2.0, 1.0]' |
| 96 |
>>> list(ComplexEncoder().iterencode(2 + 1j)) |
| 97 |
['[', '2.0', ', ', '1.0', ']'] |
| 98 |
|
| 99 |
|
| 100 |
.. highlight:: none |
| 101 |
|
| 102 |
Using :mod:`json.tool` from the shell to validate and pretty-print:: |
| 103 |
|
| 104 |
$ echo '{"json":"obj"}' | python -m json.tool |
| 105 |
{ |
| 106 |
"json": "obj" |
| 107 |
} |
| 108 |
$ echo '{1.2:3.4}' | python -mjson.tool |
| 109 |
Expecting property name enclosed in double quotes: line 1 column 2 (char 1) |
| 110 |
|
| 111 |
.. highlight:: python |
| 112 |
|
| 113 |
.. note:: |
| 114 |
|
| 115 |
JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by |
| 116 |
this module's default settings (in particular, the default *separators* |
| 117 |
value) is also a subset of YAML 1.0 and 1.1. This module can thus also be |
| 118 |
used as a YAML serializer. |
| 119 |
|
| 120 |
|
| 121 |
Basic Usage |
| 122 |
----------- |
| 123 |
|
| 124 |
.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \ |
| 125 |
check_circular=True, allow_nan=True, cls=None, \ |
| 126 |
indent=None, separators=None, encoding="utf-8", \ |
| 127 |
default=None, sort_keys=False, **kw) |
| 128 |
|
| 129 |
Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting |
| 130 |
:term:`file-like object`) using this :ref:`conversion table |
| 131 |
<py-to-json-table>`. |
| 132 |
|
| 133 |
If *skipkeys* is true (default: ``False``), then dict keys that are not |
| 134 |
of a basic type (:class:`str`, :class:`unicode`, :class:`int`, :class:`long`, |
| 135 |
:class:`float`, :class:`bool`, ``None``) will be skipped instead of raising a |
| 136 |
:exc:`TypeError`. |
| 137 |
|
| 138 |
If *ensure_ascii* is true (the default), all non-ASCII characters in the |
| 139 |
output are escaped with ``\uXXXX`` sequences, and the result is a |
| 140 |
:class:`str` instance consisting of ASCII characters only. If |
| 141 |
*ensure_ascii* is false, some chunks written to *fp* may be |
| 142 |
:class:`unicode` instances. This usually happens because the input contains |
| 143 |
unicode strings or the *encoding* parameter is used. Unless ``fp.write()`` |
| 144 |
explicitly understands :class:`unicode` (as in :func:`codecs.getwriter`) |
| 145 |
this is likely to cause an error. |
| 146 |
|
| 147 |
If *check_circular* is false (default: ``True``), then the circular |
| 148 |
reference check for container types will be skipped and a circular reference |
| 149 |
will result in an :exc:`OverflowError` (or worse). |
| 150 |
|
| 151 |
If *allow_nan* is false (default: ``True``), then it will be a |
| 152 |
:exc:`ValueError` to serialize out of range :class:`float` values (``nan``, |
| 153 |
``inf``, ``-inf``) in strict compliance of the JSON specification. |
| 154 |
If *allow_nan* is true, their JavaScript equivalents (``NaN``, |
| 155 |
``Infinity``, ``-Infinity``) will be used. |
| 156 |
|
| 157 |
If *indent* is a non-negative integer, then JSON array elements and object |
| 158 |
members will be pretty-printed with that indent level. An indent level of 0, |
| 159 |
or negative, will only insert newlines. ``None`` (the default) selects the |
| 160 |
most compact representation. |
| 161 |
|
| 162 |
.. note:: |
| 163 |
|
| 164 |
Since the default item separator is ``', '``, the output might include |
| 165 |
trailing whitespace when *indent* is specified. You can use |
| 166 |
``separators=(',', ': ')`` to avoid this. |
| 167 |
|
| 168 |
If specified, *separators* should be an ``(item_separator, key_separator)`` |
| 169 |
tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON |
| 170 |
representation, you should specify ``(',', ':')`` to eliminate whitespace. |
| 171 |
|
| 172 |
*encoding* is the character encoding for str instances, default is UTF-8. |
| 173 |
|
| 174 |
If specified, *default* should be a function that gets called for objects that |
| 175 |
can't otherwise be serialized. It should return a JSON encodable version of |
| 176 |
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` |
| 177 |
is raised. |
| 178 |
|
| 179 |
If *sort_keys* is true (default: ``False``), then the output of |
| 180 |
dictionaries will be sorted by key. |
| 181 |
|
| 182 |
To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the |
| 183 |
:meth:`default` method to serialize additional types), specify it with the |
| 184 |
*cls* kwarg; otherwise :class:`JSONEncoder` is used. |
| 185 |
|
| 186 |
.. note:: |
| 187 |
|
| 188 |
Unlike :mod:`pickle` and :mod:`marshal`, JSON is not a framed protocol so |
| 189 |
trying to serialize more objects with repeated calls to :func:`dump` and |
| 190 |
the same *fp* will result in an invalid JSON file. |
| 191 |
|
| 192 |
.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \ |
| 193 |
check_circular=True, allow_nan=True, cls=None, \ |
| 194 |
indent=None, separators=None, encoding="utf-8", \ |
| 195 |
default=None, sort_keys=False, **kw) |
| 196 |
|
| 197 |
Serialize *obj* to a JSON formatted :class:`str` using this :ref:`conversion |
| 198 |
table <py-to-json-table>`. If *ensure_ascii* is false, the result may |
| 199 |
contain non-ASCII characters and the return value may be a :class:`unicode` |
| 200 |
instance. |
| 201 |
|
| 202 |
The arguments have the same meaning as in :func:`dump`. |
| 203 |
|
| 204 |
.. note:: |
| 205 |
|
| 206 |
Keys in key/value pairs of JSON are always of the type :class:`str`. When |
| 207 |
a dictionary is converted into JSON, all the keys of the dictionary are |
| 208 |
coerced to strings. As a result of this, if a dictionary is converted |
| 209 |
into JSON and then back into a dictionary, the dictionary may not equal |
| 210 |
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string |
| 211 |
keys. |
| 212 |
|
| 213 |
.. function:: load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]]) |
| 214 |
|
| 215 |
Deserialize *fp* (a ``.read()``-supporting :term:`file-like object` |
| 216 |
containing a JSON document) to a Python object using this :ref:`conversion |
| 217 |
table <json-to-py-table>`. |
| 218 |
|
| 219 |
If the contents of *fp* are encoded with an ASCII based encoding other than |
| 220 |
UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be specified. |
| 221 |
Encodings that are not ASCII based (such as UCS-2) are not allowed, and |
| 222 |
should be wrapped with ``codecs.getreader(encoding)(fp)``, or simply decoded |
| 223 |
to a :class:`unicode` object and passed to :func:`loads`. |
| 224 |
|
| 225 |
*object_hook* is an optional function that will be called with the result of |
| 226 |
any object literal decoded (a :class:`dict`). The return value of |
| 227 |
*object_hook* will be used instead of the :class:`dict`. This feature can be used |
| 228 |
to implement custom decoders (e.g. `JSON-RPC <http://www.jsonrpc.org>`_ |
| 229 |
class hinting). |
| 230 |
|
| 231 |
*object_pairs_hook* is an optional function that will be called with the |
| 232 |
result of any object literal decoded with an ordered list of pairs. The |
| 233 |
return value of *object_pairs_hook* will be used instead of the |
| 234 |
:class:`dict`. This feature can be used to implement custom decoders that |
| 235 |
rely on the order that the key and value pairs are decoded (for example, |
| 236 |
:func:`collections.OrderedDict` will remember the order of insertion). If |
| 237 |
*object_hook* is also defined, the *object_pairs_hook* takes priority. |
| 238 |
|
| 239 |
.. versionchanged:: 2.7 |
| 240 |
Added support for *object_pairs_hook*. |
| 241 |
|
| 242 |
*parse_float*, if specified, will be called with the string of every JSON |
| 243 |
float to be decoded. By default, this is equivalent to ``float(num_str)``. |
| 244 |
This can be used to use another datatype or parser for JSON floats |
| 245 |
(e.g. :class:`decimal.Decimal`). |
| 246 |
|
| 247 |
*parse_int*, if specified, will be called with the string of every JSON int |
| 248 |
to be decoded. By default, this is equivalent to ``int(num_str)``. This can |
| 249 |
be used to use another datatype or parser for JSON integers |
| 250 |
(e.g. :class:`float`). |
| 251 |
|
| 252 |
*parse_constant*, if specified, will be called with one of the following |
| 253 |
strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. |
| 254 |
This can be used to raise an exception if invalid JSON numbers |
| 255 |
are encountered. |
| 256 |
|
| 257 |
.. versionchanged:: 2.7 |
| 258 |
*parse_constant* doesn't get called on 'null', 'true', 'false' anymore. |
| 259 |
|
| 260 |
To use a custom :class:`JSONDecoder` subclass, specify it with the ``cls`` |
| 261 |
kwarg; otherwise :class:`JSONDecoder` is used. Additional keyword arguments |
| 262 |
will be passed to the constructor of the class. |
| 263 |
|
| 264 |
|
| 265 |
.. function:: loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]]) |
| 266 |
|
| 267 |
Deserialize *s* (a :class:`str` or :class:`unicode` instance containing a JSON |
| 268 |
document) to a Python object using this :ref:`conversion table |
| 269 |
<json-to-py-table>`. |
| 270 |
|
| 271 |
If *s* is a :class:`str` instance and is encoded with an ASCII based encoding |
| 272 |
other than UTF-8 (e.g. latin-1), then an appropriate *encoding* name must be |
| 273 |
specified. Encodings that are not ASCII based (such as UCS-2) are not |
| 274 |
allowed and should be decoded to :class:`unicode` first. |
| 275 |
|
| 276 |
The other arguments have the same meaning as in :func:`load`. |
| 277 |
|
| 278 |
|
| 279 |
Encoders and Decoders |
| 280 |
--------------------- |
| 281 |
|
| 282 |
.. class:: JSONDecoder([encoding[, object_hook[, parse_float[, parse_int[, parse_constant[, strict[, object_pairs_hook]]]]]]]) |
| 283 |
|
| 284 |
Simple JSON decoder. |
| 285 |
|
| 286 |
Performs the following translations in decoding by default: |
| 287 |
|
| 288 |
.. _json-to-py-table: |
| 289 |
|
| 290 |
+---------------+-------------------+ |
| 291 |
| JSON | Python | |
| 292 |
+===============+===================+ |
| 293 |
| object | dict | |
| 294 |
+---------------+-------------------+ |
| 295 |
| array | list | |
| 296 |
+---------------+-------------------+ |
| 297 |
| string | unicode | |
| 298 |
+---------------+-------------------+ |
| 299 |
| number (int) | int, long | |
| 300 |
+---------------+-------------------+ |
| 301 |
| number (real) | float | |
| 302 |
+---------------+-------------------+ |
| 303 |
| true | True | |
| 304 |
+---------------+-------------------+ |
| 305 |
| false | False | |
| 306 |
+---------------+-------------------+ |
| 307 |
| null | None | |
| 308 |
+---------------+-------------------+ |
| 309 |
|
| 310 |
It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as their |
| 311 |
corresponding ``float`` values, which is outside the JSON spec. |
| 312 |
|
| 313 |
*encoding* determines the encoding used to interpret any :class:`str` objects |
| 314 |
decoded by this instance (UTF-8 by default). It has no effect when decoding |
| 315 |
:class:`unicode` objects. |
| 316 |
|
| 317 |
Note that currently only encodings that are a superset of ASCII work, strings |
| 318 |
of other encodings should be passed in as :class:`unicode`. |
| 319 |
|
| 320 |
*object_hook*, if specified, will be called with the result of every JSON |
| 321 |
object decoded and its return value will be used in place of the given |
| 322 |
:class:`dict`. This can be used to provide custom deserializations (e.g. to |
| 323 |
support JSON-RPC class hinting). |
| 324 |
|
| 325 |
*object_pairs_hook*, if specified will be called with the result of every |
| 326 |
JSON object decoded with an ordered list of pairs. The return value of |
| 327 |
*object_pairs_hook* will be used instead of the :class:`dict`. This |
| 328 |
feature can be used to implement custom decoders that rely on the order |
| 329 |
that the key and value pairs are decoded (for example, |
| 330 |
:func:`collections.OrderedDict` will remember the order of insertion). If |
| 331 |
*object_hook* is also defined, the *object_pairs_hook* takes priority. |
| 332 |
|
| 333 |
.. versionchanged:: 2.7 |
| 334 |
Added support for *object_pairs_hook*. |
| 335 |
|
| 336 |
*parse_float*, if specified, will be called with the string of every JSON |
| 337 |
float to be decoded. By default, this is equivalent to ``float(num_str)``. |
| 338 |
This can be used to use another datatype or parser for JSON floats |
| 339 |
(e.g. :class:`decimal.Decimal`). |
| 340 |
|
| 341 |
*parse_int*, if specified, will be called with the string of every JSON int |
| 342 |
to be decoded. By default, this is equivalent to ``int(num_str)``. This can |
| 343 |
be used to use another datatype or parser for JSON integers |
| 344 |
(e.g. :class:`float`). |
| 345 |
|
| 346 |
*parse_constant*, if specified, will be called with one of the following |
| 347 |
strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. |
| 348 |
This can be used to raise an exception if invalid JSON numbers |
| 349 |
are encountered. |
| 350 |
|
| 351 |
If *strict* is false (``True`` is the default), then control characters |
| 352 |
will be allowed inside strings. Control characters in this context are |
| 353 |
those with character codes in the 0--31 range, including ``'\t'`` (tab), |
| 354 |
``'\n'``, ``'\r'`` and ``'\0'``. |
| 355 |
|
| 356 |
If the data being deserialized is not a valid JSON document, a |
| 357 |
:exc:`ValueError` will be raised. |
| 358 |
|
| 359 |
.. method:: decode(s) |
| 360 |
|
| 361 |
Return the Python representation of *s* (a :class:`str` or |
| 362 |
:class:`unicode` instance containing a JSON document). |
| 363 |
|
| 364 |
.. method:: raw_decode(s) |
| 365 |
|
| 366 |
Decode a JSON document from *s* (a :class:`str` or :class:`unicode` |
| 367 |
beginning with a JSON document) and return a 2-tuple of the Python |
| 368 |
representation and the index in *s* where the document ended. |
| 369 |
|
| 370 |
This can be used to decode a JSON document from a string that may have |
| 371 |
extraneous data at the end. |
| 372 |
|
| 373 |
|
| 374 |
.. class:: JSONEncoder([skipkeys[, ensure_ascii[, check_circular[, allow_nan[, sort_keys[, indent[, separators[, encoding[, default]]]]]]]]]) |
| 375 |
|
| 376 |
Extensible JSON encoder for Python data structures. |
| 377 |
|
| 378 |
Supports the following objects and types by default: |
| 379 |
|
| 380 |
.. _py-to-json-table: |
| 381 |
|
| 382 |
+-------------------+---------------+ |
| 383 |
| Python | JSON | |
| 384 |
+===================+===============+ |
| 385 |
| dict | object | |
| 386 |
+-------------------+---------------+ |
| 387 |
| list, tuple | array | |
| 388 |
+-------------------+---------------+ |
| 389 |
| str, unicode | string | |
| 390 |
+-------------------+---------------+ |
| 391 |
| int, long, float | number | |
| 392 |
+-------------------+---------------+ |
| 393 |
| True | true | |
| 394 |
+-------------------+---------------+ |
| 395 |
| False | false | |
| 396 |
+-------------------+---------------+ |
| 397 |
| None | null | |
| 398 |
+-------------------+---------------+ |
| 399 |
|
| 400 |
To extend this to recognize other objects, subclass and implement a |
| 401 |
:meth:`default` method with another method that returns a serializable object |
| 402 |
for ``o`` if possible, otherwise it should call the superclass implementation |
| 403 |
(to raise :exc:`TypeError`). |
| 404 |
|
| 405 |
If *skipkeys* is false (the default), then it is a :exc:`TypeError` to |
| 406 |
attempt encoding of keys that are not str, int, long, float or ``None``. If |
| 407 |
*skipkeys* is true, such items are simply skipped. |
| 408 |
|
| 409 |
If *ensure_ascii* is true (the default), all non-ASCII characters in the |
| 410 |
output are escaped with ``\uXXXX`` sequences, and the results are |
| 411 |
:class:`str` instances consisting of ASCII characters only. If |
| 412 |
*ensure_ascii* is false, a result may be a :class:`unicode` |
| 413 |
instance. This usually happens if the input contains unicode strings or the |
| 414 |
*encoding* parameter is used. |
| 415 |
|
| 416 |
If *check_circular* is true (the default), then lists, dicts, and custom |
| 417 |
encoded objects will be checked for circular references during encoding to |
| 418 |
prevent an infinite recursion (which would cause an :exc:`OverflowError`). |
| 419 |
Otherwise, no such check takes place. |
| 420 |
|
| 421 |
If *allow_nan* is true (the default), then ``NaN``, ``Infinity``, and |
| 422 |
``-Infinity`` will be encoded as such. This behavior is not JSON |
| 423 |
specification compliant, but is consistent with most JavaScript based |
| 424 |
encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode |
| 425 |
such floats. |
| 426 |
|
| 427 |
If *sort_keys* is true (default: ``False``), then the output of dictionaries |
| 428 |
will be sorted by key; this is useful for regression tests to ensure that |
| 429 |
JSON serializations can be compared on a day-to-day basis. |
| 430 |
|
| 431 |
If *indent* is a non-negative integer (it is ``None`` by default), then JSON |
| 432 |
array elements and object members will be pretty-printed with that indent |
| 433 |
level. An indent level of 0 will only insert newlines. ``None`` is the most |
| 434 |
compact representation. |
| 435 |
|
| 436 |
.. note:: |
| 437 |
|
| 438 |
Since the default item separator is ``', '``, the output might include |
| 439 |
trailing whitespace when *indent* is specified. You can use |
| 440 |
``separators=(',', ': ')`` to avoid this. |
| 441 |
|
| 442 |
If specified, *separators* should be an ``(item_separator, key_separator)`` |
| 443 |
tuple. By default, ``(', ', ': ')`` are used. To get the most compact JSON |
| 444 |
representation, you should specify ``(',', ':')`` to eliminate whitespace. |
| 445 |
|
| 446 |
If specified, *default* should be a function that gets called for objects that |
| 447 |
can't otherwise be serialized. It should return a JSON encodable version of |
| 448 |
the object or raise a :exc:`TypeError`. If not specified, :exc:`TypeError` |
| 449 |
is raised. |
| 450 |
|
| 451 |
If *encoding* is not ``None``, then all input strings will be transformed |
| 452 |
into unicode using that encoding prior to JSON-encoding. The default is |
| 453 |
UTF-8. |
| 454 |
|
| 455 |
|
| 456 |
.. method:: default(o) |
| 457 |
|
| 458 |
Implement this method in a subclass such that it returns a serializable |
| 459 |
object for *o*, or calls the base implementation (to raise a |
| 460 |
:exc:`TypeError`). |
| 461 |
|
| 462 |
For example, to support arbitrary iterators, you could implement default |
| 463 |
like this:: |
| 464 |
|
| 465 |
def default(self, o): |
| 466 |
try: |
| 467 |
iterable = iter(o) |
| 468 |
except TypeError: |
| 469 |
pass |
| 470 |
else: |
| 471 |
return list(iterable) |
| 472 |
# Let the base class default method raise the TypeError |
| 473 |
return JSONEncoder.default(self, o) |
| 474 |
|
| 475 |
|
| 476 |
.. method:: encode(o) |
| 477 |
|
| 478 |
Return a JSON string representation of a Python data structure, *o*. For |
| 479 |
example:: |
| 480 |
|
| 481 |
>>> JSONEncoder().encode({"foo": ["bar", "baz"]}) |
| 482 |
'{"foo": ["bar", "baz"]}' |
| 483 |
|
| 484 |
|
| 485 |
.. method:: iterencode(o) |
| 486 |
|
| 487 |
Encode the given object, *o*, and yield each string representation as |
| 488 |
available. For example:: |
| 489 |
|
| 490 |
for chunk in JSONEncoder().iterencode(bigobject): |
| 491 |
mysocket.write(chunk) |
| 492 |
|
| 493 |
|
| 494 |
Standard Compliance and Interoperability |
| 495 |
---------------------------------------- |
| 496 |
|
| 497 |
The JSON format is specified by :rfc:`7159` and by |
| 498 |
`ECMA-404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>`_. |
| 499 |
This section details this module's level of compliance with the RFC. |
| 500 |
For simplicity, :class:`JSONEncoder` and :class:`JSONDecoder` subclasses, and |
| 501 |
parameters other than those explicitly mentioned, are not considered. |
| 502 |
|
| 503 |
This module does not comply with the RFC in a strict fashion, implementing some |
| 504 |
extensions that are valid JavaScript but not valid JSON. In particular: |
| 505 |
|
| 506 |
- Infinite and NaN number values are accepted and output; |
| 507 |
- Repeated names within an object are accepted, and only the value of the last |
| 508 |
name-value pair is used. |
| 509 |
|
| 510 |
Since the RFC permits RFC-compliant parsers to accept input texts that are not |
| 511 |
RFC-compliant, this module's deserializer is technically RFC-compliant under |
| 512 |
default settings. |
| 513 |
|
| 514 |
Character Encodings |
| 515 |
^^^^^^^^^^^^^^^^^^^ |
| 516 |
|
| 517 |
The RFC requires that JSON be represented using either UTF-8, UTF-16, or |
| 518 |
UTF-32, with UTF-8 being the recommended default for maximum interoperability. |
| 519 |
Accordingly, this module uses UTF-8 as the default for its *encoding* parameter. |
| 520 |
|
| 521 |
This module's deserializer only directly works with ASCII-compatible encodings; |
| 522 |
UTF-16, UTF-32, and other ASCII-incompatible encodings require the use of |
| 523 |
workarounds described in the documentation for the deserializer's *encoding* |
| 524 |
parameter. |
| 525 |
|
| 526 |
As permitted, though not required, by the RFC, this module's serializer sets |
| 527 |
*ensure_ascii=True* by default, thus escaping the output so that the resulting |
| 528 |
strings only contain ASCII characters. |
| 529 |
|
| 530 |
The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text, |
| 531 |
and this module's serializer does not add a BOM to its output. |
| 532 |
The RFC permits, but does not require, JSON deserializers to ignore an initial |
| 533 |
BOM in their input. This module's deserializer raises a :exc:`ValueError` |
| 534 |
when an initial BOM is present. |
| 535 |
|
| 536 |
The RFC does not explicitly forbid JSON strings which contain byte sequences |
| 537 |
that don't correspond to valid Unicode characters (e.g. unpaired UTF-16 |
| 538 |
surrogates), but it does note that they may cause interoperability problems. |
| 539 |
By default, this module accepts and outputs (when present in the original |
| 540 |
:class:`str`) code points for such sequences. |
| 541 |
|
| 542 |
|
| 543 |
Infinite and NaN Number Values |
| 544 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 545 |
|
| 546 |
The RFC does not permit the representation of infinite or NaN number values. |
| 547 |
Despite that, by default, this module accepts and outputs ``Infinity``, |
| 548 |
``-Infinity``, and ``NaN`` as if they were valid JSON number literal values:: |
| 549 |
|
| 550 |
>>> # Neither of these calls raises an exception, but the results are not valid JSON |
| 551 |
>>> json.dumps(float('-inf')) |
| 552 |
'-Infinity' |
| 553 |
>>> json.dumps(float('nan')) |
| 554 |
'NaN' |
| 555 |
>>> # Same when deserializing |
| 556 |
>>> json.loads('-Infinity') |
| 557 |
-inf |
| 558 |
>>> json.loads('NaN') |
| 559 |
nan |
| 560 |
|
| 561 |
In the serializer, the *allow_nan* parameter can be used to alter this |
| 562 |
behavior. In the deserializer, the *parse_constant* parameter can be used to |
| 563 |
alter this behavior. |
| 564 |
|
| 565 |
|
| 566 |
Repeated Names Within an Object |
| 567 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 568 |
|
| 569 |
The RFC specifies that the names within a JSON object should be unique, but |
| 570 |
does not mandate how repeated names in JSON objects should be handled. By |
| 571 |
default, this module does not raise an exception; instead, it ignores all but |
| 572 |
the last name-value pair for a given name:: |
| 573 |
|
| 574 |
>>> weird_json = '{"x": 1, "x": 2, "x": 3}' |
| 575 |
>>> json.loads(weird_json) |
| 576 |
{u'x': 3} |
| 577 |
|
| 578 |
The *object_pairs_hook* parameter can be used to alter this behavior. |
| 579 |
|
| 580 |
|
| 581 |
Top-level Non-Object, Non-Array Values |
| 582 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 583 |
|
| 584 |
The old version of JSON specified by the obsolete :rfc:`4627` required that |
| 585 |
the top-level value of a JSON text must be either a JSON object or array |
| 586 |
(Python :class:`dict` or :class:`list`), and could not be a JSON null, |
| 587 |
boolean, number, or string value. :rfc:`7159` removed that restriction, and |
| 588 |
this module does not and has never implemented that restriction in either its |
| 589 |
serializer or its deserializer. |
| 590 |
|
| 591 |
Regardless, for maximum interoperability, you may wish to voluntarily adhere |
| 592 |
to the restriction yourself. |
| 593 |
|
| 594 |
|
| 595 |
Implementation Limitations |
| 596 |
^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 597 |
|
| 598 |
Some JSON deserializer implementations may set limits on: |
| 599 |
|
| 600 |
* the size of accepted JSON texts |
| 601 |
* the maximum level of nesting of JSON objects and arrays |
| 602 |
* the range and precision of JSON numbers |
| 603 |
* the content and maximum length of JSON strings |
| 604 |
|
| 605 |
This module does not impose any such limits beyond those of the relevant |
| 606 |
Python datatypes themselves or the Python interpreter itself. |
| 607 |
|
| 608 |
When serializing to JSON, beware any such limitations in applications that may |
| 609 |
consume your JSON. In particular, it is common for JSON numbers to be |
| 610 |
deserialized into IEEE 754 double precision numbers and thus subject to that |
| 611 |
representation's range and precision limitations. This is especially relevant |
| 612 |
when serializing Python :class:`int` values of extremely large magnitude, or |
| 613 |
when serializing instances of "exotic" numerical types such as |
| 614 |
:class:`decimal.Decimal`. |
| 615 |
|
| 616 |
|
| 617 |
.. rubric:: Footnotes |
| 618 |
|
| 619 |
.. [#rfc-errata] As noted in `the errata for RFC 7159 |
| 620 |
<https://www.rfc-editor.org/errata_search.php?rfc=7159>`_, |
| 621 |
JSON permits literal U+2028 (LINE SEPARATOR) and |
| 622 |
U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript |
| 623 |
(as of ECMAScript Edition 5.1) does not. |