-
Notifications
You must be signed in to change notification settings - Fork 144
Bson to Document #24
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
Hmm, maybe we could define a trait, which could be used to convert a type to BSON serializable type? |
👍 ditto, was curious about this. It seems you can serialize to BSON with serde, but I can't figure out how to serialize a rust object into a document that can actually be inserted into the database. |
It is not to hard to do it now @lord. I had to create a small wrapper to make it easy to do so. |
@dariusc93 mind sharing? I'm new to Rust, and can't quite seem to figure it out. |
@lord here is what I use with rustc_serialize (you could change it so it would work with Serde). Note: The variable
Here is an example of how it works
While it isnt the very best, it works. I created this small wrapper to make it easy so i am not repeating the same code over. |
@dariusc93 this is helpful, thanks a bunch! Really appreciate it. |
Ah, looks great. |
Probably not very good code, but this ended up being my serialization/deserialization code with Serde, if anybody else in the future has the same problem I did:
The other things to watch out for is that Serde is going to want to match everything, so you need to make sure to have the Mongo ID structured correctly: (assuming you're storing a document with one string field 'meow')
Again, sorry if off-topic for this issue, but just thought I'd post in case somebody else could benefit from it. |
When I get time, I may take another look at serde (I do plan on switching in the future, only using rustc_serialize because of the libraries I use support it over serde), but good catch if serde is that strict. Ill give this a try sometime tonight and keep you up to date. |
There's a pair of helper methods that will reduce the boilerplate slightly. use bson::{from_bson, to_bson};
fn decode<T: Deserialize>(doc: &Document) -> Option<T> {
from_bson(Bson::Document(doc.clone())).ok()
}
fn encode<T: Serialize>(doc: &T) -> Option<Document> {
match to_bson(doc) {
Ok(Bson::Document(d)) => Some(d),
_ => None,
}
} The decode method could possibly be shortened to In general Serde is pretty strict, but I've tried to maintain the mapping from structs to Extended JSON as closely as possible when using it. In the meow example, the following will also work, provided you represent the OID as an ObjectId instead of a String: use bson::oid::ObjectId;
#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
pub struct Test {
#[serde(rename="_id")]
pub id: Option<ObjectId>,
pub meow: Option<String>,
} We should try to get a better README up and host the generated documentation in the near future to make it easier to use the crate and see what's available. |
Thanks @kyeah, that example was really helpful, couldn't find those helper methods anywhere. README+hosted docs would be fantastic! |
@kyeah I tried using |
@freiguy1 small typo -- the encoder and decoder modules are private, but the methods are publically exported in the main library, so Just fixed this in my previous example. Thanks for pointing it out! |
@kyeah when using those helpers I normally get "the trait |
I just ran the trait but it is: #[derive(Serialize, Deserialize)]¬
pub struct LeagueDao {¬
#[serde(rename="_id")]¬
pub id: u32,¬
pub name: String,¬
pub contact_email: String,¬
pub contact_phone: String,¬
pub sport: String,¬
pub teams: Vec<TeamDao>¬
}¬ |
@freiguy1 do you know what version of serde you were on before the update? I normally keep record of the version but since i never previously had it in my project it could be a bug with serde itself. Not just structs but any functions that tries to use those traits seem to have the problem |
Just noticed serde updated to 0.7 and bson.rs uses 0.6 |
Ahh that might be an issue. @dariusc93: here's my cargo.lock that is successfully building: https://gitlab.com/freiguy1/league-api/blob/master/Cargo.lock I think the data is in there. |
@dariusc93 What are you doing to get past this issue? Hardcoding the serde version in your cargo.toml to be something other than the newest? |
@freiguy1 the issue was on 0.7 so i downgraded to 0.6 but to be safe I did hardcode the versions instead of doing "0.6" to the one in your Cargo.lock. I also noticed you use "*" in your Cargo.toml. I would highly suggest not doing that because you never know what changes to come in the latest that may break your code. As a side note, serde 0.7 is not as strict as 0.6. might be worth taking a look at when things are fixed. |
Could this get pushed to crates.io? |
Will do it after #36 is merged. |
Already published. Cheers. |
for future reference, |
What would be the best way to convert a bson to a document that is suitable for inserting into the database?
The text was updated successfully, but these errors were encountered: