1r""" 2A simple, fast, extensible JSON encoder and decoder 3 4JSON (JavaScript Object Notation) <http://json.org> is a subset of 5JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data 6interchange format. 7 8simplejson exposes an API familiar to uses of the standard library 9marshal and pickle modules. 10 11Encoding basic Python object hierarchies:: 12 13 >>> import simplejson 14 >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) 15 '["foo", {"bar": ["baz", null, 1.0, 2]}]' 16 >>> print simplejson.dumps("\"foo\bar") 17 "\"foo\bar" 18 >>> print simplejson.dumps(u'\u1234') 19 "\u1234" 20 >>> print simplejson.dumps('\\') 21 "\\" 22 >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) 23 {"a": 0, "b": 0, "c": 0} 24 >>> from StringIO import StringIO 25 >>> io = StringIO() 26 >>> simplejson.dump(['streaming API'], io) 27 >>> io.getvalue() 28 '["streaming API"]' 29 30Compact encoding:: 31 32 >>> import simplejson 33 >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) 34 '[1,2,3,{"4":5,"6":7}]' 35 36Pretty printing:: 37 38 >>> import simplejson 39 >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) 40 { 41 "4": 5, 42 "6": 7 43 } 44 45Decoding JSON:: 46 47 >>> import simplejson 48 >>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') 49 [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] 50 >>> simplejson.loads('"\\"foo\\bar"') 51 u'"foo\x08ar' 52 >>> from StringIO import StringIO 53 >>> io = StringIO('["streaming API"]') 54 >>> simplejson.load(io) 55 [u'streaming API'] 56 57Specializing JSON object decoding:: 58 59 >>> import simplejson 60 >>> def as_complex(dct): 61 ... if '__complex__' in dct: 62 ... return complex(dct['real'], dct['imag']) 63 ... return dct 64 ... 65 >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', 66 ... object_hook=as_complex) 67 (1+2j) 68 69Extending JSONEncoder:: 70 71 >>> import simplejson 72 >>> class ComplexEncoder(simplejson.JSONEncoder): 73 ... def default(self, obj): 74 ... if isinstance(obj, complex): 75 ... return [obj.real, obj.imag] 76 ... return simplejson.JSONEncoder.default(self, obj) 77 ... 78 >>> dumps(2 + 1j, cls=ComplexEncoder) 79 '[2.0, 1.0]' 80 >>> ComplexEncoder().encode(2 + 1j) 81 '[2.0, 1.0]' 82 >>> list(ComplexEncoder().iterencode(2 + 1j)) 83 ['[', '2.0', ', ', '1.0', ']'] 84 85 86Note that the JSON produced by this module's default settings 87is a subset of YAML, so it may be used as a serializer for that as well. 88""" 89__version__ = '1.7.3' 90__all__ = [ 91 'dump', 'dumps', 'load', 'loads', 92 'JSONDecoder', 'JSONEncoder', 93] 94 95from decoder import JSONDecoder 96from encoder import JSONEncoder 97 98_default_encoder = JSONEncoder( 99 skipkeys=False, 100 ensure_ascii=True, 101 check_circular=True, 102 allow_nan=True, 103 indent=None, 104 separators=None, 105 encoding='utf-8' 106) 107 108def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, 109 allow_nan=True, cls=None, indent=None, separators=None, 110 encoding='utf-8', **kw): 111 """ 112 Serialize ``obj`` as a JSON formatted stream to ``fp`` (a 113 ``.write()``-supporting file-like object). 114 115 If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types 116 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) 117 will be skipped instead of raising a ``TypeError``. 118 119 If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp`` 120 may be ``unicode`` instances, subject to normal Python ``str`` to 121 ``unicode`` coercion rules. Unless ``fp.write()`` explicitly 122 understands ``unicode`` (as in ``codecs.getwriter()``) this is likely 123 to cause an error. 124 125 If ``check_circular`` is ``False``, then the circular reference check 126 for container types will be skipped and a circular reference will 127 result in an ``OverflowError`` (or worse). 128 129 If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to 130 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) 131 in strict compliance of the JSON specification, instead of using the 132 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). 133 134 If ``indent`` is a non-negative integer, then JSON array elements and object 135 members will be pretty-printed with that indent level. An indent level 136 of 0 will only insert newlines. ``None`` is the most compact representation. 137 138 If ``separators`` is an ``(item_separator, dict_separator)`` tuple 139 then it will be used instead of the default ``(', ', ': ')`` separators. 140 ``(',', ':')`` is the most compact JSON representation. 141 142 ``encoding`` is the character encoding for str instances, default is UTF-8. 143 144 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the 145 ``.default()`` method to serialize additional types), specify it with 146 the ``cls`` kwarg. 147 """ 148 # cached encoder 149 if (skipkeys is False and ensure_ascii is True and 150 check_circular is True and allow_nan is True and 151 cls is None and indent is None and separators is None and 152 encoding == 'utf-8' and not kw): 153 iterable = _default_encoder.iterencode(obj) 154 else: 155 if cls is None: 156 cls = JSONEncoder 157 iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, 158 check_circular=check_circular, allow_nan=allow_nan, indent=indent, 159 separators=separators, encoding=encoding, **kw).iterencode(obj) 160 # could accelerate with writelines in some versions of Python, at 161 # a debuggability cost 162 for chunk in iterable: 163 fp.write(chunk) 164 165 166def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, 167 allow_nan=True, cls=None, indent=None, separators=None, 168 encoding='utf-8', **kw): 169 """ 170 Serialize ``obj`` to a JSON formatted ``str``. 171 172 If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types 173 (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) 174 will be skipped instead of raising a ``TypeError``. 175 176 If ``ensure_ascii`` is ``False``, then the return value will be a 177 ``unicode`` instance subject to normal Python ``str`` to ``unicode`` 178 coercion rules instead of being escaped to an ASCII ``str``. 179 180 If ``check_circular`` is ``False``, then the circular reference check 181 for container types will be skipped and a circular reference will 182 result in an ``OverflowError`` (or worse). 183 184 If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to 185 serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in 186 strict compliance of the JSON specification, instead of using the 187 JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). 188 189 If ``indent`` is a non-negative integer, then JSON array elements and 190 object members will be pretty-printed with that indent level. An indent 191 level of 0 will only insert newlines. ``None`` is the most compact 192 representation. 193 194 If ``separators`` is an ``(item_separator, dict_separator)`` tuple 195 then it will be used instead of the default ``(', ', ': ')`` separators. 196 ``(',', ':')`` is the most compact JSON representation. 197 198 ``encoding`` is the character encoding for str instances, default is UTF-8. 199 200 To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the 201 ``.default()`` method to serialize additional types), specify it with 202 the ``cls`` kwarg. 203 """ 204 # cached encoder 205 if (skipkeys is False and ensure_ascii is True and 206 check_circular is True and allow_nan is True and 207 cls is None and indent is None and separators is None and 208 encoding == 'utf-8' and not kw): 209 return _default_encoder.encode(obj) 210 if cls is None: 211 cls = JSONEncoder 212 return cls( 213 skipkeys=skipkeys, ensure_ascii=ensure_ascii, 214 check_circular=check_circular, allow_nan=allow_nan, indent=indent, 215 separators=separators, encoding=encoding, 216 **kw).encode(obj) 217 218_default_decoder = JSONDecoder(encoding=None, object_hook=None) 219 220def load(fp, encoding=None, cls=None, object_hook=None, **kw): 221 """ 222 Deserialize ``fp`` (a ``.read()``-supporting file-like object containing 223 a JSON document) to a Python object. 224 225 If the contents of ``fp`` is encoded with an ASCII based encoding other 226 than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must 227 be specified. Encodings that are not ASCII based (such as UCS-2) are 228 not allowed, and should be wrapped with 229 ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` 230 object and passed to ``loads()`` 231 232 ``object_hook`` is an optional function that will be called with the 233 result of any object literal decode (a ``dict``). The return value of 234 ``object_hook`` will be used instead of the ``dict``. This feature 235 can be used to implement custom decoders (e.g. JSON-RPC class hinting). 236 237 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` 238 kwarg. 239 """ 240 return loads(fp.read(), 241 encoding=encoding, cls=cls, object_hook=object_hook, **kw) 242 243def loads(s, encoding=None, cls=None, object_hook=None, **kw): 244 """ 245 Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON 246 document) to a Python object. 247 248 If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding 249 other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name 250 must be specified. Encodings that are not ASCII based (such as UCS-2) 251 are not allowed and should be decoded to ``unicode`` first. 252 253 ``object_hook`` is an optional function that will be called with the 254 result of any object literal decode (a ``dict``). The return value of 255 ``object_hook`` will be used instead of the ``dict``. This feature 256 can be used to implement custom decoders (e.g. JSON-RPC class hinting). 257 258 To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` 259 kwarg. 260 """ 261 if cls is None and encoding is None and object_hook is None and not kw: 262 return _default_decoder.decode(s) 263 if cls is None: 264 cls = JSONDecoder 265 if object_hook is not None: 266 kw['object_hook'] = object_hook 267 return cls(encoding=encoding, **kw).decode(s) 268 269def read(s): 270 """ 271 json-py API compatibility hook. Use loads(s) instead. 272 """ 273 import warnings 274 warnings.warn("simplejson.loads(s) should be used instead of read(s)", 275 DeprecationWarning) 276 return loads(s) 277 278def write(obj): 279 """ 280 json-py API compatibility hook. Use dumps(s) instead. 281 """ 282 import warnings 283 warnings.warn("simplejson.dumps(s) should be used instead of write(s)", 284 DeprecationWarning) 285 return dumps(obj) 286 287 288