Created
February 12, 2015 22:43
-
-
Save juztin/c997c197fadfa870c816 to your computer and use it in GitHub Desktop.
Python JSON Decimal/time Encoder
This file contains hidden or 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
class JSONEncoder(json.JSONEncoder): | |
"""JSONEncoder that handles decimal and time types. | |
""" | |
def default(self, o): | |
if isinstance(o, decimal.Decimal): | |
return float(o) | |
elif hasattr(o, 'isoformat'): | |
return o.isoformat() | |
return super(DecimalEncoder, self).default(o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment