Back to index

python3.2  3.2.2
Namespaces | Functions | Variables
json Namespace Reference

Namespaces

namespace  decoder
namespace  encoder
namespace  scanner
namespace  tool

Functions

def dump
def dumps
def load
def loads

Variables

string __version__ = '2.0.9'
list __all__
string __author__ = 'Bob Ippolito <bob@redivi.com>'
tuple _default_encoder
tuple _default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)

Function Documentation

def json.dump (   obj,
  fp,
  skipkeys = False,
  ensure_ascii = True,
  check_circular = True,
  allow_nan = True,
  cls = None,
  indent = None,
  separators = None,
  default = None,
  kw 
)
Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
``.write()``-supporting file-like object).

If ``skipkeys`` is true then ``dict`` keys that are not basic types
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
instead of raising a ``TypeError``.

If ``ensure_ascii`` is false, then the strings written to ``fp`` can
contain non-ASCII characters if they appear in strings contained in
``obj``. Otherwise, all such characters are escaped in JSON strings.

If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will
result in an ``OverflowError`` (or worse).

If ``allow_nan`` is false, then it will be a ``ValueError`` to
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
in strict compliance of the JSON specification, instead of using the
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

If ``indent`` is a non-negative integer, 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 ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
``(',', ':')`` is the most compact JSON representation.

``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError.

To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

Definition at line 123 of file __init__.py.

00123 
00124         default=None, **kw):
00125     """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
00126     ``.write()``-supporting file-like object).
00127 
00128     If ``skipkeys`` is true then ``dict`` keys that are not basic types
00129     (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
00130     instead of raising a ``TypeError``.
00131 
00132     If ``ensure_ascii`` is false, then the strings written to ``fp`` can
00133     contain non-ASCII characters if they appear in strings contained in
00134     ``obj``. Otherwise, all such characters are escaped in JSON strings.
00135 
00136     If ``check_circular`` is false, then the circular reference check
00137     for container types will be skipped and a circular reference will
00138     result in an ``OverflowError`` (or worse).
00139 
00140     If ``allow_nan`` is false, then it will be a ``ValueError`` to
00141     serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
00142     in strict compliance of the JSON specification, instead of using the
00143     JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
00144 
00145     If ``indent`` is a non-negative integer, then JSON array elements and
00146     object members will be pretty-printed with that indent level. An indent
00147     level of 0 will only insert newlines. ``None`` is the most compact
00148     representation.
00149 
00150     If ``separators`` is an ``(item_separator, dict_separator)`` tuple
00151     then it will be used instead of the default ``(', ', ': ')`` separators.
00152     ``(',', ':')`` is the most compact JSON representation.
00153 
00154     ``default(obj)`` is a function that should return a serializable version
00155     of obj or raise TypeError. The default simply raises TypeError.
00156 
00157     To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
00158     ``.default()`` method to serialize additional types), specify it with
00159     the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
00160 
00161     """
00162     # cached encoder
00163     if (not skipkeys and ensure_ascii and
00164         check_circular and allow_nan and
00165         cls is None and indent is None and separators is None and
00166         default is None and not kw):
00167         iterable = _default_encoder.iterencode(obj)
00168     else:
00169         if cls is None:
00170             cls = JSONEncoder
00171         iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
00172             check_circular=check_circular, allow_nan=allow_nan, indent=indent,
00173             separators=separators,
00174             default=default, **kw).iterencode(obj)
00175     # could accelerate with writelines in some versions of Python, at
00176     # a debuggability cost
00177     for chunk in iterable:
00178         fp.write(chunk)
00179 

Here is the call graph for this function:

Here is the caller graph for this function:

def json.dumps (   obj,
  skipkeys = False,
  ensure_ascii = True,
  check_circular = True,
  allow_nan = True,
  cls = None,
  indent = None,
  separators = None,
  default = None,
  kw 
)
Serialize ``obj`` to a JSON formatted ``str``.

If ``skipkeys`` is false then ``dict`` keys that are not basic types
(``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
instead of raising a ``TypeError``.

If ``ensure_ascii`` is false, then the return value can contain non-ASCII
characters if they appear in strings contained in ``obj``. Otherwise, all
such characters are escaped in JSON strings.

If ``check_circular`` is false, then the circular reference check
for container types will be skipped and a circular reference will
result in an ``OverflowError`` (or worse).

If ``allow_nan`` is false, then it will be a ``ValueError`` to
serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
strict compliance of the JSON specification, instead of using the
JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

If ``indent`` is a non-negative integer, 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 ``separators`` is an ``(item_separator, dict_separator)`` tuple
then it will be used instead of the default ``(', ', ': ')`` separators.
``(',', ':')`` is the most compact JSON representation.

``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError.

To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

Definition at line 182 of file __init__.py.

00182 
00183         default=None, **kw):
00184     """Serialize ``obj`` to a JSON formatted ``str``.
00185 
00186     If ``skipkeys`` is false then ``dict`` keys that are not basic types
00187     (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
00188     instead of raising a ``TypeError``.
00189 
00190     If ``ensure_ascii`` is false, then the return value can contain non-ASCII
00191     characters if they appear in strings contained in ``obj``. Otherwise, all
00192     such characters are escaped in JSON strings.
00193 
00194     If ``check_circular`` is false, then the circular reference check
00195     for container types will be skipped and a circular reference will
00196     result in an ``OverflowError`` (or worse).
00197 
00198     If ``allow_nan`` is false, then it will be a ``ValueError`` to
00199     serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
00200     strict compliance of the JSON specification, instead of using the
00201     JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
00202 
00203     If ``indent`` is a non-negative integer, then JSON array elements and
00204     object members will be pretty-printed with that indent level. An indent
00205     level of 0 will only insert newlines. ``None`` is the most compact
00206     representation.
00207 
00208     If ``separators`` is an ``(item_separator, dict_separator)`` tuple
00209     then it will be used instead of the default ``(', ', ': ')`` separators.
00210     ``(',', ':')`` is the most compact JSON representation.
00211 
00212     ``default(obj)`` is a function that should return a serializable version
00213     of obj or raise TypeError. The default simply raises TypeError.
00214 
00215     To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
00216     ``.default()`` method to serialize additional types), specify it with
00217     the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
00218 
00219     """
00220     # cached encoder
00221     if (not skipkeys and ensure_ascii and
00222         check_circular and allow_nan and
00223         cls is None and indent is None and separators is None and
00224         default is None and not kw):
00225         return _default_encoder.encode(obj)
00226     if cls is None:
00227         cls = JSONEncoder
00228     return cls(
00229         skipkeys=skipkeys, ensure_ascii=ensure_ascii,
00230         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
00231         separators=separators, default=default,
00232         **kw).encode(obj)
00233 

Here is the caller graph for this function:

def json.load (   fp,
  cls = None,
  object_hook = None,
  parse_float = None,
  parse_int = None,
  parse_constant = None,
  object_pairs_hook = None,
  kw 
)
Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object.

``object_hook`` is an optional function that will be called with the
result of any object literal decode (a ``dict``). The return value of
``object_hook`` will be used instead of the ``dict``. This feature
can be used to implement custom decoders (e.g. JSON-RPC class hinting).

``object_pairs_hook`` is an optional function that will be called with the
result of any object literal decoded with an ordered list of pairs.  The
return value of ``object_pairs_hook`` will be used instead of the ``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,
collections.OrderedDict will remember the order of insertion). If
``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.

To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg; otherwise ``JSONDecoder`` is used.

Definition at line 238 of file __init__.py.

00238 
00239         parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
00240     """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
00241     a JSON document) to a Python object.
00242 
00243     ``object_hook`` is an optional function that will be called with the
00244     result of any object literal decode (a ``dict``). The return value of
00245     ``object_hook`` will be used instead of the ``dict``. This feature
00246     can be used to implement custom decoders (e.g. JSON-RPC class hinting).
00247 
00248     ``object_pairs_hook`` is an optional function that will be called with the
00249     result of any object literal decoded with an ordered list of pairs.  The
00250     return value of ``object_pairs_hook`` will be used instead of the ``dict``.
00251     This feature can be used to implement custom decoders that rely on the
00252     order that the key and value pairs are decoded (for example,
00253     collections.OrderedDict will remember the order of insertion). If
00254     ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
00255 
00256     To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
00257     kwarg; otherwise ``JSONDecoder`` is used.
00258 
00259     """
00260     return loads(fp.read(),
00261         cls=cls, object_hook=object_hook,
00262         parse_float=parse_float, parse_int=parse_int,
00263         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
00264 

Here is the call graph for this function:

Here is the caller graph for this function:

def json.loads (   s,
  encoding = None,
  cls = None,
  object_hook = None,
  parse_float = None,
  parse_int = None,
  parse_constant = None,
  object_pairs_hook = None,
  kw 
)
Deserialize ``s`` (a ``str`` instance containing a JSON
document) to a Python object.

``object_hook`` is an optional function that will be called with the
result of any object literal decode (a ``dict``). The return value of
``object_hook`` will be used instead of the ``dict``. This feature
can be used to implement custom decoders (e.g. JSON-RPC class hinting).

``object_pairs_hook`` is an optional function that will be called with the
result of any object literal decoded with an ordered list of pairs.  The
return value of ``object_pairs_hook`` will be used instead of the ``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,
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. 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. float).

``parse_constant``, if specified, will be called with one of the
following strings: -Infinity, Infinity, NaN, null, true, false.
This can be used to raise an exception if invalid JSON numbers
are encountered.

To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg; otherwise ``JSONDecoder`` is used.

The ``encoding`` argument is ignored and deprecated.

Definition at line 266 of file __init__.py.

00266 
00267         parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
00268     """Deserialize ``s`` (a ``str`` instance containing a JSON
00269     document) to a Python object.
00270 
00271     ``object_hook`` is an optional function that will be called with the
00272     result of any object literal decode (a ``dict``). The return value of
00273     ``object_hook`` will be used instead of the ``dict``. This feature
00274     can be used to implement custom decoders (e.g. JSON-RPC class hinting).
00275 
00276     ``object_pairs_hook`` is an optional function that will be called with the
00277     result of any object literal decoded with an ordered list of pairs.  The
00278     return value of ``object_pairs_hook`` will be used instead of the ``dict``.
00279     This feature can be used to implement custom decoders that rely on the
00280     order that the key and value pairs are decoded (for example,
00281     collections.OrderedDict will remember the order of insertion). If
00282     ``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.
00283 
00284     ``parse_float``, if specified, will be called with the string
00285     of every JSON float to be decoded. By default this is equivalent to
00286     float(num_str). This can be used to use another datatype or parser
00287     for JSON floats (e.g. decimal.Decimal).
00288 
00289     ``parse_int``, if specified, will be called with the string
00290     of every JSON int to be decoded. By default this is equivalent to
00291     int(num_str). This can be used to use another datatype or parser
00292     for JSON integers (e.g. float).
00293 
00294     ``parse_constant``, if specified, will be called with one of the
00295     following strings: -Infinity, Infinity, NaN, null, true, false.
00296     This can be used to raise an exception if invalid JSON numbers
00297     are encountered.
00298 
00299     To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
00300     kwarg; otherwise ``JSONDecoder`` is used.
00301 
00302     The ``encoding`` argument is ignored and deprecated.
00303 
00304     """
00305     if (cls is None and object_hook is None and
00306             parse_int is None and parse_float is None and
00307             parse_constant is None and object_pairs_hook is None and not kw):
00308         return _default_decoder.decode(s)
00309     if cls is None:
00310         cls = JSONDecoder
00311     if object_hook is not None:
00312         kw['object_hook'] = object_hook
00313     if object_pairs_hook is not None:
00314         kw['object_pairs_hook'] = object_pairs_hook
00315     if parse_float is not None:
00316         kw['parse_float'] = parse_float
00317     if parse_int is not None:
00318         kw['parse_int'] = parse_int
00319     if parse_constant is not None:
00320         kw['parse_constant'] = parse_constant
00321     return cls(**kw).decode(s)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 [
00002     'dump', 'dumps', 'load', 'loads',
00003     'JSONDecoder', 'JSONEncoder',
00004 ]

Definition at line 101 of file __init__.py.

string json.__author__ = 'Bob Ippolito <bob@redivi.com>'

Definition at line 106 of file __init__.py.

Definition at line 100 of file __init__.py.

tuple json._default_decoder = JSONDecoder(object_hook=None, object_pairs_hook=None)

Definition at line 234 of file __init__.py.

Initial value:
00001 JSONEncoder(
00002     skipkeys=False,
00003     ensure_ascii=True,
00004     check_circular=True,
00005     allow_nan=True,
00006     indent=None,
00007     separators=None,
00008     default=None,
00009 )

Definition at line 111 of file __init__.py.