-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support encoding UUIDs as bytes #493
Comments
There currently isn't a way to override how msgspec will encode/decode types that are natively supported. Most types support overriding how subclasses are encoded/decoded, but for ease of integration with A few questions:
In [8]: u # an example uuid
Out[8]: UUID('9b65a26c-d67e-445f-b29e-a3881734a701')
In [9]: u.hex.encode() # does it encode the bytes in their hex format?
Out[9]: b'9b65a26cd67e445fb29ea3881734a701'
In [10]: u.bytes # or does it encode the bytes directly?
Out[10]: b'\x9be\xa2l\xd6~D_\xb2\x9e\xa3\x88\x174\xa7\x01'
In [11]: u.bytes_le # or perhaps using the little endian representation?
Out[11]: b'l\xa2e\x9b~\xd6_D\xb2\x9e\xa3\x88\x174\xa7\x01' |
thanks for fast answer)
|
Ok, I think we can fix this by:
Given those changes the following would work: import msgspec
import uuid
enc = msgspec.msgpack.Encoder(uuid_format="bytes") # encode uuids as bytes
u = uuid.uuid4()
print(u)
#> f647ac40-3902-4cf8-b4f5-462a7e469fb0
msg = enc.encode(u)
print(msg)
#> b'\xc4\x10\xf6G\xac@9\x02L\xf8\xb4\xf5F*~F\x9f\xb0'
u2 = msgspec.msgpack.decode(msg, type=uuid.UUID) # we might require passing `strict=False` here to decode from bytes, I'm undecided
assert u == u2 Would that resolve your issue? |
yep, it looks like what we need. Awesome. EDITED: |
Description
Hello! Thanks for your hard work. We have a
go
package with broken msgpack library (it pack uuid in bytes eg).We want to move on your library, but as I see all uuid subtypes are packed as string. /~https://github.com/jcrist/msgspec/blob/main/msgspec/_core.c#L11748
Are there any chances that something will keep options open?
The text was updated successfully, but these errors were encountered: