datetime support break default function
See original GitHub issueBefore version 2.0.2 we use DjangoJSONEncoder.default
to handle datatime, but it doesn’t works now, and the orjson datetime is not compatible with the logic below:
https://github.com/django/django/blob/master/django/core/serializers/json.py#L81
def default(self, o):
# See "Date Time String Format" in the ECMA-262 specification.
if isinstance(o, datetime.datetime):
r = o.isoformat()
if o.microsecond:
r = r[:23] + r[26:]
if r.endswith('+00:00'):
r = r[:-6] + 'Z'
return r
elif isinstance(o, datetime.date):
return o.isoformat()
elif isinstance(o, datetime.time):
if is_aware(o):
raise ValueError("JSON can't represent timezone-aware times.")
r = o.isoformat()
if o.microsecond:
r = r[:12]
return r
elif isinstance(o, datetime.timedelta):
return duration_iso_string(o)
elif isinstance(o, (decimal.Decimal, uuid.UUID, Promise)):
return str(o)
else:
return super().default(o)
Is there a way to make default works with datetime object?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Does inheritance of DateTime change any default function ...
When you implement/extend a class, the parent class (in this case DateTime ) will pass its methods to the child class( OwnDate ),...
Read more >Demystifying DateTime Manipulation in JavaScript - Toptal
This in-depth guide to DateTime manipulation should help you understand the programming concepts and best practices relevant to time and date without having ......
Read more >DateTime Functions - Alteryx Help
Use a DateTime function to add or subtract intervals, find the current date, find the first or last day of the month, extract...
Read more >Datetime functions | BigQuery - Google Cloud
Constructs a DATETIME object using a TIMESTAMP object. It supports an optional parameter to specify a time zone. If no time zone is...
Read more >Choose the right date function - Microsoft Support
Date functions like DateAdd, DatePart, and DateSerial help you calculate and display dates the way you want.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ll look into overriding orjson serializing datetime objects. It also makes sense for another use case to allow serializing
datetime.datetime
objects without atzinfo
. I could make orjson output three digits of microseconds and match Django, but I don’t see a reason to prefer that over Python’s full precision, so it looks like you do need to avoid its handling.I think it is ok now, three digits or six digits is ok. Thank you very much!