app/simplejson/__init__.py
author Pawel Solyga <Pawel.Solyga@gmail.com>
Sun, 25 Jan 2009 11:31:15 +0000
changeset 975 295d67509412
permissions -rw-r--r--
Add simplejson library to app folder. Patch by: Pawel Solyga Reviewed by: to-be-reviewed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
975
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
r"""JSON (JavaScript Object Notation) <http://json.org> is a subset of
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
interchange format.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
:mod:`simplejson` exposes an API familiar to users of the standard library
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
:mod:`marshal` and :mod:`pickle` modules. It is the externally maintained
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
version of the :mod:`json` library contained in Python 2.6, but maintains
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
compatibility with Python 2.4 and Python 2.5 and (currently) has
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
significant performance advantages, even without using the optional C
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
extension for speedups.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
Encoding basic Python object hierarchies::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
    >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
    '["foo", {"bar": ["baz", null, 1.0, 2]}]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
    >>> print json.dumps("\"foo\bar")
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
    "\"foo\bar"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
    >>> print json.dumps(u'\u1234')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
    "\u1234"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
    >>> print json.dumps('\\')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
    "\\"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
    >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
    {"a": 0, "b": 0, "c": 0}
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
    >>> from StringIO import StringIO
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
    >>> io = StringIO()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
    >>> json.dump(['streaming API'], io)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
    >>> io.getvalue()
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
    '["streaming API"]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
Compact encoding::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
    >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':'))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
    '[1,2,3,{"4":5,"6":7}]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
Pretty printing::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
    >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
    >>> print '\n'.join([l.rstrip() for l in  s.splitlines()])
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
    {
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
        "4": 5,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
        "6": 7
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
    }
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
Decoding JSON::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
    >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
    >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
    True
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
    >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    54
    True
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
    >>> from StringIO import StringIO
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
    >>> io = StringIO('["streaming API"]')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
    >>> json.load(io)[0] == 'streaming API'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
    True
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
Specializing JSON object decoding::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
    >>> def as_complex(dct):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
    ...     if '__complex__' in dct:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    65
    ...         return complex(dct['real'], dct['imag'])
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
    ...     return dct
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
    ...
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
    >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
    ...     object_hook=as_complex)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
    (1+2j)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    71
    >>> import decimal
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
    >>> json.loads('1.1', parse_float=decimal.Decimal) == decimal.Decimal('1.1')
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
    True
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
Specializing JSON object encoding::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    76
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    77
    >>> import simplejson as json
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
    >>> def encode_complex(obj):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    79
    ...     if isinstance(obj, complex):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    80
    ...         return [obj.real, obj.imag]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    81
    ...     raise TypeError("%r is not JSON serializable" % (o,))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    82
    ...
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    83
    >>> json.dumps(2 + 1j, default=encode_complex)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
    '[2.0, 1.0]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    85
    >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
    '[2.0, 1.0]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
    >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j))
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
    '[2.0, 1.0]'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    89
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    91
Using simplejson.tool from the shell to validate and pretty-print::
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    92
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
    $ echo '{"json":"obj"}' | python -msimplejson.tool
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
    {
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    95
        "json": "obj"
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    96
    }
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    97
    $ echo '{ 1.2:3.4}' | python -msimplejson.tool
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    98
    Expecting property name: line 1 column 2 (char 2)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    99
"""
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
__version__ = '2.0.7'
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   101
__all__ = [
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
    'dump', 'dumps', 'load', 'loads',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
    'JSONDecoder', 'JSONEncoder',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
]
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   106
from decoder import JSONDecoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   107
from encoder import JSONEncoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   108
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   109
_default_encoder = JSONEncoder(
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   110
    skipkeys=False,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   111
    ensure_ascii=True,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   112
    check_circular=True,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   113
    allow_nan=True,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   114
    indent=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   115
    separators=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   116
    encoding='utf-8',
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   117
    default=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   118
)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   119
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   120
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   121
        allow_nan=True, cls=None, indent=None, separators=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
        encoding='utf-8', default=None, **kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   123
    """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   124
    ``.write()``-supporting file-like object).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   125
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   126
    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   127
    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   128
    will be skipped instead of raising a ``TypeError``.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   129
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   130
    If ``ensure_ascii`` is ``False``, then the some chunks written to ``fp``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   131
    may be ``unicode`` instances, subject to normal Python ``str`` to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   132
    ``unicode`` coercion rules. Unless ``fp.write()`` explicitly
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   133
    understands ``unicode`` (as in ``codecs.getwriter()``) this is likely
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   134
    to cause an error.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   135
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   136
    If ``check_circular`` is ``False``, then the circular reference check
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   137
    for container types will be skipped and a circular reference will
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   138
    result in an ``OverflowError`` (or worse).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   139
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   140
    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   141
    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   142
    in strict compliance of the JSON specification, instead of using the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   143
    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   144
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   145
    If ``indent`` is a non-negative integer, then JSON array elements and object
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   146
    members will be pretty-printed with that indent level. An indent level
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   147
    of 0 will only insert newlines. ``None`` is the most compact representation.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   148
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   149
    If ``separators`` is an ``(item_separator, dict_separator)`` tuple
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   150
    then it will be used instead of the default ``(', ', ': ')`` separators.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   151
    ``(',', ':')`` is the most compact JSON representation.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   152
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   153
    ``encoding`` is the character encoding for str instances, default is UTF-8.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   154
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   155
    ``default(obj)`` is a function that should return a serializable version
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   156
    of obj or raise TypeError. The default simply raises TypeError.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   157
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   158
    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   159
    ``.default()`` method to serialize additional types), specify it with
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   160
    the ``cls`` kwarg.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   161
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   162
    """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   163
    # cached encoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   164
    if (skipkeys is False and ensure_ascii is True and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   165
        check_circular is True and allow_nan is True and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   166
        cls is None and indent is None and separators is None and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   167
        encoding == 'utf-8' and default is None and not kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   168
        iterable = _default_encoder.iterencode(obj)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   169
    else:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   170
        if cls is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   171
            cls = JSONEncoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   172
        iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   173
            check_circular=check_circular, allow_nan=allow_nan, indent=indent,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   174
            separators=separators, encoding=encoding,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   175
            default=default, **kw).iterencode(obj)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   176
    # could accelerate with writelines in some versions of Python, at
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   177
    # a debuggability cost
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   178
    for chunk in iterable:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   179
        fp.write(chunk)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   180
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   181
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   182
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   183
        allow_nan=True, cls=None, indent=None, separators=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   184
        encoding='utf-8', default=None, **kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   185
    """Serialize ``obj`` to a JSON formatted ``str``.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   186
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   187
    If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   188
    (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   189
    will be skipped instead of raising a ``TypeError``.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   190
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   191
    If ``ensure_ascii`` is ``False``, then the return value will be a
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   192
    ``unicode`` instance subject to normal Python ``str`` to ``unicode``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   193
    coercion rules instead of being escaped to an ASCII ``str``.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   194
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   195
    If ``check_circular`` is ``False``, then the circular reference check
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   196
    for container types will be skipped and a circular reference will
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   197
    result in an ``OverflowError`` (or worse).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   198
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   199
    If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   200
    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   201
    strict compliance of the JSON specification, instead of using the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   202
    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   203
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   204
    If ``indent`` is a non-negative integer, then JSON array elements and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   205
    object members will be pretty-printed with that indent level. An indent
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   206
    level of 0 will only insert newlines. ``None`` is the most compact
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   207
    representation.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   208
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   209
    If ``separators`` is an ``(item_separator, dict_separator)`` tuple
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   210
    then it will be used instead of the default ``(', ', ': ')`` separators.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   211
    ``(',', ':')`` is the most compact JSON representation.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   212
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   213
    ``encoding`` is the character encoding for str instances, default is UTF-8.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   214
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   215
    ``default(obj)`` is a function that should return a serializable version
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   216
    of obj or raise TypeError. The default simply raises TypeError.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   217
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   218
    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   219
    ``.default()`` method to serialize additional types), specify it with
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   220
    the ``cls`` kwarg.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   221
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   222
    """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   223
    # cached encoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   224
    if (skipkeys is False and ensure_ascii is True and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   225
        check_circular is True and allow_nan is True and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   226
        cls is None and indent is None and separators is None and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   227
        encoding == 'utf-8' and default is None and not kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   228
        return _default_encoder.encode(obj)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   229
    if cls is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   230
        cls = JSONEncoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   231
    return cls(
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   232
        skipkeys=skipkeys, ensure_ascii=ensure_ascii,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   233
        check_circular=check_circular, allow_nan=allow_nan, indent=indent,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   234
        separators=separators, encoding=encoding, default=default,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   235
        **kw).encode(obj)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   236
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   237
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   238
_default_decoder = JSONDecoder(encoding=None, object_hook=None)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   239
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   240
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   241
def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   242
        parse_int=None, parse_constant=None, **kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   243
    """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   244
    a JSON document) to a Python object.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   245
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   246
    If the contents of ``fp`` is encoded with an ASCII based encoding other
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   247
    than utf-8 (e.g. latin-1), then an appropriate ``encoding`` name must
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   248
    be specified. Encodings that are not ASCII based (such as UCS-2) are
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   249
    not allowed, and should be wrapped with
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   250
    ``codecs.getreader(fp)(encoding)``, or simply decoded to a ``unicode``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   251
    object and passed to ``loads()``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   252
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   253
    ``object_hook`` is an optional function that will be called with the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   254
    result of any object literal decode (a ``dict``). The return value of
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   255
    ``object_hook`` will be used instead of the ``dict``. This feature
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   256
    can be used to implement custom decoders (e.g. JSON-RPC class hinting).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   257
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   258
    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   259
    kwarg.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   260
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   261
    """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   262
    return loads(fp.read(),
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   263
        encoding=encoding, cls=cls, object_hook=object_hook,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   264
        parse_float=parse_float, parse_int=parse_int,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   265
        parse_constant=parse_constant, **kw)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   266
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   267
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   268
def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   269
        parse_int=None, parse_constant=None, **kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   270
    """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   271
    document) to a Python object.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   272
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   273
    If ``s`` is a ``str`` instance and is encoded with an ASCII based encoding
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   274
    other than utf-8 (e.g. latin-1) then an appropriate ``encoding`` name
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   275
    must be specified. Encodings that are not ASCII based (such as UCS-2)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   276
    are not allowed and should be decoded to ``unicode`` first.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   277
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   278
    ``object_hook`` is an optional function that will be called with the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   279
    result of any object literal decode (a ``dict``). The return value of
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   280
    ``object_hook`` will be used instead of the ``dict``. This feature
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   281
    can be used to implement custom decoders (e.g. JSON-RPC class hinting).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   282
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   283
    ``parse_float``, if specified, will be called with the string
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   284
    of every JSON float to be decoded. By default this is equivalent to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   285
    float(num_str). This can be used to use another datatype or parser
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   286
    for JSON floats (e.g. decimal.Decimal).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   287
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   288
    ``parse_int``, if specified, will be called with the string
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   289
    of every JSON int to be decoded. By default this is equivalent to
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   290
    int(num_str). This can be used to use another datatype or parser
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   291
    for JSON integers (e.g. float).
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   292
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   293
    ``parse_constant``, if specified, will be called with one of the
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   294
    following strings: -Infinity, Infinity, NaN, null, true, false.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   295
    This can be used to raise an exception if invalid JSON numbers
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   296
    are encountered.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   297
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   298
    To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   299
    kwarg.
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   300
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   301
    """
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   302
    if (cls is None and encoding is None and object_hook is None and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   303
            parse_int is None and parse_float is None and
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   304
            parse_constant is None and not kw):
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   305
        return _default_decoder.decode(s)
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   306
    if cls is None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   307
        cls = JSONDecoder
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   308
    if object_hook is not None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   309
        kw['object_hook'] = object_hook
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   310
    if parse_float is not None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   311
        kw['parse_float'] = parse_float
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   312
    if parse_int is not None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   313
        kw['parse_int'] = parse_int
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   314
    if parse_constant is not None:
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   315
        kw['parse_constant'] = parse_constant
295d67509412 Add simplejson library to app folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   316
    return cls(encoding=encoding, **kw).decode(s)