Created
August 14, 2013 18:37
-
-
Save defuz/6234090 to your computer and use it in GitHub Desktop.
This file contains 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 Foo(object): | |
def __init__(self, **kwargs): | |
self.__dict__.update(kwargs) | |
class MyEncoder(json.JSONEncoder): | |
def default(self, obj): | |
if isinstance(obj, Foo): | |
return obj.__dict__ | |
return json.JSONEncoder.default(obj) | |
x = Foo(a=42, b=Foo(c=23)) | |
print MyEncoder().encode(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment