How to I serialize an object like this with protobuf-net:
public class MyObject{
public string Key {get; set;}
public List<Object> Values {get; set;}
}
When I try to serialize this with a TypeModel protobuf-net throws an error stating that it doesn't know how to serialize System.Object. Now I know that Values will only ever contain primitives (int, string, float, DateTime, etc.). So how do I let protobuf-net know about this?
This isn't really doable in pure ProtoBuf, in any sense. ProtoBuf is strongly typed, yet does not contain type information in the message; type information is always specified externally. Thus there are two "good" solutions; Ie, solutions which would be easy to interpret by a protobuf implementation other than Protobuf-net (Which you may or may not care about, but marc sure seems to).
1: Replace List<object>
with List<PrimitiveType>
where PrimitiveType
contains optional fields corresponding to all the 12-or-so primitive types (Depending on your definition of "Primitive Type".), and you ensure only one of those is filled in on each instance.
2: Replace List<object>
with a combination of List<int>
, List<double>
, List<string>
etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With