asmmo
June 27, 2021, 11:34am
1
I have a single level json
where the values are of i32
s, f32
s, and String
s.
How to deserialize that json
to HashMap<String, String>
?
{
"key1": "value1",
"key2": 0,
"key3": 6.7
}
How to deserialize it? Is there a way in serde
that can help here?
let json: HashMap<String, String>= serde_json::from_str(r#"{"key1": "value1", "key2": 0, "key3": 6.7}"#).unwrap();
Maybe you should define an enum to represent your case, and impl Deserialize for it
asmmo
June 27, 2021, 11:40am
3
I know this. I want a work around
@asmmo It would be helpful if you could specify how the strings in Rust should look like after deserialization. For example, what do you want as a result when deserializing "key4": 0.000
. Here is a solution which produces:
[src/main.rs:15] map = {
"key1": "\"value1\"",
"key2": "0",
"key3": "6.7",
"key4": "0.000",
}
Code on the Playground
kornel
June 27, 2021, 1:00pm
5
You can easily deserialize to HashMap<String, serde_json::Value>
.
If you want to convert to string on the fly, then you need the deserialize_with
annotation and write your own stringifying deserializer.
system
Closed
September 25, 2021, 1:01pm
6
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.