Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 1 | r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 2 | JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data |
| 3 | interchange format. |
| 4 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 5 | :mod:`json` 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. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 11 | |
| 12 | Encoding basic Python object hierarchies:: |
| 13 | |
| 14 | >>> import 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('\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 io import StringIO |
| 26 | >>> io = StringIO() |
| 27 | >>> json.dump(['streaming API'], io) |
| 28 | >>> io.getvalue() |
| 29 | '["streaming API"]' |
| 30 | |
| 31 | Compact encoding:: |
| 32 | |
| 33 | >>> import json |
| 34 | >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) |
| 35 | '[1,2,3,{"4":5,"6":7}]' |
| 36 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 37 | Pretty printing:: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 38 | |
| 39 | >>> import json |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 40 | >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) |
| 41 | >>> print('\n'.join([l.rstrip() for l in s.splitlines()])) |
| 42 | { |
| 43 | "4": 5, |
| 44 | "6": 7 |
| 45 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 46 | |
| 47 | Decoding JSON:: |
| 48 | |
| 49 | >>> import json |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 50 | >>> obj = ['foo', {'bar': ['baz', None, 1.0, 2]}] |
| 51 | >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj |
| 52 | True |
| 53 | >>> json.loads('"\\"foo\\bar"') == '"foo\x08ar' |
| 54 | True |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 55 | >>> from io import StringIO |
| 56 | >>> io = StringIO('["streaming API"]') |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 57 | >>> json.load(io)[0] == 'streaming API' |
| 58 | True |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 59 | |
| 60 | Specializing JSON object decoding:: |
| 61 | |
| 62 | >>> import 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) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 71 | >>> from decimal import Decimal |
| 72 | >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1') |
| 73 | True |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 74 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 75 | Specializing JSON object encoding:: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 76 | |
| 77 | >>> import json |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 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") |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 82 | ... |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 83 | >>> json.dumps(2 + 1j, default=encode_complex) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 84 | '[2.0, 1.0]' |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 85 | >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 86 | '[2.0, 1.0]' |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 87 | >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) |
| 88 | '[2.0, 1.0]' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 89 | |
| 90 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 91 | Using json.tool from the shell to validate and pretty-print:: |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 92 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 93 | $ echo '{"json":"obj"}' | python -m json.tool |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 94 | { |
| 95 | "json": "obj" |
| 96 | } |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 97 | $ echo '{ 1.2:3.4}' | python -m json.tool |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 98 | Expecting property name: line 1 column 2 (char 2) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 99 | """ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 100 | __version__ = '2.0.9' |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 101 | __all__ = [ |
| 102 | 'dump', 'dumps', 'load', 'loads', |
| 103 | 'JSONDecoder', 'JSONEncoder', |
| 104 | ] |
| 105 | |
| 106 | __author__ = 'Bob Ippolito <bob@redivi.com>' |
| 107 | |
| 108 | from .decoder import JSONDecoder |
| 109 | from .encoder import JSONEncoder |
| 110 | |
| 111 | _default_encoder = JSONEncoder( |
| 112 | skipkeys=False, |
| 113 | ensure_ascii=True, |
| 114 | check_circular=True, |
| 115 | allow_nan=True, |
| 116 | indent=None, |
| 117 | separators=None, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 118 | default=None, |
| 119 | ) |
| 120 | |
| 121 | def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, |
| 122 | allow_nan=True, cls=None, indent=None, separators=None, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 123 | default=None, **kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 124 | """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a |
| 125 | ``.write()``-supporting file-like object). |
| 126 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 127 | If ``skipkeys`` is true then ``dict`` keys that are not basic types |
| 128 | (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be |
| 129 | skipped instead of raising a ``TypeError``. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 131 | If ``ensure_ascii`` is false, then the some chunks written to ``fp`` |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 132 | may be ``unicode`` instances, subject to normal Python ``str`` to |
| 133 | ``unicode`` coercion rules. Unless ``fp.write()`` explicitly |
| 134 | understands ``unicode`` (as in ``codecs.getwriter()``) this is likely |
| 135 | to cause an error. |
| 136 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 137 | If ``check_circular`` is false, then the circular reference check |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 138 | for container types will be skipped and a circular reference will |
| 139 | result in an ``OverflowError`` (or worse). |
| 140 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 141 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 142 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) |
| 143 | in strict compliance of the JSON specification, instead of using the |
| 144 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 145 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 146 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 147 | object members will be pretty-printed with that indent level. An indent |
| 148 | level of 0 will only insert newlines. ``None`` is the most compact |
| 149 | representation. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 150 | |
| 151 | If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
| 152 | then it will be used instead of the default ``(', ', ': ')`` separators. |
| 153 | ``(',', ':')`` is the most compact JSON representation. |
| 154 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 155 | ``default(obj)`` is a function that should return a serializable version |
| 156 | of obj or raise TypeError. The default simply raises TypeError. |
| 157 | |
| 158 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 159 | ``.default()`` method to serialize additional types), specify it with |
| 160 | the ``cls`` kwarg. |
| 161 | |
| 162 | """ |
| 163 | # cached encoder |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 164 | if (not skipkeys and ensure_ascii and |
| 165 | check_circular and allow_nan and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 166 | cls is None and indent is None and separators is None and |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 167 | default is None and not kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 168 | iterable = _default_encoder.iterencode(obj) |
| 169 | else: |
| 170 | if cls is None: |
| 171 | cls = JSONEncoder |
| 172 | iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 173 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 174 | separators=separators, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 175 | default=default, **kw).iterencode(obj) |
| 176 | # could accelerate with writelines in some versions of Python, at |
| 177 | # a debuggability cost |
| 178 | for chunk in iterable: |
| 179 | fp.write(chunk) |
| 180 | |
| 181 | |
| 182 | def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, |
| 183 | allow_nan=True, cls=None, indent=None, separators=None, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 184 | default=None, **kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 185 | """Serialize ``obj`` to a JSON formatted ``str``. |
| 186 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 187 | If ``skipkeys`` is false then ``dict`` keys that are not basic types |
| 188 | (``str``, ``unicode``, ``int``, ``float``, ``bool``, ``None``) will be |
| 189 | skipped instead of raising a ``TypeError``. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 191 | If ``ensure_ascii`` is false, then the return value will be a |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 192 | ``unicode`` instance subject to normal Python ``str`` to ``unicode`` |
| 193 | coercion rules instead of being escaped to an ASCII ``str``. |
| 194 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 195 | If ``check_circular`` is false, then the circular reference check |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 196 | for container types will be skipped and a circular reference will |
| 197 | result in an ``OverflowError`` (or worse). |
| 198 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 199 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 200 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in |
| 201 | strict compliance of the JSON specification, instead of using the |
| 202 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 203 | |
| 204 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 205 | object members will be pretty-printed with that indent level. An indent |
| 206 | level of 0 will only insert newlines. ``None`` is the most compact |
| 207 | representation. |
| 208 | |
| 209 | If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
| 210 | then it will be used instead of the default ``(', ', ': ')`` separators. |
| 211 | ``(',', ':')`` is the most compact JSON representation. |
| 212 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 213 | ``default(obj)`` is a function that should return a serializable version |
| 214 | of obj or raise TypeError. The default simply raises TypeError. |
| 215 | |
| 216 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 217 | ``.default()`` method to serialize additional types), specify it with |
| 218 | the ``cls`` kwarg. |
| 219 | |
| 220 | """ |
| 221 | # cached encoder |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 222 | if (not skipkeys and ensure_ascii and |
| 223 | check_circular and allow_nan and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 224 | cls is None and indent is None and separators is None and |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 225 | default is None and not kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 226 | return _default_encoder.encode(obj) |
| 227 | if cls is None: |
| 228 | cls = JSONEncoder |
| 229 | return cls( |
| 230 | skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 231 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 232 | separators=separators, default=default, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 233 | **kw).encode(obj) |
| 234 | |
| 235 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 236 | _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 237 | |
| 238 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 239 | def load(fp, cls=None, object_hook=None, parse_float=None, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 240 | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 241 | """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing |
| 242 | a JSON document) to a Python object. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 243 | |
| 244 | ``object_hook`` is an optional function that will be called with the |
| 245 | result of any object literal decode (a ``dict``). The return value of |
| 246 | ``object_hook`` will be used instead of the ``dict``. This feature |
| 247 | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
| 248 | |
| 249 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
| 250 | kwarg. |
| 251 | |
| 252 | """ |
| 253 | return loads(fp.read(), |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 254 | cls=cls, object_hook=object_hook, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 255 | parse_float=parse_float, parse_int=parse_int, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 256 | parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 257 | |
| 258 | |
| 259 | def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 260 | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 261 | """Deserialize ``s`` (a ``str`` instance containing a JSON |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 262 | document) to a Python object. |
| 263 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 264 | ``object_hook`` is an optional function that will be called with the |
| 265 | result of any object literal decode (a ``dict``). The return value of |
| 266 | ``object_hook`` will be used instead of the ``dict``. This feature |
| 267 | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
| 268 | |
| 269 | ``parse_float``, if specified, will be called with the string |
| 270 | of every JSON float to be decoded. By default this is equivalent to |
| 271 | float(num_str). This can be used to use another datatype or parser |
| 272 | for JSON floats (e.g. decimal.Decimal). |
| 273 | |
| 274 | ``parse_int``, if specified, will be called with the string |
| 275 | of every JSON int to be decoded. By default this is equivalent to |
| 276 | int(num_str). This can be used to use another datatype or parser |
| 277 | for JSON integers (e.g. float). |
| 278 | |
| 279 | ``parse_constant``, if specified, will be called with one of the |
| 280 | following strings: -Infinity, Infinity, NaN, null, true, false. |
| 281 | This can be used to raise an exception if invalid JSON numbers |
| 282 | are encountered. |
| 283 | |
| 284 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
| 285 | kwarg. |
| 286 | |
| 287 | """ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 288 | if (cls is None and object_hook is None and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 289 | parse_int is None and parse_float is None and |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 290 | parse_constant is None and object_pairs_hook is None and not kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 291 | return _default_decoder.decode(s) |
| 292 | if cls is None: |
| 293 | cls = JSONDecoder |
| 294 | if object_hook is not None: |
| 295 | kw['object_hook'] = object_hook |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 296 | if object_pairs_hook is not None: |
| 297 | kw['object_pairs_hook'] = object_pairs_hook |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 298 | if parse_float is not None: |
| 299 | kw['parse_float'] = parse_float |
| 300 | if parse_int is not None: |
| 301 | kw['parse_int'] = parse_int |
| 302 | if parse_constant is not None: |
| 303 | kw['parse_constant'] = parse_constant |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 304 | return cls(**kw).decode(s) |