Note that there are some explanatory texts on larger screens.

plurals
  1. POProtobuf-net .proto file generation for inheritance
    primarykey
    data
    text
    <p>I am prototyping Protobuf-net to replace some of our existing C# code which is currently using [Datacontract] to Serialize objects to Xml.</p> <p>Using protobuffer we can easily share data with Java. I am thus very interested in the .proto file generation of Protobuf-net. This worked well for almost all the use cases I tested so far.</p> <p>But now with inheritance it's a different ball game. The .proto file that's been generated for the Inherited classes is very simple - not including any of the fields for the Base Class.</p> <p>The inheritance itself is working fine in C# - I can read the generated byte stream (see my test below). So the internal binary stream contains all the fields of the base class</p> <p>The generated .proto:</p> <pre><code>message ProtoScholar { optional string Subject = 1; } </code></pre> <p>If I can understand how the byte stream is written out I can create the corresponding .proto file by hand. </p> <p>Anyone got any experience in creating a .proto file for inheritance using protobuf-net? </p> <p>Any info on how the data stream is being created for inheritance will be great.</p> <p>My DataModel is as follows:</p> <pre><code>[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; } } </code></pre> <p>My NUnit test looks like this:</p> <pre><code>[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&lt;ProtoScholar&gt;(); Console.WriteLine(proto); //Serialize to a Memory Stream Serializer.Serialize(ms, protoBuffer); Console.WriteLine(ms.Length); ms.Position = 0; var deserializedProtoBuffer = Serializer.Deserialize&lt;ProtoScholar&gt;(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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload