I have a struct
:
struct MyStruct {
key_outer: String,
key_inner: String,
data: String
}
and an instance of it:
let my_struct = MyStruct {
key_outer: "key_outer".into(),
key_inner: "key_inner".into(),
data: "my-data".into(),
}
How can I implement the trait Serialize
for it, so the output of the instance:
serde_json::to_string(&my_struct).unwrap()
///expected output
{"key_outer": {"key_inner": "my-data"}}
I tried this, it doesn't work:
impl Serialize for MyStruct
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut inner2 = serializer.serialize_map(Some(1))?;
inner2.serialize_entry(&self.key_inner, &self.data)?;
inner2.end();
let mut inner1 = serializer.serialize_map(Some(1))?;
inner1.serialize_entry(&self.key_outer, &inner2)?;
inner1.end();
}
}