Trying to serialize two different Vec
fields into a single array in the JSON output. I can't figure out how to implement the serialize()
method:
struct Base<'a> {
workspace: Vec<Workspace<'a>>,
methods: Vec<Request<'a>>,
// other fields ...
}
impl Serialize for Base<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("Base", 5)?;
state.serialize_field("resources", &self.methods)?;
state.serialize_field("resources", &self.workspace)?;
// other fields ...
state.end()
}
}
I would like to serialize both workspace
and methods
fields together, however the last "resources"
field overwrites the first one. I tried to workaround it using something like this, but it yields an error because serializer
moves:
let mut resources = serializer.serialize_seq(Some(self.workspace.len() + self.methods.len()))?;
self.workspace.iter().for_each(|f| { resources.serialize_element(f); });
self.methods.iter().for_each(|f| { resources.serialize_element(f); });
resources.end();
So how would I tie these two together?