yukinarit / pyserde

Yet another serialization library on top of dataclasses, inspired by serde-rs.
https://yukinarit.github.io/pyserde/guide/en
MIT License
729 stars 40 forks source link

Provide a way to disable `datetime` being converted to `str` #483

Closed uyha closed 8 months ago

uyha commented 8 months ago

Currently, https://github.com/yukinarit/pyserde/blob/35901de31a8b135c777710d5388c8f5a0f11dc66/serde/se.py#L383-L384 converts datetime to an ISO string unconditionally. This makes the msgpack serializer unable to convert datetime to its extension type in the spec. It would be nice to have a flag to disable this convertion and let the serializer handle it.

yukinarit commented 8 months ago

Hi @uyha Can you please try reuse_instances=True?

This is how objects are treated within generated code.

uyha commented 8 months ago

I changed https://github.com/yukinarit/pyserde/blob/a517254b18b2927348b94f59de095ce873abe101/serde/msgpack.py#L71 to

 kwargs: Any = {"c": cls, "reuse_instances": True, "convert_sets": True} 

with the following snippet, it still converts the datetime object to a str

from serde.msgpack import to_msgpack

from datetime import datetime

from msgpack import packb

time = datetime.fromisoformat("2020-01-01 00:00:00+00")
print(to_msgpack(time, datetime=True))
print(packb(time, datetime=True))

which outputs

b'\xb92020-01-01T00:00:00+00:00'
b'\xd6\xff^\x0b\xe1\x00'
yukinarit commented 8 months ago

Ah,

  1. reuse_instances are not available in to_msgpack
  2. reuse_instances works only for dataclass field
    
    @dataclass
    class Foo:
    v: datetime

f = Foo(datetime.fromisoformat("2020-01-01 00:00:00+00")) print(to_dict(f, reuse_instances=True)) print(to_dict(f, reuse_instances=False))


it prints

{'v': datetime.datetime(2020, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)} {'v': '2020-01-01T00:00:00+00:00'}

yukinarit commented 8 months ago

ok, I found a way to fix. let me post it tomorrow

yukinarit commented 8 months ago

@uyha v0.14.2 was published to PyPI. Please take a look if it resolves your issue.

uyha commented 8 months ago

yup, works