You can write your own serializer to work with json, but why not use pyyaml
, which supports it out of the box:
>>> import yaml >>> class Foo: ... def bar(self): ... print 'Hello I am bar' ... def zoo(self,i): ... self.i = i ... print "Eye is ",i ... >>> f = Foo() >>> f.zoo(2) Eye is 2 >>> s = yaml.dump(f) >>> f2 = yaml.load(s) >>> f2.zoo(3) Eye is 3 >>> s '!!python/object:__main__.Foo {i: 2}\n' >>> f2 = yaml.load(s) >>> f2.i 2
source share