Skip to content

Instantly share code, notes, and snippets.

@atiaxi
Created December 15, 2014 21:12
Show Gist options
  • Save atiaxi/c3662f5947fa33cfff92 to your computer and use it in GitHub Desktop.
Save atiaxi/c3662f5947fa33cfff92 to your computer and use it in GitHub Desktop.
import json
class Blarg:
def __init__(self, something=1):
self.something=something
self.werg = {"another thing": 1}
def to_json(self):
return json.dumps({
"something": self.something,
"werg": self.werg,
})
@classmethod
def from_json(cls, json_string):
json_dict = json.loads(json_string)
proto = cls(json_dict['something'])
proto.werg = json_dict['werg']
return proto
b = Blarg("hi")
stored = b.to_json()
print stored
restored = Blarg.from_json(stored)
print restored.something
print restored.werg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment