Is it possible to (de)serialize a list of heterogeneous objects?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A
List<object>poses a number of challenges for a serialization format that does not include type metadata… but as always, there are tricks, traps, and workarounds.If you mean at the outermost level, there are a few options here that revolve around using a different tag(/field-number) to indicate the message type. This is useful on an API (for example, a socket conversation) where different messages might be expected at any time. As example would be: How can I send multiple types of objects across Protobuf?
Inside a message is somewhat trickier; if the list of candidate types is small, then a reasonable workaround is something like presented here: Protobuf attributes with a hierarchy of generic classes, i.e. treating each anticipated type as a specialization.
If the type can’t be predicted, then v2 includes another workaround, but including more metadata than normal; see http://marcgravell.blogspot.com/2011/03/objects-graphs-and-all-that-jazz.html