I have three structs A, B and C that are being fed to a method process_data()
as a list of JSON. All the three structs are serde serializable/deserializable.
They are defined below as follows:-
#[derive(Serialize, Deserialize)]
struct A {
pub a: u32,
}
#[derive(Serialize, Deserialize)]
struct B {
pub b: u32,
}
#[derive(Serialize, Deserialize)]
struct C {
pub c: u32,
}
The function signature looks like this
fn process_data(data: String) {}
data
can have any of these structs but its guaranteed that one of A, B or C will be there
data = "A{a: 1}"
or data = "[A{a:1}, B{b:1}, C{c:1}]"
or data = "[B{b:1}, A{a:1}, C{c:1}]"
I am looking for a way to process the variable data
through serde within process_data
, such that I can extract the structs from the data stream.
What I have tried so far. I tried defining a struct called Collect which holds all the structs like this:-
#[derive(Serialize, Deserialize)]
struct Collect {
pub a1: Option<A>
pub b1: Option<B>,
pub c1: Option<C>
}
and then process the data as follows:-
serde_json::from_str::<Collect>(data.as_str())
But the previous command throws an error. Also I am looking to preserve the order of the vector in which the data is coming
I am not sure if serde will work in this case.