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 |
Éric Araujo | de579d4 | 2011-04-21 02:37:41 +0200 | [diff] [blame] | 34 | >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',', ':')) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 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 |
Georg Brandl | 4009c9e | 2010-10-06 08:26:09 +0000 | [diff] [blame] | 128 | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
| 129 | instead of raising a ``TypeError``. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 130 | |
Georg Brandl | 4009c9e | 2010-10-06 08:26:09 +0000 | [diff] [blame] | 131 | If ``ensure_ascii`` is false, then the strings written to ``fp`` can |
| 132 | contain non-ASCII characters if they appear in strings contained in |
| 133 | ``obj``. Otherwise, all such characters are escaped in JSON strings. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 134 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 135 | If ``check_circular`` is false, then the circular reference check |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 136 | for container types will be skipped and a circular reference will |
| 137 | result in an ``OverflowError`` (or worse). |
| 138 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 139 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 140 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) |
| 141 | in strict compliance of the JSON specification, instead of using the |
| 142 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 143 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 144 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 145 | object members will be pretty-printed with that indent level. An indent |
| 146 | level of 0 will only insert newlines. ``None`` is the most compact |
| 147 | representation. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 148 | |
| 149 | If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
| 150 | then it will be used instead of the default ``(', ', ': ')`` separators. |
| 151 | ``(',', ':')`` is the most compact JSON representation. |
| 152 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 153 | ``default(obj)`` is a function that should return a serializable version |
| 154 | of obj or raise TypeError. The default simply raises TypeError. |
| 155 | |
| 156 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 157 | ``.default()`` method to serialize additional types), specify it with |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 158 | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 159 | |
| 160 | """ |
| 161 | # cached encoder |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 162 | if (not skipkeys and ensure_ascii and |
| 163 | check_circular and allow_nan and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 164 | cls is None and indent is None and separators is None and |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 165 | default is None and not kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 166 | iterable = _default_encoder.iterencode(obj) |
| 167 | else: |
| 168 | if cls is None: |
| 169 | cls = JSONEncoder |
| 170 | iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 171 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 172 | separators=separators, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 173 | default=default, **kw).iterencode(obj) |
| 174 | # could accelerate with writelines in some versions of Python, at |
| 175 | # a debuggability cost |
| 176 | for chunk in iterable: |
| 177 | fp.write(chunk) |
| 178 | |
| 179 | |
| 180 | def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, |
| 181 | allow_nan=True, cls=None, indent=None, separators=None, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 182 | default=None, **kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 183 | """Serialize ``obj`` to a JSON formatted ``str``. |
| 184 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 185 | If ``skipkeys`` is false then ``dict`` keys that are not basic types |
Georg Brandl | 4009c9e | 2010-10-06 08:26:09 +0000 | [diff] [blame] | 186 | (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped |
| 187 | instead of raising a ``TypeError``. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 188 | |
Georg Brandl | 4009c9e | 2010-10-06 08:26:09 +0000 | [diff] [blame] | 189 | If ``ensure_ascii`` is false, then the return value can contain non-ASCII |
| 190 | characters if they appear in strings contained in ``obj``. Otherwise, all |
| 191 | such characters are escaped in JSON strings. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 192 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 193 | If ``check_circular`` is false, then the circular reference check |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 194 | for container types will be skipped and a circular reference will |
| 195 | result in an ``OverflowError`` (or worse). |
| 196 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 197 | If ``allow_nan`` is false, then it will be a ``ValueError`` to |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 198 | serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in |
| 199 | strict compliance of the JSON specification, instead of using the |
| 200 | JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). |
| 201 | |
| 202 | If ``indent`` is a non-negative integer, then JSON array elements and |
| 203 | object members will be pretty-printed with that indent level. An indent |
| 204 | level of 0 will only insert newlines. ``None`` is the most compact |
| 205 | representation. |
| 206 | |
| 207 | If ``separators`` is an ``(item_separator, dict_separator)`` tuple |
| 208 | then it will be used instead of the default ``(', ', ': ')`` separators. |
| 209 | ``(',', ':')`` is the most compact JSON representation. |
| 210 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 211 | ``default(obj)`` is a function that should return a serializable version |
| 212 | of obj or raise TypeError. The default simply raises TypeError. |
| 213 | |
| 214 | To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the |
| 215 | ``.default()`` method to serialize additional types), specify it with |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 216 | the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 217 | |
| 218 | """ |
| 219 | # cached encoder |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 220 | if (not skipkeys and ensure_ascii and |
| 221 | check_circular and allow_nan and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 222 | cls is None and indent is None and separators is None and |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 223 | default is None and not kw): |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 224 | return _default_encoder.encode(obj) |
| 225 | if cls is None: |
| 226 | cls = JSONEncoder |
| 227 | return cls( |
| 228 | skipkeys=skipkeys, ensure_ascii=ensure_ascii, |
| 229 | check_circular=check_circular, allow_nan=allow_nan, indent=indent, |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 230 | separators=separators, default=default, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 231 | **kw).encode(obj) |
| 232 | |
| 233 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 234 | _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 235 | |
| 236 | |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 237 | def load(fp, cls=None, object_hook=None, parse_float=None, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 238 | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 239 | """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing |
| 240 | a JSON document) to a Python object. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 241 | |
| 242 | ``object_hook`` is an optional function that will be called with the |
| 243 | result of any object literal decode (a ``dict``). The return value of |
| 244 | ``object_hook`` will be used instead of the ``dict``. This feature |
| 245 | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
| 246 | |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 247 | ``object_pairs_hook`` is an optional function that will be called with the |
| 248 | result of any object literal decoded with an ordered list of pairs. The |
| 249 | return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
| 250 | This feature can be used to implement custom decoders that rely on the |
| 251 | order that the key and value pairs are decoded (for example, |
| 252 | collections.OrderedDict will remember the order of insertion). If |
| 253 | ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
| 254 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 255 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 256 | kwarg; otherwise ``JSONDecoder`` is used. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 257 | |
| 258 | """ |
| 259 | return loads(fp.read(), |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 260 | cls=cls, object_hook=object_hook, |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 261 | parse_float=parse_float, parse_int=parse_int, |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 262 | parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 263 | |
| 264 | |
| 265 | 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] | 266 | parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 267 | """Deserialize ``s`` (a ``str`` instance containing a JSON |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 268 | document) to a Python object. |
| 269 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 270 | ``object_hook`` is an optional function that will be called with the |
| 271 | result of any object literal decode (a ``dict``). The return value of |
| 272 | ``object_hook`` will be used instead of the ``dict``. This feature |
| 273 | can be used to implement custom decoders (e.g. JSON-RPC class hinting). |
| 274 | |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 275 | ``object_pairs_hook`` is an optional function that will be called with the |
| 276 | result of any object literal decoded with an ordered list of pairs. The |
| 277 | return value of ``object_pairs_hook`` will be used instead of the ``dict``. |
| 278 | This feature can be used to implement custom decoders that rely on the |
| 279 | order that the key and value pairs are decoded (for example, |
| 280 | collections.OrderedDict will remember the order of insertion). If |
| 281 | ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority. |
| 282 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 283 | ``parse_float``, if specified, will be called with the string |
| 284 | of every JSON float to be decoded. By default this is equivalent to |
| 285 | float(num_str). This can be used to use another datatype or parser |
| 286 | for JSON floats (e.g. decimal.Decimal). |
| 287 | |
| 288 | ``parse_int``, if specified, will be called with the string |
| 289 | of every JSON int to be decoded. By default this is equivalent to |
| 290 | int(num_str). This can be used to use another datatype or parser |
| 291 | for JSON integers (e.g. float). |
| 292 | |
| 293 | ``parse_constant``, if specified, will be called with one of the |
| 294 | following strings: -Infinity, Infinity, NaN, null, true, false. |
| 295 | This can be used to raise an exception if invalid JSON numbers |
| 296 | are encountered. |
| 297 | |
| 298 | To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` |
Georg Brandl | c524cff | 2010-11-26 08:42:45 +0000 | [diff] [blame] | 299 | kwarg; otherwise ``JSONDecoder`` is used. |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | a970e62 | 2011-01-21 21:39:56 +0000 | [diff] [blame] | 301 | The ``encoding`` argument is ignored and deprecated. |
| 302 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 303 | """ |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 304 | if (cls is None and object_hook is None and |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 305 | parse_int is None and parse_float is None and |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 306 | 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] | 307 | return _default_decoder.decode(s) |
| 308 | if cls is None: |
| 309 | cls = JSONDecoder |
| 310 | if object_hook is not None: |
| 311 | kw['object_hook'] = object_hook |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 312 | if object_pairs_hook is not None: |
| 313 | kw['object_pairs_hook'] = object_pairs_hook |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 314 | if parse_float is not None: |
| 315 | kw['parse_float'] = parse_float |
| 316 | if parse_int is not None: |
| 317 | kw['parse_int'] = parse_int |
| 318 | if parse_constant is not None: |
| 319 | kw['parse_constant'] = parse_constant |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 320 | return cls(**kw).decode(s) |