Skip to content
This repository has been archived by the owner on Sep 17, 2018. It is now read-only.

Commit

Permalink
Updated to the latest git version of simplejson.
Browse files Browse the repository at this point in the history
  • Loading branch information
tav committed May 17, 2011
1 parent 8a48383 commit 3c16b84
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 155 deletions.
70 changes: 51 additions & 19 deletions simplejson/__init__.py
Expand Up @@ -97,7 +97,7 @@
$ echo '{ 1.2:3.4}' | python -m simplejson.tool $ echo '{ 1.2:3.4}' | python -m simplejson.tool
Expecting property name: line 1 column 2 (char 2) Expecting property name: line 1 column 2 (char 2)
""" """
__version__ = '2.1.0rc2' __version__ = '2.1.7'
__all__ = [ __all__ = [
'dump', 'dumps', 'load', 'loads', 'dump', 'dumps', 'load', 'loads',
'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder',
Expand All @@ -106,12 +106,25 @@


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


from decimal import Decimal

from decoder import JSONDecoder, JSONDecodeError from decoder import JSONDecoder, JSONDecodeError
from encoder import JSONEncoder from encoder import JSONEncoder
try: def _import_OrderedDict():
from collections import OrderedDict import collections
except ImportError: try:
from ordered_dict import OrderedDict return collections.OrderedDict
except AttributeError:
import ordered_dict
return ordered_dict.OrderedDict
OrderedDict = _import_OrderedDict()

def _import_c_make_encoder():
try:
from simplejson._speedups import make_encoder
return make_encoder
except ImportError:
return None


_default_encoder = JSONEncoder( _default_encoder = JSONEncoder(
skipkeys=False, skipkeys=False,
Expand All @@ -122,11 +135,12 @@
separators=None, separators=None,
encoding='utf-8', encoding='utf-8',
default=None, default=None,
use_decimal=False,
) )


def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None, allow_nan=True, cls=None, indent=None, separators=None,
encoding='utf-8', default=None, **kw): encoding='utf-8', default=None, use_decimal=False, **kw):
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
``.write()``-supporting file-like object). ``.write()``-supporting file-like object).
Expand Down Expand Up @@ -165,6 +179,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
``default(obj)`` is a function that should return a serializable version ``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError. of obj or raise TypeError. The default simply raises TypeError.
If *use_decimal* is true (default: ``False``) then decimal.Decimal
will be natively serialized to JSON with full precision.
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with ``.default()`` method to serialize additional types), specify it with
the ``cls`` kwarg. the ``cls`` kwarg.
Expand All @@ -174,15 +191,16 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
if (not skipkeys and ensure_ascii and if (not skipkeys and ensure_ascii and
check_circular and allow_nan and check_circular and allow_nan and
cls is None and indent is None and separators is None and cls is None and indent is None and separators is None and
encoding == 'utf-8' and default is None and not kw): encoding == 'utf-8' and default is None and not use_decimal
and not kw):
iterable = _default_encoder.iterencode(obj) iterable = _default_encoder.iterencode(obj)
else: else:
if cls is None: if cls is None:
cls = JSONEncoder cls = JSONEncoder
iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
check_circular=check_circular, allow_nan=allow_nan, indent=indent, check_circular=check_circular, allow_nan=allow_nan, indent=indent,
separators=separators, encoding=encoding, separators=separators, encoding=encoding,
default=default, **kw).iterencode(obj) default=default, use_decimal=use_decimal, **kw).iterencode(obj)
# could accelerate with writelines in some versions of Python, at # could accelerate with writelines in some versions of Python, at
# a debuggability cost # a debuggability cost
for chunk in iterable: for chunk in iterable:
Expand All @@ -191,7 +209,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,


def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
allow_nan=True, cls=None, indent=None, separators=None, allow_nan=True, cls=None, indent=None, separators=None,
encoding='utf-8', default=None, **kw): encoding='utf-8', default=None, use_decimal=False, **kw):
"""Serialize ``obj`` to a JSON formatted ``str``. """Serialize ``obj`` to a JSON formatted ``str``.
If ``skipkeys`` is false then ``dict`` keys that are not basic types If ``skipkeys`` is false then ``dict`` keys that are not basic types
Expand Down Expand Up @@ -227,6 +245,9 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
``default(obj)`` is a function that should return a serializable version ``default(obj)`` is a function that should return a serializable version
of obj or raise TypeError. The default simply raises TypeError. of obj or raise TypeError. The default simply raises TypeError.
If *use_decimal* is true (default: ``False``) then decimal.Decimal
will be natively serialized to JSON with full precision.
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
``.default()`` method to serialize additional types), specify it with ``.default()`` method to serialize additional types), specify it with
the ``cls`` kwarg. the ``cls`` kwarg.
Expand All @@ -236,23 +257,25 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
if (not skipkeys and ensure_ascii and if (not skipkeys and ensure_ascii and
check_circular and allow_nan and check_circular and allow_nan and
cls is None and indent is None and separators is None and cls is None and indent is None and separators is None and
encoding == 'utf-8' and default is None and not kw): encoding == 'utf-8' and default is None and not use_decimal
and not kw):
return _default_encoder.encode(obj) return _default_encoder.encode(obj)
if cls is None: if cls is None:
cls = JSONEncoder cls = JSONEncoder
return cls( return cls(
skipkeys=skipkeys, ensure_ascii=ensure_ascii, skipkeys=skipkeys, ensure_ascii=ensure_ascii,
check_circular=check_circular, allow_nan=allow_nan, indent=indent, check_circular=check_circular, allow_nan=allow_nan, indent=indent,
separators=separators, encoding=encoding, default=default, separators=separators, encoding=encoding, default=default,
**kw).encode(obj) use_decimal=use_decimal, **kw).encode(obj)




_default_decoder = JSONDecoder(encoding=None, object_hook=None, _default_decoder = JSONDecoder(encoding=None, object_hook=None,
object_pairs_hook=None) object_pairs_hook=None)




def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): parse_int=None, parse_constant=None, object_pairs_hook=None,
use_decimal=False, **kw):
"""Deserialize ``fp`` (a ``.read()``-supporting file-like object containing """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object. a JSON document) to a Python object.
Expand Down Expand Up @@ -292,6 +315,9 @@ def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
can be used to raise an exception if invalid JSON numbers are can be used to raise an exception if invalid JSON numbers are
encountered. encountered.
If *use_decimal* is true (default: ``False``) then it implies
parse_float=decimal.Decimal for parity with ``dump``.
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg. kwarg.
Expand All @@ -300,11 +326,12 @@ def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None,
encoding=encoding, cls=cls, object_hook=object_hook, encoding=encoding, cls=cls, object_hook=object_hook,
parse_float=parse_float, parse_int=parse_int, parse_float=parse_float, parse_int=parse_int,
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook,
**kw) use_decimal=use_decimal, **kw)




def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): parse_int=None, parse_constant=None, object_pairs_hook=None,
use_decimal=False, **kw):
"""Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
document) to a Python object. document) to a Python object.
Expand Down Expand Up @@ -344,13 +371,17 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
can be used to raise an exception if invalid JSON numbers are can be used to raise an exception if invalid JSON numbers are
encountered. encountered.
If *use_decimal* is true (default: ``False``) then it implies
parse_float=decimal.Decimal for parity with ``dump``.
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg. kwarg.
""" """
if (cls is None and encoding is None and object_hook is None and if (cls is None and encoding is None and object_hook is None and
parse_int is None and parse_float is None and parse_int is None and parse_float is None and
parse_constant is None and object_pairs_hook is None and not kw): parse_constant is None and object_pairs_hook is None
and not use_decimal and not kw):
return _default_decoder.decode(s) return _default_decoder.decode(s)
if cls is None: if cls is None:
cls = JSONDecoder cls = JSONDecoder
Expand All @@ -364,17 +395,18 @@ def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None,
kw['parse_int'] = parse_int kw['parse_int'] = parse_int
if parse_constant is not None: if parse_constant is not None:
kw['parse_constant'] = parse_constant kw['parse_constant'] = parse_constant
if use_decimal:
if parse_float is not None:
raise TypeError("use_decimal=True implies parse_float=Decimal")
kw['parse_float'] = Decimal
return cls(encoding=encoding, **kw).decode(s) return cls(encoding=encoding, **kw).decode(s)




def _toggle_speedups(enabled): def _toggle_speedups(enabled):
import simplejson.decoder as dec import simplejson.decoder as dec
import simplejson.encoder as enc import simplejson.encoder as enc
import simplejson.scanner as scan import simplejson.scanner as scan
try: c_make_encoder = _import_c_make_encoder()
from simplejson._speedups import make_encoder as c_make_encoder
except ImportError:
c_make_encoder = None
if enabled: if enabled:
dec.scanstring = dec.c_scanstring or dec.py_scanstring dec.scanstring = dec.c_scanstring or dec.py_scanstring
enc.c_make_encoder = c_make_encoder enc.c_make_encoder = c_make_encoder
Expand Down

0 comments on commit 3c16b84

Please sign in to comment.