Here is a Rust playground link with what I have so far.
I am working on a Rust project where the serialization/deserialization MUST be interoperable with a C# project. I have these two structs that are approximations of my C# classes to outline my case:
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MyStruct<'a> {
#[serde(flatten)]
#[serde(borrow)]
other_struct_map: HashMap<&'a str, MyOtherStruct<'a>>
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MyOtherStruct<'a> {
#[serde(flatten)]
#[serde(borrow)]
data: HashMap<&'a str, &'a str>
}
I have been using serde(flatten)
because it gets very close to what I want (and because HashMaps don't generically implement Deserialize/Serialize), but it is not correct. My Rust playground result takes these instances of my structs:
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
let other = MyOtherStruct { data: map };
let mut map2 = HashMap::new();
map2.insert("key1", other);
let myStruct = MyStruct { other_struct_map: map2 };
And serializes it this way:
{
"key1": {
"key2": "value2",
"key1": "value1"
}
}
But this is not how C# would want this. It would serialize with the field names kept, so like this:
{
"otherStructMap": {
"data": {
"key1": {
"key2": "value2",
"key1": "value1"
}
}
}
}
I tried to use serialize_with
and MapSerializer
to create a HashMap<&str, _>
approach and it compiles but it produces the exact same results as flatten: Rust Playground. I suspect this is close to a working solution if I knew how to make sure what I serialize_map is wrapped in another json node. I tried this because an important part of my solution is it must be generic for any HashMap<&str, _>
because I have multiple such HashMaps and it would be ideal if I had one function that works for them all.
Thanks a lot for the help