Skip to content

Commit

Permalink
jsonpickle: push decode() into the unpickler module
Browse files Browse the repository at this point in the history
Refactor the code so that we only have a single decode() function.
Expose the Unpickler class.

Signed-off-by: David Aguilar <davvid@gmail.com>
  • Loading branch information
davvid committed Mar 10, 2018
1 parent 8e1d18f commit 7965e77
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 34 deletions.
24 changes: 1 addition & 23 deletions jsonpickle/__init__.py
Expand Up @@ -63,6 +63,7 @@ def __init__(self, name):
from .handlers import register # side-effect: registers built-in handlers
from .handlers import unregister
from .pickler import Pickler, encode
from .unpickler import Unpickler, decode

__all__ = ('encode', 'decode')

Expand All @@ -74,29 +75,6 @@ def __init__(self, name):
remove_backend = json.remove_backend
enable_fallthrough = json.enable_fallthrough

def decode(string, backend=None, keys=False, classes=None):
"""Convert a JSON string into a Python object.
The keyword argument 'keys' defaults to False.
If set to True then jsonpickle will decode non-string dictionary keys
into python objects via the jsonpickle protocol.
The keyword argument 'classes' defaults to None.
If set to a single class, or a sequence (list, set, tuple) of classes,
then the classes will be made available when constructing objects. This
can be used to give jsonpickle access to local classes that are not
available through the global module import scope.
>>> str(decode('"my string"'))
'my string'
>>> decode('36')
36
"""
if backend is None:
backend = json
return unpickler.decode(string, backend=backend, keys=keys, classes=classes)


# json.load(),loads(), dump(), dumps() compatibility
dumps = encode
loads = decode
32 changes: 21 additions & 11 deletions jsonpickle/unpickler.py
Expand Up @@ -15,22 +15,32 @@
from . import tags
from . import handlers
from .compat import numeric_types, set, unicode
from .backend import JSONBackend
from .backend import JSONBackend, json


def decode(string, backend=None, context=None, keys=False, reset=True,
safe=False, classes=None):
backend = _make_backend(backend)
if context is None:
context = Unpickler(keys=keys, backend=backend, safe=safe)
return context.restore(backend.decode(string), reset=reset, classes=classes)
"""Convert a JSON string into a Python object.
The keyword argument 'keys' defaults to False.
If set to True then jsonpickle will decode non-string dictionary keys
into python objects via the jsonpickle protocol.
def _make_backend(backend):
if backend is None:
return JSONBackend()
else:
return backend
The keyword argument 'classes' defaults to None.
If set to a single class, or a sequence (list, set, tuple) of classes,
then the classes will be made available when constructing objects. This
can be used to give jsonpickle access to local classes that are not
available through the global module import scope.
>>> decode('"my string"') == 'my string'
True
>>> decode('36')
36
"""
backend = backend or json
context = context or Unpickler(keys=keys, backend=backend, safe=safe)
data = backend.decode(string)
return context.restore(data, reset=reset, classes=classes)


def _safe_hasattr(obj, attr):
Expand Down Expand Up @@ -96,7 +106,7 @@ def _obj_setvalue(obj, idx, proxy):
class Unpickler(object):

def __init__(self, backend=None, keys=False, safe=False):
self.backend = _make_backend(backend)
self.backend = backend or json
self.keys = keys
self.safe = safe

Expand Down

0 comments on commit 7965e77

Please sign in to comment.