Something like in https://stackoverflow.com/q/11875770/1812727
class DateTimeEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
def json_dumps(obj):
return json.dumps(obj, cls=DateTimeEncoder)
But like, there are probably more things we could do, or more edge cases to consider. Basically sane default json encoding deserves the pockets treatment.
I'll also accept a link to a simple library that already does this sort of thing.