-
Notifications
You must be signed in to change notification settings - Fork 135
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
Deserialize ObjectId from hex string? #418
Comments
Hi! Thanks for raising this - there's no particular reason that's lacking, we should definitely add it :) I've filed https://jira.mongodb.org/browse/RUST-1681 to track this. We'd also be happy to accept a PR if you'd like to implement it! |
I took the liberty to give it a try in #421. |
Sorry that I didn't notice this earlier - as @patrickfreed quite correctly points out, the implementation of |
Yes, so I realized this after opening the issue. The problem I was having wasn't actually related to this, I was just thinking that maybe doing this would fix it. I wanted to have the object ids represented with the ObjectId type in my code, and as a bson object id in mongodb, but as a string for the json serialization. The problem was that if I changed the serializer, it would serialize it as a string when interacting with mongodb as well, so it wouldn't store it as an ObjectId there either. I thought maybe having this feature would solve that, but now I don't think it would, because the issue is that it is serializing it the same for json and bson, when I don't want it to do that. What I ended up doing is creating a new struct for just my json api that serializes to a hex string and keeping my main struct for interacting with mongodb the same. If there's a way to do this with a single struct by differentiating between serializing for json vs bson, I'd love to know about it. But, I think this issue can probably be closed. |
Having distinct structs is probably the best way to go there, thanks for clarifying! |
I'm using mongodb for the backend api of a project I'm working on, and I want the api to return the hex string in the json, so I'm using
serialize_with = "bson::serde_helpers::serialize_object_id_as_hex_string"
. But this causes errors when I try to deserialize from the json response, which has the id as the raw hex string, because that's how it was serialized.The serde_helpers module has three functions for objectid's: serialize_object_id_as_hex_string, deserialize_hex_string_from_object_id, and serialize_hex_string_as_object_id.
Is there a reason that there is no fourth, something like deserialize_object_id_from_hex_string?
Thanks!
The text was updated successfully, but these errors were encountered: