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 @@ ...@@ -212,7 +212,10 @@
separators=separators, encoding=encoding, separators=separators, encoding=encoding,
**kw).encode(obj) **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 Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
a JSON document) to a Python object. a JSON document) to a Python object.
...@@ -232,9 +235,11 @@ ...@@ -232,9 +235,11 @@
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 not kw:
return _decode(fp.read())
if cls is None: if cls is None:
cls = JSONDecoder cls = JSONDecoder
if object_hook is not None: if object_hook is not None:
kw['object_hook'] = object_hook kw['object_hook'] = object_hook
return cls(encoding=encoding, **kw).decode(fp.read()) return cls(encoding=encoding, **kw).decode(fp.read())
...@@ -235,10 +240,11 @@ ...@@ -235,10 +240,11 @@
if cls is None: if cls is None:
cls = JSONDecoder cls = JSONDecoder
if object_hook is not None: if object_hook is not None:
kw['object_hook'] = object_hook kw['object_hook'] = object_hook
return cls(encoding=encoding, **kw).decode(fp.read()) 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 Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON
document) to a Python object. document) to a Python object.
...@@ -256,6 +262,8 @@ ...@@ -256,6 +262,8 @@
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 not kw:
return _decode(s)
if cls is None: if cls is None:
cls = JSONDecoder cls = JSONDecoder
if object_hook is not None: 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