Skip to content

Instantly share code, notes, and snippets.

View rgegriff's full-sized avatar

George Griffin rgegriff

  • NYC
View GitHub Profile
@rgegriff
rgegriff / encoders.py
Last active February 11, 2021 19:30 — forked from majgis/encoders.py
JSON Encoder and Decoder for datetime and timedelta
# Taken from http://taketwoprogramming.blogspot.com/2009/06/subclassing-jsonencoder-and-jsondecoder.html
from json import JSONEncoder, JSONDecoder
class DateTimeAwareJSONEncoder(JSONEncoder):
"""
Converts a python object, where datetime and timedelta objects are converted
into objects that can be decoded using the DateTimeAwareJSONDecoder.
"""
def default(self, obj):
if isinstance(obj, datetime):
return {