Serde: Deserialize from a HashMap<string, string>

I would like to deserialize directly from a HashMap<string, string> (provided by an external library that I have no control over).

Having read the docs of Serde, I believe I would need to either:

a) write my own deserializer to do this

or

b) transcode through another medium.

Unless I have misunderstood something, a) seems to lead to a lot of boilerplate code writing internal Serde implementations, while b) just seems wasteful.

I feel like the Serde ecosystem is so fully-featured that I may just be missing a really simple way to do this.

An example of my expectation:

struct MyStruct {
  name: String,
  #[serde(deserialize_with = "deserialize_idea")]
  idea: String,
}

fn deser(input: HashMap<String, String>): Result<Serde::Deserialize::Error> {
  input.deserialize()
  // Or I suppose we would need to have something like, aping serde_json::from_str
  serde_stringy_hashmap::from_hashmap(input)
}
1 Like

A HashMap is already a "deserialized" type (it's not a "string", series of char, or a series of bytes).

Do you want to transform a HashMap<String, String> into a Vec<MyStruct> ?

3 Likes

And what are you deserializing into? Doesn't IntoDeserializer does exactly this?

4 Likes

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.