Skip to content

Instantly share code, notes, and snippets.

@bryder
Last active April 28, 2016 20:15
Show Gist options
  • Save bryder/8858d2f3e65d20625d17 to your computer and use it in GitHub Desktop.
Save bryder/8858d2f3e65d20625d17 to your computer and use it in GitHub Desktop.
make json.dumps serialise common things. Doh.
# from stack overflow http://stackoverflow.com/questions/8230315/python-sets-are-not-json-serializable and others
class SerialiseThis(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, set):
return list(obj)
if isinstance(obj, datetime.datetime):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
json.dumps(set([1,2,3,4,5,Something()]), cls=SerialseThis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment