How does Protobuf-net support for Dictionary/KeyVa

2019-05-07 13:36发布

I am trying to understand protobuf-net's Dictionary/KeyValuePair support. We would like to use the underlying binary stream and the generated proto file from java, but the generated .proto file contains what look like a custom type called Pair_String_Int32.

Can someone please shed some light on this?

I've got a class mapped like this:

[DataContract]
public class ForwardCurve
{
    [DataMember(Order=1, IsRequired = true)]
    public string Commodity { get; set; }

    [DataMember(Order = 2, IsRequired = false)]
    public IDictionary<string, int> DictValue { get; set; }

    [DataMember(Order = 3, IsRequired = false)]
    public IList<string> ListValue { get; set; }

}

The generated .proto file using Serializer.GetProto will be:

message ForwardCurve {
   required string Commodity = 1;
   repeated Pair_String_Int32 DictValue = 2;
   repeated string ListValue = 3;
}

So what is Pair_String_Int32 (and what is going into the protobuffer byte stream) and is there any way of mapping this so that protobuf, by using protoc can create the equivalent mapping code in Java?

2条回答
Lonely孤独者°
2楼-- · 2019-05-07 14:01

I can check later (I'm on iPod at the moment) but I believe is it simply a "repeated" set of a dummy type with members key=1 value=2 (using the default types for each - so c# string maps to proto string etc). I am in the process of re-implementing GetProto for v2, so I'll try to ensure these dummy types are included in the output.

查看更多
乱世女痞
3楼-- · 2019-05-07 14:06

To get this for work add a new message to the generated .proto file that looks like this.

message Pair_String_Int32 {
 required string Key = 1;
 required int32 Value = 2;    
}

Then protoc will be able to create the corresponding code for Java.

查看更多
登录 后发表回答