I have some JSON of this format:
[ 123, {“some struct”: “is always here”}, {“optional struct”: ”is not always here”}, “String1”, “String2” ] Note that the second struct is sometimes not present - the array will sometimes contain 4 elements, other times 5. This means that seq[1] is always the struct, but seq[2] may either be the struct or String1.
I’ve written a custom Deserialize using the SeqAccess visitor, looking something like this:
let number: usize = v.next_element()?; let struct: SomeStruct = v.next_element()?; let maybe_struct: OtherStruct = v.next_element()?; // … The problem is that calling v.next_element() where the struct is NOT present, it wants to deserialise immediately into OtherStruct, which fails because the next element is String1. If I call v.next_element() again, the visitor has already moved onto the next element, and I’ll get String2.
I think I’m looking at this wrong and probably need to write a visitor which receives a method call for each element in the sequence, I’ve not been able to locate any examples or documentation that explains how to do this.
next_elementinto some sort of intermediate representation - e.g. aStructOrStringenum. Then you could match on that to determine what to expect for the remaining elements.