Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize List<object> (where the objects are supported primitives) in Protobuf.NET?

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?

like image 838
Timothy Baldridge Avatar asked Sep 21 '11 22:09

Timothy Baldridge


1 Answers

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.

like image 134
DanielOfTaebl Avatar answered Sep 23 '22 04:09

DanielOfTaebl