Skip to content
Snippets Groups Projects
Commit 0b63b759242c authored by Bob Ippolito's avatar Bob Ippolito
Browse files

small decoder optimization

git-svn-id: http://simplejson.googlecode.com/svn/trunk@46 a4795897-2c25-0410-b006-0d3caba88fa1
parent 3dd63130ded9
No related branches found
No related tags found
No related merge requests found
......@@ -212,7 +212,10 @@
separators=separators, encoding=encoding,
**kw).encode(obj)
def load(fp, encoding=None, cls=None, object_hook=None, **kw):
_default_decoder = JSONDecoder(encoding=None, object_hook=None)
def load(fp, encoding=None, cls=None, object_hook=None,
_decode=_default_decoder.decode, **kw):
"""
Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object.
......@@ -232,9 +235,11 @@
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg.
"""
if cls is None and encoding is None and object_hook is None and not kw:
return _decode(fp.read())
if cls is None:
cls = JSONDecoder
if object_hook is not None:
kw['object_hook'] = object_hook
return cls(encoding=encoding, **kw).decode(fp.read())
......@@ -235,10 +240,11 @@
if cls is None:
cls = JSONDecoder
if object_hook is not None:
kw['object_hook'] = object_hook
return cls(encoding=encoding, **kw).decode(fp.read())
def loads(s, encoding=None, cls=None, object_hook=None, **kw):
def loads(s, encoding=None, cls=None, object_hook=None,
_decode=_default_decoder.decode, **kw):
"""
Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
document) to a Python object.
......@@ -256,6 +262,8 @@
To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``
kwarg.
"""
if cls is None and encoding is None and object_hook is None and not kw:
return _decode(s)
if cls is None:
cls = JSONDecoder
if object_hook is not None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment