Created
February 20, 2014 22:22
-
-
Save dolinsky/9124542 to your computer and use it in GitHub Desktop.
Custom JSONEncoder to handle date() encoding in Flask
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
#!/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