Skip to content

Instantly share code, notes, and snippets.

@dolinsky
Created February 20, 2014 22:22
Show Gist options
  • Save dolinsky/9124542 to your computer and use it in GitHub Desktop.
Save dolinsky/9124542 to your computer and use it in GitHub Desktop.
Custom JSONEncoder to handle date() encoding in Flask
#!/usr/bin/env python
from sandman import app
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
if isinstance(obj, date):
return obj.isoformat()
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
app.json_encoder = CustomJSONEncoder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment