I've got several enum variants that are represented as internally tagged JSON. I've also got a legacy variant that doesn't contain a tag field, but would like to be able to parse that into a variant too.
This of course panics with "missing field `type`".
So I'm basically looking for a way to deserialize to Thing::Legacy when the type field is missing (or somehow specify a default value of "legacy" for that field).
Any suggestions on how to achieve this using Serde?
The derives shipped with serde do not allow you to mix different tag representations for the same enum. One option is to split it into two enums. The first enum contains all tagged variants, the other the rest.
serde also provides you the option to keep using your Thing struct and translate it from a better deserializable form (see above) when needed. Namely the from and into attributes of serde are relevant here.
This solution is great into to perform tagging ourselves but when I tried your code, I get this element added to the resulting vector. How do I get rid of type_: MustBe!("a") and instead just have type: "a" or not have that field all together?