Storing instances using jsonpickle

Josh English Joshua.R.English at gmail.com
Mon Sep 15 15:30:44 EDT 2014


On Wednesday, September 3, 2014 7:19:07 PM UTC-7, Ned Batchelder wrote:

> Typically, you need to decide explicitly on a serialized representation 
> for your data.  Even if it's JSON, you need to decide what that JSON 
> looks like.  Then you need to write code that converts the JSON-able 
> data structure (dict of lists, whatever) into your object.  Often a 
> version number is a good idea so that you have some chance of using old 
> data as your code changes.
> 

Right now my cheap workaround is to define a function that saves the instances __dict__ using json, and to recreate the object, I create a new object using the __init__ method and cycle through the rest of the json keys and apply them to the new object using setattr.

It's a quick and dirty hack, but it seems to be working. I do have to make sure that everything that lands in the instance's __dict__ is serializable, but that's not so tough.

I need to add a version number, though. Good idea, that.


Josh



More information about the Python-list mailing list