About implement VARIANT in Protobuf-net

Viewed 155

I have some protobuf decoder string that I decoeder from https://protogen.marcgravell.com/decode like this:

Field #1: 08 Varint Value = 0, Hex = 00
Field #2: 12 String Length = 0, Hex = 00, UTF8 = ""
Field #3: 1A String Length = 51, Hex = 33, UTF8 = "..................." (total 46 chars)
    As sub-object :
    Field #1: 08 Varint Value = 1610099957
Field #4: 20 Varint Value = 2, Hex = 02
Field #5: 2A String Length = 166, Hex = A6-01, UTF8 = "..........................." (total 156 chars)
    As sub-object :
    Field #1: 08 Varint Value = 1610070445
    Field #2: F5 String Length = 148, Hex = D9-E0, UTF8 = "........................." (total 138 chars)
        As sub-object :
        Field #1: 08 String Length = 39, Hex = 00, UTF8 = "......" (total 37 chars)
            As sub-object :
            Field #1: 08 Varint Value = 12345678
            Field #2: 33 Varint Value = 3333333333, Hex = 08-F5-D9-E0-FF
Field #6: F5 Varint Value = 1, Hex = D9
Field #7: 42 String Length = 0, Hex = 00, UTF8 = ""

how can I define the proto class and the varint size in c# like below:

[ProtoContract]
public class AllFields
{
    [ProtoMember(1)]
    public int Field1 { get; set; }
    [ProtoMember(2)]
    public string Field2 { get; set; }  
    [ProtoMember(3)]
    public string Field3 { get; set; }   
    public abstract class subField3
    {
        [ProtoMember(1)]
        public long subField3_1 { get; set; }
    }   
    [ProtoMember(4)]
    public int Field4 { get; set; } 
    [ProtoMember(5)]
    public string Field5 { get; set; }  
    public abstract class subField5
    {
    }
    ....
}

And how to inheritance the sub-object

3 Answers

The size of a varint is determined by the value: there is nothing to configure or determine.

As for the sub-objects: instead of using string, use a concrete (non-abstract) class, and you should be fine - just use that type instead of string. The library shows both possibilities because without the schema, it can't be sure which it is, since strings and sub-objects use the same protocol layout. But field 5 could be:

[Proto member(5)]
public Whatever SomeName { get; set;}

...

[ProtoContract]
public class Whatever {
    [ProtoMember(1)]
    public int Id {get; set;}

    [ProtoMember(2)]
    public AnotherType AnotherThing {get; set;}
}

this is decorde string:

parser pic

there are multiple field 2.

Finally, I got it:

[ProtoContract]
public class MessageStructs
{
    [ProtoMember(1)]
    public FontsInfo fontInfo;
    [ProtoMember(2)]
    public Structs MsgContent;
}

[ProtoContract]
[ProtoInclude(21, typeof(Structs))]
public interface IInterface
{
    MainContent MessageContent {get;}
    MsgType MessageType {get;}
    Informations MessageOther {get;}
}

[ProtoContract]
public class Structs : IInterface
{
    [ProtoMember(1)]
    public MainContent Message_Content {get; set;}
    [ProtoMember(9)]
    public MsgType Message_Type {get; set;}
    [ProtoMember(37)]
    public Informations Message_Other {get; set;}
}


Related