hi 👋
sorry i ask too much🙏
so the issue is with this code:
import uuid
from bson.binary import Binary
from cattrs.preconf.bson import make_converter
converter = make_converter()
converter.register_unstructure_hook(uuid.UUID, lambda x: Binary.from_uuid(x))
converter.register_unstructure_hook(
Union[uuid.UUID, None], lambda x: Binary.from_uuid(x) if x else None
)
bson doesn't serialize UUIDs
but they have Binary.from_uuid(x) so we can achive it like this
and it works
s = bson_converter.structure({"uu": uuid.uuid4()}, Something)
dumps = bson_converter.dumps(s)
but when doing bson_converter.unstructure(s), i would expect to see a UUID object
but since dumps and unstructure use the same hooks, i see this instead
'uu': Binary(b'\xd5\xd2\x0e\x15TWJ\xf0\xbae\xe6\xed\x8a\xc6\xa9\xb3', 4)
which is not a big issue for me, but it got me thinking if there is a workaround
hi 👋
sorry i ask too much🙏
so the issue is with this code:
bson doesn't serialize UUIDs
but they have
Binary.from_uuid(x)so we can achive it like thisand it works
but when doing
bson_converter.unstructure(s), i would expect to see a UUID objectbut since dumps and unstructure use the same hooks, i see this instead
'uu': Binary(b'\xd5\xd2\x0e\x15TWJ\xf0\xbae\xe6\xed\x8a\xc6\xa9\xb3', 4)which is not a big issue for me, but it got me thinking if there is a workaround