Should be able to with:
default = lambda o: o.isoformat() if isinstance(
o, (datetime.date, datetime.datetime)) else o.__dict__
You can use None
as well for your return, but see what happens when you do that if you were to include a custom object that JSONEncoder
doesn't pick up on:
default = lambda o: o.isoformat() if isinstance(
o, (datetime.date, datetime.datetime)) else None
class A:
def __init__(self, x):
self.x = x
a = A(10)
d = {'date': datetime.datetime.now(), 'myobject': a}
f = json.dumps(z, default=default)
>>> json.loads(f)
{'date': '2020-08-13T22:35:46.872594', 'myobject': None}
Same thing but with o.__dict__
:
>>> json.loads(f)
{'date': '2020-08-13T22:36:22.512864', 'myobject': {'x': 10}}
Although with a custom object ideally you should have __repr__
set up well and then you can even recreate your object.