I have a use case that requires deserializing JSON into a map of "remote" (defined in another crate) structs. I've had a laughably difficult time with this, so I must be missing something obvious.
The following is essentially the desired end state:
use hyper::Uri;
use serde_json;
use std::collections::HashMap;
fn main() {
let data = r#"
{
"/a": "http://example.com/86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
"/b": "http://example.com/e9d71f5ee7c92d6dc9e92ffdad17b8bd49418f98",
"/c": "http://example.com/84a516841ba77a5b4648de2cd0dfcb30ea46dbb4"
}"#;
let map: HashMap<String, Uri> = serde_json::from_str(data).unwrap();
println!("{:?}", map);
}
which fails because:
the trait bound `Uri: serde::de::Deserialize<'_>` is not satisfied required because of the requirements
on the impl of `serde::de::Deserialize<'_>` for `HashMap<std::string::String, Uri>`
While the serde docs describe a pretty nasty but potentially viable workaround for deriving Deserialize on remote structs, it requires the use of #[serde(with = "LocalStructRedefinition")] on any referencing container type, which does not appear possible when creating a HashMap.
Intuitively this must be a common use case... is there a way to solve this that doesn't involve:
- deserializing the data into a
HashMap<String, String> - iterate through the map, parsing the values into a new
HashMap<String, Uri>