Protobuf-net to serialize plot model

Mar 17, 2014 at 3:39 PM
Edited Mar 17, 2014 at 4:19 PM
I am currently exploring how to use protobuf-net to serialize oxyplot.
It should be a nice tool that help on saving and reading plot
But here I got a "Common Language Runtime detected an invalid program." on line "Serializer.Serialize"
Anyone know the issue?
            var PolygonAnnotationType = RuntimeTypeModel.Default.Add(typeof(PolygonAnnotation), false).Add("Points");
            var IDataPointType = RuntimeTypeModel.Default.Add(typeof(IDataPoint), false).Add("X", "Y");
            IDataPointType.AddSubType(100, typeof(DataPoint));

            var poly = new PolygonAnnotation();
            IList<IDataPoint> ps = new List<IDataPoint>() { new DataPoint(0, 0), new DataPoint(1, 0) };
            poly.Points = ps;
            MemoryStream ms = new MemoryStream();
            Serializer.Serialize<PolygonAnnotation>(ms, poly);
            string stringBase64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
            MemoryStream afterStream = new MemoryStream(byteAfter64);
            var newPlot = Serializer.Deserialize<PolygonAnnotation>(afterStream);
            Console.Write(stringBase64);
The following code works. difference is NewDataPoint is a class, instead of structure.
private static void Test1() {

            var poly = new NewPolygon() {
                Points = new List<IDataPoint>(){
                    new NewDataPoint(0,0),new NewDataPoint(1,1)
                }
            };
            RuntimeTypeModel.Default.Add(typeof(NewPolygon), false).Add("Points");
            RuntimeTypeModel.Default.Add(typeof(IDataPoint), false).Add("X","Y").AddSubType(100,typeof(NewDataPoint));

            MemoryStream ms = new MemoryStream();
            Serializer.Serialize<NewPolygon>(ms, poly);
            string stringBase64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            byte[] byteAfter64 = Convert.FromBase64String(stringBase64);
            MemoryStream afterStream = new MemoryStream(byteAfter64);
            var newpoly = Serializer.Deserialize<NewPolygon>(afterStream);
            Console.Write(stringBase64);
        }

class NewDataPoint : IDataPoint {
        public double X { get; set; }
        public double Y { get; set; }
        public NewDataPoint() { }
        public NewDataPoint(double x, double y) {
            this.X = x;
            this.Y = y;
        }
    }
    class NewPolygon : TextualAnnotation {
        public IList<IDataPoint> Points { get; set; }
    }

Coordinator
Mar 19, 2014 at 8:31 AM
This is interesting. How is protobuf handling versioning? The PlotModel and related classes will continue to evolve...
Mar 19, 2014 at 2:28 PM
Edited Mar 19, 2014 at 2:29 PM
Found the link about versioning. Haven't test yet. Actually I am also have no ideas about this.

Protobuf-net: the unofficial manual