How to unpickle into objects?

59 views
Skip to first unread message

Dan Bar Dov

unread,
Apr 25, 2010, 12:11:57 AM4/25/10
to jsonp...@googlegroups.com
I am pickling a dictionary of objects.
After I unpickle, what I get looks like:
{u'books': {u'The Folk Of The Fringe': {u'py/object': u'__main__.book', u'name': u'The Folk Of The Fringe', u'read': u'0', u'notes': u'', u'grade': u'\n', u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Card Orson Scott'], u'id': 275}, 
u'Ghost': {u'py/object': u'__main__.book', u'name': u'Ghost', u'read': u'1', u'notes': u'', u'grade': u'\n', u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Anthony Piers'], u'id': 276}, 
u'Rocheworld': {u'py/object': u'__main__.book', u'name': u'Rocheworld', u'read': u'1', u'notes': u'', u'grade': u'"5"\n', u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Forward Robert L.'], u'id': 274}, 
u'All Flesh Is Grass': {u'py/object': u'__main__.book', u'name': u'All Flesh Is Grass', u'read': u'0', u'notes': u'', u'grade': u'\n', u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Simak Clifford D.'], u'id': 277}}}

The unpickling does not generate instantiated objects. 
Is it supposed to? or am I missing something.
If I have to instantiate the objects myself, what good is the "object info" in the pickled stream? I know what the data is anyway?

Thanks,
Dan 

--
You received this message because you are subscribed to the Google Groups "jsonpickle" group.
To post to this group, send email to jsonp...@googlegroups.com.
To unsubscribe from this group, send email to jsonpickle+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jsonpickle?hl=en.

David Aguilar

unread,
Apr 25, 2010, 7:37:29 PM4/25/10
to jsonp...@googlegroups.com, jsonp...@googlegroups.com
On Apr 24, 2010, at 9:11 PM, Dan Bar Dov <bar...@gmail.com> wrote:

> I am pickling a dictionary of objects.
> After I unpickle, what I get looks like:
> {u'books': {u'The Folk Of The Fringe': {u'py/object':
> u'__main__.book',

Herein lies the problem.
Move 'class book' into a separate, importable module that's not part
of the main script.

I hope that helps,
--David

Victor Stavka

unread,
May 29, 2013, 2:38:31 PM5/29/13
to jsonp...@googlegroups.com
Hi David,
I am having the same problem but I have the classes defined in the separate module. I am just starting my python coding so please excuse me if it is a stupid question. 
Here is the code:
    object1 = dbtest.SubSubDBObject("object1", 1, 1.0, date.today())
    object2 = dbtest.SubDBObject ("object2", 2, 2.0, date.today() + datetime.timedelta(days=1), object1)
    object2.printAll()
    pickled = jsonpickle.encode(object2) 
    print pickled
    object3 = jsonpickle.decode(pickled)
    print(object3)

Here the output I get:
('object1', 1, 1.0, datetime.date(2013, 5, 29))
('object2', 2, 2.0)
2013-05-30
{"py/object": "dbtest.SubDBObject", "testSubObject": {"py/object": "dbtest.SubSubDBObject", "testDate": {"py/object": "datetime.date", "__reduce__": [{"py/type": "datetime.date"}, ["B90FHQ==\n"]]}, "testFloat": 1.0, "testString": "object1", "testInt": 1}, "testString": "object2", "testInt": 2, "testDate": {"py/object": "datetime.date", "__reduce__": [{"py/type": "datetime.date"}, ["B90FHg==\n"]]}, "testFloat": 2.0}
{u'py/object': u'dbtest.SubDBObject', u'testSubObject': {u'py/object': u'dbtest.SubSubDBObject', u'testDate': {u'py/object': u'datetime.date', u'__reduce__': [{u'py/type': u'datetime.date'}, [u'B90FHQ==\n']]}, u'testFloat': 1.0, u'testString': u'object1', u'testInt': 1}, u'testString': u'object2', u'testDate': {u'py/object': u'datetime.date', u'__reduce__': [{u'py/type': u'datetime.date'}, [u'B90FHg==\n']]}, u'testInt': 2, u'testFloat': 2.0}

I am using python 2.7.3 and fresh jsonpickle from github.
Thanks a lot,
Victor

Victor Stavka

unread,
May 30, 2013, 10:05:07 AM5/30/13
to jsonp...@googlegroups.com
Nevermind, figured it out.
Your objects have to inherit from "object", that solved the problem
Cheers,
Victor


On Sunday, April 25, 2010 12:11:57 AM UTC-4, dbbd wrote:

David Aguilar

unread,
May 30, 2013, 10:03:29 PM5/30/13
to jsonpickle
On Thu, May 30, 2013 at 7:05 AM, Victor Stavka <sta...@gmail.com> wrote:
> Nevermind, figured it out.
> Your objects have to inherit from "object", that solved the problem

Ah, I'm glad that worked for you. Sorry I hadn't responded earlier
(very busy over here).

I'll create an issue to "update the docs to mention that you must
inherit from object" so that this is more obvious.

cheers,
David


> On Sunday, April 25, 2010 12:11:57 AM UTC-4, dbbd wrote:
>>
>> I am pickling a dictionary of objects.
>> After I unpickle, what I get looks like:
>> {u'books': {u'The Folk Of The Fringe': {u'py/object': u'__main__.book',
>> u'name': u'The Folk Of The Fringe', u'read': u'0', u'notes': u'', u'grade':
>> u'\n', u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Card Orson Scott'],
>> u'id': 275},
>> u'Ghost': {u'py/object': u'__main__.book', u'name': u'Ghost', u'read':
>> u'1', u'notes': u'', u'grade': u'\n', u'purchased': u'1/6/1996 0:00:00',
>> u'authors': [u'Anthony Piers'], u'id': 276},
>> u'Rocheworld': {u'py/object': u'__main__.book', u'name': u'Rocheworld',
>> u'read': u'1', u'notes': u'', u'grade': u'"5"\n', u'purchased': u'1/6/1996
>> 0:00:00', u'authors': [u'Forward Robert L.'], u'id': 274},
>> u'All Flesh Is Grass': {u'py/object': u'__main__.book', u'name': u'All
>> Flesh Is Grass', u'read': u'0', u'notes': u'', u'grade': u'\n',
>> u'purchased': u'1/6/1996 0:00:00', u'authors': [u'Simak Clifford D.'],
>> u'id': 277}}}
>>
>> The unpickling does not generate instantiated objects.
>> Is it supposed to? or am I missing something.
>> If I have to instantiate the objects myself, what good is the "object
>> info" in the pickled stream? I know what the data is anyway?
>>
>> Thanks,
>> Dan

--
David
Reply all
Reply to author
Forward
0 new messages