Last active
December 7, 2015 13:47
-
-
Save telendt/b5e82cbfc29df95a517a to your computer and use it in GitHub Desktop.
Serializable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"b": { | |
"param": "yello!" | |
}, | |
"a": "test" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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