-
-
Notifications
You must be signed in to change notification settings - Fork 792
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
Easy(er) way to deserialize a HashMap<&str, serde_json::Value> #1739
Comments
The use serde::de::value::MapDeserializer;
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
#[derive(Deserialize, Debug)]
struct MyStruct {
a: usize,
b: usize,
}
fn main() {
let content = "{\"a\":1}";
let mut data: HashMap<&str, Value> = serde_json::from_str(content).unwrap();
data.insert("b", Value::from(2));
let v = MyStruct::deserialize(MapDeserializer::new(data.into_iter())).unwrap();
println!("{:#?}", v);
} |
Super nice @dtolnay! Exactly the kind of solution I was hoping for 👍 Thanks! |
Hmm... I actually have the same issue/question for doing this the other way around. So serialize a struct into a Additionally, if I do take the above two-step approach, I cannot use a If doing this more elegant (in one go) isn;t possible, that it would be great to know how I can keep using Thanks! |
Hmm...Maybe a better solution for my problem/challenge is something along the lines of #1470 Asked an addition question there about that approach so if that works out this one can be closed... |
Guess I'll close this issue as I don't expect any updates anymore... |
@dtolnay I can't seem to get your example to work with serde_yaml (but it works fine with json). Am I doing something wrong, or is this just a feature that is not yet implemented?
|
@norbertkeri serde_yaml does not implement this but I filed dtolnay/serde-yaml#169 to follow up. |
Thanks! |
I know there is a
serde_json::Map
which hasString
keys, but I would really like to use something like this:This works, but it would be nice if there is a way to do this without the additional
to_value
. Is that possible? Thanks!The text was updated successfully, but these errors were encountered: