Skip to content

Instantly share code, notes, and snippets.

@defuz
Created August 14, 2013 18:37
Show Gist options
  • Save defuz/6234090 to your computer and use it in GitHub Desktop.
Save defuz/6234090 to your computer and use it in GitHub Desktop.
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