Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

592 lines (436 sloc) 23.563 kB

: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.5 and (currently) has significant performance advantages, even without using the optional C extension for speedups.

Development of simplejson happens on Github: http://github.com/simplejson/simplejson

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
>>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4 * ' ')
>>> print '\n'.join([l.rstrip() for l in  s.splitlines()])
{
    "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

Using Decimal instead of float:

>>> import simplejson as json
>>> from decimal import Decimal
>>> json.loads('1.1', use_decimal=True) == Decimal('1.1')
True
>>> json.dumps(Decimal('1.1'), use_decimal=True) == '1.1'
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(repr(o) + " is not JSON serializable")
...
>>> 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 :mod:`simplejson.tool` from the shell to validate and pretty-print:

$ echo '{"json":"obj"}' | python -m simplejson.tool
{
    "json": "obj"
}
$ echo '{ 1.2:3.4}' | python -m simplejson.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 is an optional function that 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).

object_pairs_hook is an optional function that will be called with the result of any object literal decode with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the :class:`dict`. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, :class:`collections.OrderedDict` will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

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.

strict controls the parser's behavior when it encounters an invalid control character in a string. The default setting of True means that unescaped control characters are parse errors, if False then control characters will be allowed in strings.

Extensible JSON encoder for Python data structures.

Supports the following objects and types by default:

Python JSON
dict, namedtuple 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 false (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 (not 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 string, then JSON array elements and object members will be pretty-printed with a newline followed by that string repeated for each level of nesting. None (the default) selects the most compact representation without any newlines. For backwards compatibility with versions of simplejson earlier than 2.1.0, an integer is also accepted and is converted to a string with that many spaces.

If specified, separators should be an (item_separator, key_separator) tuple. By default, (', ', ': ') are used. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

If specified, default should be 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'.

If namedtuple_as_object is true (default: True), objects with _asdict() methods will be encoded as JSON objects.

If tuple_as_array is true (default: True), :class:`tuple` (and subclasses) will be encoded as JSON arrays.

Subclass of :class:`JSONEncoder` that escapes &, <, and > for embedding in HTML.

Exceptions

Jump to Line
Something went wrong with that request. Please try again.