I'm trying to replace an existing serializer with protobuf for C# by Marc Gravell. My code is extensive and my goal is to be able to do the switch with minimal changes.
I came across an issue which I believe I understand why it happens but require help with overcoming - particularly a solution that would require the least changes in my already existing code and classes. My code is complex so I created the following short sample to demonstrate the issue:
using System;
using System.Collections.Generic;
using System.IO;
using ProtoBuf;
namespace ConsoleApplication1
{
class program_issue
{
[ProtoContract]
public class Father
{
public Father()
{
sonny = new Son();
}
[ProtoMember(101)]
public string Name;
[ProtoMember(102)]
public Son sonny;
}
[ProtoContract]
public class Son
{
public Son()
{
Dict.Add(10, "ten");
}
[ProtoMember(103)]
public Dictionary<int, string> Dict = new Dictionary<int, string>();
}
static void Main(string[] args)
{
Father f1 = new Father();
f1.Name = "Hello";
byte[] bts = PBSerializer.Serialize(typeof(Father), f1);
Father f2;
PBSerializer.Deserialize(bts, out f2);
}
public static class PBSerializer
{
public static byte[] Serialize(Type objType, object obj)
{
MemoryStream stream = new MemoryStream();
ProtoBuf.Serializer.Serialize(stream, obj);
string s = Convert.ToBase64String(stream.ToArray());
byte[] bytes = stream.ToArray();
return bytes;
}
public static void Deserialize(byte[] data, out Father obj)
{
using (MemoryStream stream = new MemoryStream(data))
{
obj = ProtoBuf.Serializer.Deserialize<Father>(stream);
}
}
}
}
}
In short, when the parent object is created, it creates a son object which inits a dictionary with some values. I assume that when protobuf tries to rebuild the object when deserializing it uses the same constructor (thus also initiating the dictionary with values) and then tries to push the same values again as part of the deserializing -> error.
How can I overcome it with minimal changes to my code?
Kind regards, Yossi.