:mod:`simplejson` --- JSON encoder and decoder
JSON (JavaScript Object Notation) <http://json.org> is a subset of JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data interchange format.
:mod:`simplejson` exposes an API familiar to users of the standard library :mod:`marshal` and :mod:`pickle` modules. It is the externally maintained version of the :mod:`json` library contained in Python 2.6, but maintains compatibility with Python 2.4 and Python 2.5 and (currently) has significant performance advantages, even without using the optional C extension for speedups.
Encoding basic Python object hierarchies:
>>> import simplejson as json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print json.dumps("\"foo\bar")
"\"foo\bar"
>>> print json.dumps(u'\u1234')
"\u1234"
>>> print json.dumps('\\')
"\\"
>>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
{"a": 0, "b": 0, "c": 0}
>>> from StringIO import StringIO
>>> io = StringIO()
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'
Compact encoding:
>>> import simplejson as json
>>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
'[1,2,3,{"4":5,"6":7}]'
Pretty printing:
>>> import simplejson as json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
"4": 5,
"6": 7
}
Decoding JSON:
>>> import simplejson as json
>>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
True
>>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
True
>>> from StringIO import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)[0] == 'streaming API'
True
Specializing JSON object decoding:
>>> import simplejson as json
>>> def as_complex(dct):
... if '__complex__' in dct:
... return complex(dct['real'], dct['imag'])
... return dct
...
>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
... object_hook=as_complex)
(1+2j)
>>> import decimal
>>> json.loads('1.1', parse_float=decimal.Decimal) == decimal.Decimal('1.1')
True
Specializing JSON object encoding:
>>> import simplejson as json
>>> def encode_complex(obj):
... if isinstance(obj, complex):
... return [obj.real, obj.imag]
... raise TypeError("%r is not JSON serializable" % (o,))
...
>>> json.dumps(2 + 1j, default=encode_complex)
'[2.0, 1.0]'
>>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
'[2.0, 1.0]'
>>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
'[2.0, 1.0]'
Using simplejson.tool from the shell to validate and pretty-print:
$ echo '{"json":"obj"}' | python -msimplejson.tool
{
"json": "obj"
}
$ echo '{ 1.2:3.4}' | python -msimplejson.tool
Expecting property name: line 1 column 2 (char 2)
Note
The JSON produced by this module's default settings is a subset of YAML, so it may be used as a serializer for that as well.
Basic Usage
Encoders and decoders
Simple JSON decoder.
Performs the following translations in decoding by default:
JSON | Python |
---|---|
object | dict |
array | list |
string | unicode |
number (int) | int, long |
number (real) | float |
true | True |
false | False |
null | None |
It also understands NaN
, Infinity
, and -Infinity
as their
corresponding float
values, which is outside the JSON spec.
encoding determines the encoding used to interpret any :class:`str` objects
decoded by this instance ('utf-8'
by default). It has no effect when decoding
:class:`unicode` objects.
Note that currently only encodings that are a superset of ASCII work, strings of other encodings should be passed in as :class:`unicode`.
object_hook, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the given :class:`dict`. This can be used to provide custom deserializations (e.g. to support JSON-RPC class hinting).
parse_float, if specified, will be called with the string of every JSON
float to be decoded. By default, this is equivalent to float(num_str)
.
This can be used to use another datatype or parser for JSON floats
(e.g. :class:`decimal.Decimal`).
parse_int, if specified, will be called with the string of every JSON int
to be decoded. By default, this is equivalent to int(num_str)
. This can
be used to use another datatype or parser for JSON integers
(e.g. :class:`float`).
parse_constant, if specified, will be called with one of the following
strings: '-Infinity'
, 'Infinity'
, 'NaN'
. This can be used to
raise an exception if invalid JSON numbers are encountered.
Extensible JSON encoder for Python data structures.
Supports the following objects and types by default:
Python | JSON |
---|---|
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
To extend this to recognize other objects, subclass and implement a
:meth:`default` method with another method that returns a serializable object
for o
if possible, otherwise it should call the superclass implementation
(to raise :exc:`TypeError`).
If skipkeys is False
(the default), then it is a :exc:`TypeError` to
attempt encoding of keys that are not str, int, long, float or None. If
skipkeys is True
, such items are simply skipped.
If ensure_ascii is True
(the default), the output is guaranteed to be
:class:`str` objects with all incoming unicode characters escaped. If
ensure_ascii is False
, the output will be a unicode object.
If check_circular is True
(the default), then lists, dicts, and custom
encoded objects will be checked for circular references during encoding to
prevent an infinite recursion (which would cause an :exc:`OverflowError`).
Otherwise, no such check takes place.
If allow_nan is True
(the default), then NaN
, Infinity
, and
-Infinity
will be encoded as such. This behavior is not JSON
specification compliant, but is consistent with most JavaScript based
encoders and decoders. Otherwise, it will be a :exc:`ValueError` to encode
such floats.
If sort_keys is True
(the default), then the output of dictionaries
will be sorted by key; this is useful for regression tests to ensure that
JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer (it is None
by default), then JSON
array elements and object members will be pretty-printed with that indent
level. An indent level of 0 will only insert newlines. None
is the most
compact representation.
If specified, separators should be an (item_separator, key_separator)
tuple. The default is (', ', ': ')
. To get the most compact JSON
representation, you should specify (',', ':')
to eliminate whitespace.
If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a :exc:`TypeError`.
If encoding is not None
, then all input strings will be transformed
into unicode using that encoding prior to JSON-encoding. The default is
'utf-8'
.