Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/cattrs/preconf/json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Preconfigured converters for the stdlib json."""
from base64 import b85decode, b85encode
from datetime import datetime
from datetime import datetime, date, time
from json import dumps, loads
from typing import Any, Type, TypeVar, Union

Expand All @@ -24,7 +24,7 @@ def configure_converter(converter: BaseConverter):
Configure the converter for use with the stdlib json module.

* bytes are serialized as base64 strings
* datetimes are serialized as ISO 8601
* datetimes, dates, times are serialized as ISO 8601
* counters are serialized as dicts
* sets are serialized as lists
"""
Expand All @@ -35,6 +35,12 @@ def configure_converter(converter: BaseConverter):
converter.register_unstructure_hook(datetime, lambda v: v.isoformat())
converter.register_structure_hook(datetime, lambda v, _: datetime.fromisoformat(v))

converter.register_unstructure_hook(date, lambda v: v.isoformat())
converter.register_structure_hook(date, lambda v, _: date.fromisoformat(v))

converter.register_unstructure_hook(time, lambda v: v.isoformat())
converter.register_structure_hook(time, lambda v, _: time.fromisoformat(v))


def make_converter(*args: Any, **kwargs: Any) -> JsonConverter:
kwargs["unstruct_collection_overrides"] = {
Expand Down
10 changes: 8 additions & 2 deletions src/cattrs/preconf/orjson.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Preconfigured converters for orjson."""
from base64 import b85decode, b85encode
from datetime import datetime
from datetime import datetime, date, time
from enum import Enum
from typing import Any, Type, TypeVar

Expand All @@ -26,7 +26,7 @@ def configure_converter(converter: BaseConverter):
Configure the converter for use with the orjson library.

* bytes are serialized as base85 strings
* datetimes are serialized as ISO 8601
* datetimes, dates, times are serialized as ISO 8601
* sets are serialized as lists
* string enum mapping keys have special handling
* mapping keys are coerced into strings when unstructuring
Expand All @@ -39,6 +39,12 @@ def configure_converter(converter: BaseConverter):
converter.register_unstructure_hook(datetime, lambda v: v.isoformat())
converter.register_structure_hook(datetime, lambda v, _: datetime.fromisoformat(v))

converter.register_unstructure_hook(date, lambda v: v.isoformat())
converter.register_structure_hook(date, lambda v, _: date.fromisoformat(v))

converter.register_unstructure_hook(time, lambda v: v.isoformat())
converter.register_structure_hook(time, lambda v, _: time.fromisoformat(v))

def gen_unstructure_mapping(cl: Any, unstructure_to=None):
key_handler = str
args = getattr(cl, "__args__", None)
Expand Down
10 changes: 8 additions & 2 deletions src/cattrs/preconf/ujson.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Preconfigured converters for ujson."""
from base64 import b85decode, b85encode
from datetime import datetime
from datetime import datetime, date, time
from typing import Any, AnyStr, Type, TypeVar

from ujson import dumps, loads
Expand All @@ -25,7 +25,7 @@ def configure_converter(converter: BaseConverter):
Configure the converter for use with the ujson library.

* bytes are serialized as base64 strings
* datetimes are serialized as ISO 8601
* datetimes, dates, times are serialized as ISO 8601
* sets are serialized as lists
"""
converter.register_unstructure_hook(
Expand All @@ -36,6 +36,12 @@ def configure_converter(converter: BaseConverter):
converter.register_unstructure_hook(datetime, lambda v: v.isoformat())
converter.register_structure_hook(datetime, lambda v, _: datetime.fromisoformat(v))

converter.register_unstructure_hook(date, lambda v: v.isoformat())
converter.register_structure_hook(date, lambda v, _: date.fromisoformat(v))

converter.register_unstructure_hook(time, lambda v: v.isoformat())
converter.register_structure_hook(time, lambda v, _: time.fromisoformat(v))


def make_converter(*args: Any, **kwargs: Any) -> UjsonConverter:
kwargs["unstruct_collection_overrides"] = {
Expand Down