saving and loading objects from file using jsonpickle
Asked Answered
A

3

24

I have the following simple methods for writing a python object to a file using jsonpickle:

def json_serialize(obj, filename, use_jsonpickle=True):
    f = open(filename, 'w')
    if use_jsonpickle:
        import jsonpickle
        json_obj = jsonpickle.encode(obj)
        f.write(json_obj)
    else:
        simplejson.dump(obj, f) 
    f.close()

def json_load_file(filename, use_jsonpickle=True):
    f = open(filename)
    if use_jsonpickle:
        import jsonpickle
        json_str = f.read()
        obj = jsonpickle.decode(json_str)
    else:
        obj = simplejson.load(f)
    return obj

the problem is that whenever I use these, it loads my objects back as dictionaries (that have fields like: "py/object": "my_module.MyClassName") but not as an actual Python object of the type that was used to generate the json string. How can I make it so jsonpickle actually converts the loaded string back to the object?

to illustrate this with an example, consider the following:

class Foo:
    def __init__(self, hello):
    self.hello = hello

# make a Foo obj
obj = Foo("hello world")
obj_str = jsonpickle.encode(obj)
restored_obj = jsonpickle.decode(obj_str)
list_objects = [restored_obj]
# We now get a list with a dictionary, rather than
# a list containing a Foo object
print "list_objects: ", list_objects

This yields:

list_objects:  [{'py/object': 'as_events.Foo', 'hello': 'hello world'}]

Rather than something like: [Foo()]. How can I fix this?

thanks.

Adamson answered 7/3, 2010 at 21:39 Comment(2)
Can you import as_events before jsonpickle.decode() line?Fifteenth
FWIW this is a great example of when you should use the with open() context manager for file writing. When you run into errors serializing here you'll leave your files open--that's bad.Crossbow
A
24

The correct answer was that I was not inheriting from object. Without inheriting from object, jsonpickle cannot correctly decode classes that take one or more arguments in the constructor, it seems. I am by no means an expert but making it Foo(object): rather than Foo: in the class declaration fixed it.

Adamson answered 9/3, 2010 at 0:27 Comment(0)
F
4

Make sure that use_jsonpickle == True in json_load_file(). It seems that you serialize using jsonpickle and load using json.

>>> import jsonpickle
>>> class A(object):
...    def __init__(self, name):
...       self.name = name
... 
>>> js = jsonpickle.encode(A('abc'))
>>> js
'{"py/object": "__main__.A", "name": "abc"}'     # <-- json string
>>> a = jsonpickle.decode(js)
>>> a
<__main__.A object at 0x7f826a87bd90>            # <-- python object
>>> a.name
u'abc'
>>> import json
>>> b = json.loads(js)
>>> b
{u'py/object': u'__main__.A', u'name': u'abc'}    # <-- dictionary

Make sure that object type is available

>>> del A
>>> c = jsonpickle.decode(js)                  # no type available
>>> c
{u'py/object': u'__main__.A', u'name': u'abc'}
>>> type(c)
<type 'dict'>
>>> class A(object):
...    def __init__(self, name):
...        self.name = name
... 
>>> d = jsonpickle.decode(js)                   # type is available
>>> d
<__main__.A object at 0x7f826a87bdd0>
>>> type(d)
<class '__main__.A'>
Fifteenth answered 7/3, 2010 at 22:22 Comment(3)
Can anyone tell me what is this "u" thing in {u'py/object': u'main.A', u'name': u'abc'} ?Iceskate
@Iceskate it is how unicode string literals are written in Python 2. repr(unicode_string) uses the same text representation.Fifteenth
Thank you so much!Iceskate
S
0

As of this posting, there is a bug which causes encoding to be wrong if the serialized object is an inner class. Make sure the class is not located within another class. I've filed an issue with the maintainer. https://github.com/jsonpickle/jsonpickle/issues/210

Stockdale answered 2/7, 2018 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.