From dd5b4c4c2f3a03216ff7c4918960be71eaae3272 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tin=20Tvrtkovi=C4=87?= Date: Sun, 26 Nov 2023 00:18:44 +0100 Subject: [PATCH] orjson passthrough (#463) --- HISTORY.md | 1 + src/cattrs/preconf/orjson.py | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8a269568..81320cd5 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,6 +4,7 @@ - Add support for [PEP 695](https://peps.python.org/pep-0695/) type aliases. ([#452](https://github.com/python-attrs/cattrs/pull/452)) +- The {class}`orjson preconf converter ` now passes through dates and datetimes to orjson while unstructuring, greatly improving speed. - More robust support for `Annotated` and `NotRequired` in TypedDicts. ([#450](https://github.com/python-attrs/cattrs/pull/450)) - [PEP 695](https://peps.python.org/pep-0695/) generics are now tested. diff --git a/src/cattrs/preconf/orjson.py b/src/cattrs/preconf/orjson.py index 664f92b4..fcd380b9 100644 --- a/src/cattrs/preconf/orjson.py +++ b/src/cattrs/preconf/orjson.py @@ -28,7 +28,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 and dates are passed through to be serialized as RFC 3339 by orjson * sets are serialized as lists * string enum mapping keys have special handling * mapping keys are coerced into strings when unstructuring @@ -38,9 +38,7 @@ def configure_converter(converter: BaseConverter): ) converter.register_structure_hook(bytes, lambda v, _: b85decode(v)) - 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)) def gen_unstructure_mapping(cl: Any, unstructure_to=None):