-
Notifications
You must be signed in to change notification settings - Fork 144
Can't serialize/deserialize uuid properly #311
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
Comments
I have the same problem. |
I also have the same issue with the struct |
Hi @nanoqsh and @dizda, thanks for reporting this issue. I had a response drafted last week but forgot to actually post it here, sorry for the delay! There indeed are some issues with using The current situationThe problems stem from the fact that the Due to what was potentially an oversight (see #297), the raw serializer (used in Since we do own the So in summary: a
Next stepsThe documentation is currently limited and incorrect as you point out. We should update it to include some of the info in the comment here as well update the incorrect "serializes as a string" comment. Unfortunately, directly serializing We can also introduce integration with #[serde_as]
#[derive(Debug, Serialize, Deserialize)]
struct MyData {
#[serde_as(as = "Option<bson::Binary>")
date: Option<uuid::Uuid>
} Lastly, we can introduce some mechanism that makes the Thanks again for reporting this issue. Be sure to follow the RUST tickets linked above to track progress towards improving this situation. In the meantime, you'll need to implement custom (de)serialization logic to ensure everything is consistent. Sorry for any inconvenience this may cause, and we hope to have some improvements out soon! |
We just released version |
@patrickfreed It would be better to include #[serde_as]
#[derive(Debug, Serialize, Deserialize)]
struct MyData {
#[serde(default)]
#[serde_as(as = "Option<bson::Uuid>")
date: Option<uuid::Uuid>
} |
I want to store my objects with
uuid
identifier. One of my types looks like this:I use
State
trait to express two states:New
andSaved
. It allows me to conveniently create an object ofAccount<New>
type withoutuuid
anywhere in the application, then id will be an unit type. So I can't directly useuuid_as_binary
attribute, this throws a build error. But that wouldn't be a problem if I could serialize an uuid to string. The doc says it serialize as string by default:But when I save any object to db, then fetch all (for debug) I see it stores as binary format (with
BinarySubtype::Generic
):Ok, I can use binary as well, but then I can't
find
an object by _id because when I try to do something like:The
filter
document serializes as:(with
BinarySubtype::Uuid
)How can these different serializations be made to work together? Of course I can use
uuid_as_binary
attribute, but that will break my application's flexibility and invariant checking. Maybe there is some more suitable solution?In addition, it seems to me that various serialization behavior is unintuitive and more like an implementation flaw. It would be more convenient if uuid was just (de)serialized as string by default.
I also think it would be great to implement this with type wrappers. For example,
Uuid
will be (de)serialized as a string, andBinary(Uuid)
as binary data. Then it would be more convenient to use them in generic code.Project's Cargo.toml dependencies
The text was updated successfully, but these errors were encountered: