Skip to content

Instantly share code, notes, and snippets.

@tokibito
Created March 8, 2012 09:50
Show Gist options
  • Save tokibito/2000026 to your computer and use it in GitHub Desktop.
Save tokibito/2000026 to your computer and use it in GitHub Desktop.
# coding: utf-8
import json
class Spam(object):
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return "<%s: name=%s, value=%s>" % (
self.__class__.__name__,
self.name,
self.value)
def as_dict(self):
return {'name': self.name, 'value': self.value}
@classmethod
def from_dict(cls, data):
return cls(data['name'], data['value'])
def main():
obj = Spam('Furukawa', 'bucho')
print "original: %s" % obj
s = json.dumps(obj.as_dict())
print "======================="
print s
print "======================="
obj_2 = Spam.from_dict(json.loads(s))
print "json.loads: %s" % obj_2
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment