Skip to content

Instantly share code, notes, and snippets.

@sweemeng
Created June 22, 2011 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sweemeng/1040013 to your computer and use it in GitHub Desktop.
Save sweemeng/1040013 to your computer and use it in GitHub Desktop.
Output Objects To JSON
import json
"""
>>> c = C()
>>> c.a = 1
>>> c.b = 2
>>> c.__dict__
{'a':1,'b':2}
>>> c.to_json()
'{"a":1,"b":2}'
>>> json.dumps(c.__dict__)
'{"a":1,"b":2}'
"""
class C:
def to_json(self):
json.dumps(self.__dict__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment