ijl / orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy
Apache License 2.0
6.06k stars 209 forks source link

Support freezegun fake types #481

Closed nhairs closed 4 months ago

nhairs commented 4 months ago

freezegun is a utility library commonly used in testing to control the time. In doing so it causes the datetime library to produce fake types.

Although these types correctly identify themselves as instances of their relevant type, orjson does not identify them as such leading to errors.

Although it would be possible to use the default argument, this may not always be feasible for the calling library. It would be great if orjson could natively support these.

>>> import datetime
>>> import freezegun.api
>>> isinstance(freezegun.api.FakeDate(2024,5,5), datetime.date)
True
>>> isinstance(freezegun.api.FakeDatetime(2024, 5, 5), datetime.datetime)
True
>>> import datetime
>>> import freezegun.api
>>> import orjson
>>> now = datetime.datetime.now()
>>> orjson.dumps(now)
b'"2023-08-21T11:29:54.532301"'
>>> orjson.dumps(freezegun.api.datetime_to_fakedatetime(now))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Type is not JSON serializable: FakeDatetime