I am prototyping Protobuf-net to replace part of our existing C # code, which currently uses [Datacontract] to serialize objects for Xml.
Using protobuffer, we can easily exchange data with Java. Therefore, I am very interested in generating .proto Protobuf-net files. This worked well for almost all of the use cases I have tested so far.
But now with inheritance, this is a different ball game. The .proto file created for Inherited classes is very simple - not including any of the fields in the base class.
Inheritance itself works fine in C # - I can read the generated byte stream (see my test below). Thus, the internal binary stream contains all the fields of the base class
Generated .proto:
message ProtoScholar {
optional string Subject = 1;
}
If I can understand how a stream of bytes is written out, I can create the corresponding .proto file manually.
Has anyone gotten the experience of creating a .proto file for inheritance using protobuf-net?
Any information on how the data stream for inheritance is created will be great.
My DataModel is as follows:
[DataContract]
[ProtoInclude(7, typeof(ProtoScholar))]
public class ProtoAlien
{
[DataMember(Order = 1)]
public string Name
{
get;
set;
}
[DataMember(Order = 2)]
public double? Average
{
get;
set;
}
[DataMember(Order = 3)]
public int? HowMuch
{
get;
set;
}
[DataMember(Order = 4, IsRequired = true)]
public Height Length
{
get; set;
}
[DataMember(Order = 5, IsRequired = true)]
public Character Personality
{
get;
set;
}
[DataMember(Order = 6, IsRequired = true)]
public DateTime When
{
get; set;
}
public enum Height
{
Short = 1,
Medium,
Tall
}
public enum Character : long
{
Wasp = 1717986918,
BumbleBee,
WorkerBee,
Hornet,
Queen
}
}
[DataContract()]
public class ProtoScholar : ProtoAlien
{
[DataMember(Order=1)]
public string Subject
{
get; set;
}
}
My NUnit test looks like this:
[Test]
public void TestInheritanceSupport()
{
var protoBuffer = new ProtoScholar
{
Name = "Ben11",
HowMuch = null,
Length = ProtoAlien.Height.Tall,
Personality = ProtoAlien.Character.WorkerBee,
When = new DateTime(2010, 4, 1, 2, 33, 56, 392),
Subject = "Alien Economics"
};
using (var ms = new MemoryStream())
{
var proto = Serializer.GetProto<ProtoScholar>();
Console.WriteLine(proto);
Serializer.Serialize(ms, protoBuffer);
Console.WriteLine(ms.Length);
ms.Position = 0;
var deserializedProtoBuffer = Serializer.Deserialize<ProtoScholar>(ms);
Assert.AreEqual("Ben11", deserializedProtoBuffer.Name);
Assert.Null(deserializedProtoBuffer.HowMuch);
Assert.AreEqual(ProtoAlien.Height.Tall, deserializedProtoBuffer.Length);
Assert.AreEqual(ProtoAlien.Character.WorkerBee, deserializedProtoBuffer.Personality);
Assert.AreEqual(new DateTime(2010, 4, 1, 2, 33, 56, 392), deserializedProtoBuffer.When);
Assert.AreEqual("Alien Economics", deserializedProtoBuffer.Subject);
}
}