Open kitschen opened 4 years ago
You can override the deserializer for your TimeRange
object.
from dataclasses_serialization.serializer_base import dict_to_dataclass
@JSONSerializer.register_deserializer(TimeRange)
def deserialize_time_range(cls, dct):
dct = {
key + "_dt": value
for key, value in dct.items()
}
return dict_to_dataclass(cls, dct, JSONSerializer.deserialize)
You can also override the serializer in a similar way.
If you have many such classes, you can override the serializer/deserializer for dataclass
, with whatever your renaming logic is.
I receive JSON with a datetime field called
from
. Asfrom
is a keyword in Python, I cannot create a dataclass with afrom
field. So deserialization is not possible.Is there any way of mapping the JSON's
from
to a different Python field name?Example test case (which currently fails):