Hello. I have the following structs:
struct Player {
name: String,
}
struct Agent {
name: String,
}
#[derive(Serialize)]
struct Match {
agents: HashMap<Player, Agent>,
}
How can I make serde
to flatten the keys and values when serializing/deserializing the Match
? For example
Match {
agents: HashMap::from([(Player { name: "Rb"}, Agent { name: "Killjoy"})])
}
should get serialized into
{ agents: {"Rb": "Killjoy" }}
and same with deserialization.
I have actually achieved the serializing part with implementing my own serializer, but deser seems more complicated, so I wanted to know if there's any easier way to do both.
(FYI: the serializer part is pretty easy:
impl Serialize for Player {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer {
serializer.serialize_str(&self.name)
}
}
impl Serialize for Agent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer {
serializer.serialize_str(&self.name)
}
}
)