michael@0: r""" michael@0: A simple, fast, extensible JSON encoder and decoder michael@0: michael@0: JSON (JavaScript Object Notation) is a subset of michael@0: JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data michael@0: interchange format. michael@0: michael@0: simplejson exposes an API familiar to uses of the standard library michael@0: marshal and pickle modules. michael@0: michael@0: Encoding basic Python object hierarchies:: michael@0: michael@0: >>> import simplejson michael@0: >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) michael@0: '["foo", {"bar": ["baz", null, 1.0, 2]}]' michael@0: >>> print simplejson.dumps("\"foo\bar") michael@0: "\"foo\bar" michael@0: >>> print simplejson.dumps(u'\u1234') michael@0: "\u1234" michael@0: >>> print simplejson.dumps('\\') michael@0: "\\" michael@0: >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) michael@0: {"a": 0, "b": 0, "c": 0} michael@0: >>> from StringIO import StringIO michael@0: >>> io = StringIO() michael@0: >>> simplejson.dump(['streaming API'], io) michael@0: >>> io.getvalue() michael@0: '["streaming API"]' michael@0: michael@0: Compact encoding:: michael@0: michael@0: >>> import simplejson michael@0: >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) michael@0: '[1,2,3,{"4":5,"6":7}]' michael@0: michael@0: Pretty printing:: michael@0: michael@0: >>> import simplejson michael@0: >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) michael@0: { michael@0: "4": 5, michael@0: "6": 7 michael@0: } michael@0: michael@0: Decoding JSON:: michael@0: michael@0: >>> import simplejson michael@0: >>> simplejson.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') michael@0: [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] michael@0: >>> simplejson.loads('"\\"foo\\bar"') michael@0: u'"foo\x08ar' michael@0: >>> from StringIO import StringIO michael@0: >>> io = StringIO('["streaming API"]') michael@0: >>> simplejson.load(io) michael@0: [u'streaming API'] michael@0: michael@0: Specializing JSON object decoding:: michael@0: michael@0: >>> import simplejson michael@0: >>> def as_complex(dct): michael@0: ... if '__complex__' in dct: michael@0: ... return complex(dct['real'], dct['imag']) michael@0: ... return dct michael@0: ... michael@0: >>> simplejson.loads('{"__complex__": true, "real": 1, "imag": 2}', michael@0: ... object_hook=as_complex) michael@0: (1+2j) michael@0: >>> import decimal michael@0: >>> simplejson.loads('1.1', parse_float=decimal.Decimal) michael@0: Decimal("1.1") michael@0: michael@0: Extending JSONEncoder:: michael@0: michael@0: >>> import simplejson michael@0: >>> class ComplexEncoder(simplejson.JSONEncoder): michael@0: ... def default(self, obj): michael@0: ... if isinstance(obj, complex): michael@0: ... return [obj.real, obj.imag] michael@0: ... return simplejson.JSONEncoder.default(self, obj) michael@0: ... michael@0: >>> dumps(2 + 1j, cls=ComplexEncoder) michael@0: '[2.0, 1.0]' michael@0: >>> ComplexEncoder().encode(2 + 1j) michael@0: '[2.0, 1.0]' michael@0: >>> list(ComplexEncoder().iterencode(2 + 1j)) michael@0: ['[', '2.0', ', ', '1.0', ']'] michael@0: michael@0: michael@0: Using simplejson from the shell to validate and michael@0: pretty-print:: michael@0: michael@0: $ echo '{"json":"obj"}' | python -msimplejson.tool michael@0: { michael@0: "json": "obj" michael@0: } michael@0: $ echo '{ 1.2:3.4}' | python -msimplejson.tool michael@0: Expecting property name: line 1 column 2 (char 2) michael@0: michael@0: Note that the JSON produced by this module's default settings michael@0: is a subset of YAML, so it may be used as a serializer for that as well. michael@0: """ michael@0: __version__ = '1.9.2' michael@0: __all__ = [ michael@0: 'dump', 'dumps', 'load', 'loads', michael@0: 'JSONDecoder', 'JSONEncoder', michael@0: ] michael@0: michael@0: if __name__ == '__main__': michael@0: import warnings michael@0: warnings.warn('python -msimplejson is deprecated, use python -msiplejson.tool', DeprecationWarning) michael@0: from simplejson.decoder import JSONDecoder michael@0: from simplejson.encoder import JSONEncoder michael@0: else: michael@0: from decoder import JSONDecoder michael@0: from encoder import JSONEncoder michael@0: michael@0: _default_encoder = JSONEncoder( michael@0: skipkeys=False, michael@0: ensure_ascii=True, michael@0: check_circular=True, michael@0: allow_nan=True, michael@0: indent=None, michael@0: separators=None, michael@0: encoding='utf-8', michael@0: default=None, michael@0: ) michael@0: michael@0: def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, michael@0: allow_nan=True, cls=None, indent=None, separators=None, michael@0: encoding='utf-8', default=None, **kw): michael@0: """ michael@0: Serialize ``obj`` as a JSON formatted stream to ``fp`` (a michael@0: ``.write()``-supporting file-like object). michael@0: michael@0: If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types michael@0: (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) michael@0: will be skipped instead of raising a ``TypeError``. michael@0: michael@0: If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp`` michael@0: may be ``unicode`` instances, subject to normal Python ``str`` to michael@0: ``unicode`` coercion rules. Unless ``fp.write()`` explicitly michael@0: understands ``unicode`` (as in ``codecs.getwriter()``) this is likely michael@0: to cause an error. michael@0: michael@0: If ``check_circular`` is ``False``, then the circular reference check michael@0: for container types will be skipped and a circular reference will michael@0: result in an ``OverflowError`` (or worse). michael@0: michael@0: If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to michael@0: serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) michael@0: in strict compliance of the JSON specification, instead of using the michael@0: JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). michael@0: michael@0: If ``indent`` is a non-negative integer, then JSON array elements and object michael@0: members will be pretty-printed with that indent level. An indent level michael@0: of 0 will only insert newlines. ``None`` is the most compact representation. michael@0: michael@0: If ``separators`` is an ``(item_separator, dict_separator)`` tuple michael@0: then it will be used instead of the default ``(', ', ': ')`` separators. michael@0: ``(',', ':')`` is the most compact JSON representation. michael@0: michael@0: ``encoding`` is the character encoding for str instances, default is UTF-8. michael@0: michael@0: ``default(obj)`` is a function that should return a serializable version michael@0: of obj or raise TypeError. The default simply raises TypeError. michael@0: michael@0: To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the michael@0: ``.default()`` method to serialize additional types), specify it with michael@0: the ``cls`` kwarg. michael@0: """ michael@0: # cached encoder michael@0: if (skipkeys is False and ensure_ascii is True and michael@0: check_circular is True and allow_nan is True and michael@0: cls is None and indent is None and separators is None and michael@0: encoding == 'utf-8' and default is None and not kw): michael@0: iterable = _default_encoder.iterencode(obj) michael@0: else: michael@0: if cls is None: michael@0: cls = JSONEncoder michael@0: iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, michael@0: check_circular=check_circular, allow_nan=allow_nan, indent=indent, michael@0: separators=separators, encoding=encoding, michael@0: default=default, **kw).iterencode(obj) michael@0: # could accelerate with writelines in some versions of Python, at michael@0: # a debuggability cost michael@0: for chunk in iterable: michael@0: fp.write(chunk) michael@0: michael@0: michael@0: def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, michael@0: allow_nan=True, cls=None, indent=None, separators=None, michael@0: encoding='utf-8', default=None, **kw): michael@0: """ michael@0: Serialize ``obj`` to a JSON formatted ``str``. michael@0: michael@0: If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types michael@0: (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) michael@0: will be skipped instead of raising a ``TypeError``. michael@0: michael@0: If ``ensure_ascii`` is ``False``, then the return value will be a michael@0: ``unicode`` instance subject to normal Python ``str`` to ``unicode`` michael@0: coercion rules instead of being escaped to an ASCII ``str``. michael@0: michael@0: If ``check_circular`` is ``False``, then the circular reference check michael@0: for container types will be skipped and a circular reference will michael@0: result in an ``OverflowError`` (or worse). michael@0: michael@0: If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to michael@0: serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in michael@0: strict compliance of the JSON specification, instead of using the michael@0: JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). michael@0: michael@0: If ``indent`` is a non-negative integer, then JSON array elements and michael@0: object members will be pretty-printed with that indent level. An indent michael@0: level of 0 will only insert newlines. ``None`` is the most compact michael@0: representation. michael@0: michael@0: If ``separators`` is an ``(item_separator, dict_separator)`` tuple michael@0: then it will be used instead of the default ``(', ', ': ')`` separators. michael@0: ``(',', ':')`` is the most compact JSON representation. michael@0: michael@0: ``encoding`` is the character encoding for str instances, default is UTF-8. michael@0: michael@0: ``default(obj)`` is a function that should return a serializable version michael@0: of obj or raise TypeError. The default simply raises TypeError. michael@0: michael@0: To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the michael@0: ``.default()`` method to serialize additional types), specify it with michael@0: the ``cls`` kwarg. michael@0: """ michael@0: # cached encoder michael@0: if (skipkeys is False and ensure_ascii is True and michael@0: check_circular is True and allow_nan is True and michael@0: cls is None and indent is None and separators is None and michael@0: encoding == 'utf-8' and default is None and not kw): michael@0: return _default_encoder.encode(obj) michael@0: if cls is None: michael@0: cls = JSONEncoder michael@0: return cls( michael@0: skipkeys=skipkeys, ensure_ascii=ensure_ascii, michael@0: check_circular=check_circular, allow_nan=allow_nan, indent=indent, michael@0: separators=separators, encoding=encoding, default=default, michael@0: **kw).encode(obj) michael@0: michael@0: michael@0: _default_decoder = JSONDecoder(encoding=None, object_hook=None) michael@0: michael@0: michael@0: def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, michael@0: parse_int=None, parse_constant=None, **kw): michael@0: """ michael@0: Deserialize ``fp`` (a ``.read()``-supporting file-like object containing michael@0: a JSON document) to a Python object. michael@0: michael@0: If the contents of ``fp`` is encoded with an ASCII based encoding other michael@0: than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must michael@0: be specified. Encodings that are not ASCII based (such as UCS-2) are michael@0: not allowed, and should be wrapped with michael@0: ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode`` michael@0: object and passed to ``loads()`` michael@0: michael@0: ``object_hook`` is an optional function that will be called with the michael@0: result of any object literal decode (a ``dict``). The return value of michael@0: ``object_hook`` will be used instead of the ``dict``. This feature michael@0: can be used to implement custom decoders (e.g. JSON-RPC class hinting). michael@0: michael@0: To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` michael@0: kwarg. michael@0: """ michael@0: return loads(fp.read(), michael@0: encoding=encoding, cls=cls, object_hook=object_hook, michael@0: parse_float=parse_float, parse_int=parse_int, michael@0: parse_constant=parse_constant, **kw) michael@0: michael@0: michael@0: def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, michael@0: parse_int=None, parse_constant=None, **kw): michael@0: """ michael@0: Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON michael@0: document) to a Python object. michael@0: michael@0: If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding michael@0: other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name michael@0: must be specified. Encodings that are not ASCII based (such as UCS-2) michael@0: are not allowed and should be decoded to ``unicode`` first. michael@0: michael@0: ``object_hook`` is an optional function that will be called with the michael@0: result of any object literal decode (a ``dict``). The return value of michael@0: ``object_hook`` will be used instead of the ``dict``. This feature michael@0: can be used to implement custom decoders (e.g. JSON-RPC class hinting). michael@0: michael@0: ``parse_float``, if specified, will be called with the string michael@0: of every JSON float to be decoded. By default this is equivalent to michael@0: float(num_str). This can be used to use another datatype or parser michael@0: for JSON floats (e.g. decimal.Decimal). michael@0: michael@0: ``parse_int``, if specified, will be called with the string michael@0: of every JSON int to be decoded. By default this is equivalent to michael@0: int(num_str). This can be used to use another datatype or parser michael@0: for JSON integers (e.g. float). michael@0: michael@0: ``parse_constant``, if specified, will be called with one of the michael@0: following strings: -Infinity, Infinity, NaN, null, true, false. michael@0: This can be used to raise an exception if invalid JSON numbers michael@0: are encountered. michael@0: michael@0: To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` michael@0: kwarg. michael@0: """ michael@0: if (cls is None and encoding is None and object_hook is None and michael@0: parse_int is None and parse_float is None and michael@0: parse_constant is None and not kw): michael@0: return _default_decoder.decode(s) michael@0: if cls is None: michael@0: cls = JSONDecoder michael@0: if object_hook is not None: michael@0: kw['object_hook'] = object_hook michael@0: if parse_float is not None: michael@0: kw['parse_float'] = parse_float michael@0: if parse_int is not None: michael@0: kw['parse_int'] = parse_int michael@0: if parse_constant is not None: michael@0: kw['parse_constant'] = parse_constant michael@0: return cls(encoding=encoding, **kw).decode(s) michael@0: michael@0: michael@0: # michael@0: # Compatibility cruft from other libraries michael@0: # michael@0: michael@0: michael@0: def decode(s): michael@0: """ michael@0: demjson, python-cjson API compatibility hook. Use loads(s) instead. michael@0: """ michael@0: import warnings michael@0: warnings.warn("simplejson.loads(s) should be used instead of decode(s)", michael@0: DeprecationWarning) michael@0: return loads(s) michael@0: michael@0: michael@0: def encode(obj): michael@0: """ michael@0: demjson, python-cjson compatibility hook. Use dumps(s) instead. michael@0: """ michael@0: import warnings michael@0: warnings.warn("simplejson.dumps(s) should be used instead of encode(s)", michael@0: DeprecationWarning) michael@0: return dumps(obj) michael@0: michael@0: michael@0: def read(s): michael@0: """ michael@0: jsonlib, JsonUtils, python-json, json-py API compatibility hook. michael@0: Use loads(s) instead. michael@0: """ michael@0: import warnings michael@0: warnings.warn("simplejson.loads(s) should be used instead of read(s)", michael@0: DeprecationWarning) michael@0: return loads(s) michael@0: michael@0: michael@0: def write(obj): michael@0: """ michael@0: jsonlib, JsonUtils, python-json, json-py API compatibility hook. michael@0: Use dumps(s) instead. michael@0: """ michael@0: import warnings michael@0: warnings.warn("simplejson.dumps(s) should be used instead of write(s)", michael@0: DeprecationWarning) michael@0: return dumps(obj) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: import simplejson.tool michael@0: simplejson.tool.main()