Skip to content

Instantly share code, notes, and snippets.

@telendt
Last active December 7, 2015 13:47
Show Gist options
  • Save telendt/b5e82cbfc29df95a517a to your computer and use it in GitHub Desktop.
Save telendt/b5e82cbfc29df95a517a to your computer and use it in GitHub Desktop.
Serializable
{
"b": {
"param": "yello!"
},
"a": "test"
}
import json
class Serializable(object):
class _CustomEncoder(json.JSONEncoder):
def default(self, obj):
return obj.__dict__ if isinstance(obj, Serializable) else obj
def __repr__(self):
return json.dumps(self, indent=2, cls=Serializable._CustomEncoder)
class A(Serializable):
def __init__(self, a):
self.a = a
self.b = B("yello!")
class B(Serializable):
def __init__(self, param):
self.param = param
print(A("test"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment