Deserialize a json array that might have two types inside

Hi,

I have this API endpoint which might return Vec<T> but also Vec<T|U> at certain indexes:

struct A;
struct B;
Response = [A,A,A,A,B,A,A];

Does Serde have features to handle this?

You should use an enum for this like Either, so take Vec<Either<A, B>> as the result from deserialising the vector. Depending on how your data actually looks like, you might need to roll out your own enum with a different representation, usually #[serde(untagged)].

3 Likes