Skip to content

Commit

Permalink
io.json: Serialize date objects as ISO 8601 too
Browse files Browse the repository at this point in the history
No reason to error on them, and we're going to start getting them when
reading metadata from Excel.
  • Loading branch information
tsibley committed Jul 17, 2024
1 parent d1a7ddb commit e20f920
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions augur/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
SOFTWARE.
"""
import json
from datetime import datetime
from datetime import date, datetime
from typing import Iterable
from uuid import UUID

Expand All @@ -41,7 +41,12 @@ def as_json(value):
"""
Converts *value* to a JSON string using our custom :class:`JsonEncoder`.
The custom encoder supports serialization of :class:`~datetime.datetime` objects:
The custom encoder supports serialization of :class:`~datetime.date` objects:
>>> as_json(date(year=2024, month=7, day=17))
'"2024-07-17"'
:class:`~datetime.datetime` objects:
>>> as_json(datetime(year=2024, month=7, day=17, hour=11, minute=38))
'"2024-07-17T11:38:00"'
Expand Down Expand Up @@ -106,10 +111,11 @@ def default(self, value):
"""
Returns *value* as JSON or raises a TypeError.
Serializes:
* :class:`~datetime.date` using :meth:`~datetime.date.isoformat()`
* :class:`~datetime.datetime` using :meth:`~datetime.datetime.isoformat()`
* :class:`~uuid.UUID` using ``str()``
"""
if isinstance(value, datetime):
if isinstance(value, (date, datetime)):
return value.isoformat()

elif isinstance(value, UUID):
Expand Down

0 comments on commit e20f920

Please sign in to comment.