Protobuf-net 对 Dictionary/KeyValuePair 的支持是如何工作的?
我试图了解 protobuf-net 的 Dictionary/KeyValuePair 支持。我们希望使用底层二进制流和从 java 生成的 proto 文件,但生成的 .proto 文件包含看起来像名为 Pair_String_Int32 的自定义类型。
有人可以解释一下吗?
我有一个像这样映射的类:
[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; }
}
使用 Serializer.GetProto 生成的 .proto 文件将是:
message ForwardCurve {
required string Commodity = 1;
repeated Pair_String_Int32 DictValue = 2;
repeated string ListValue = 3;
}
那么 Pair_String_Int32 是什么(以及进入 protobuffer 字节流的内容),有没有什么方法可以映射它,以便 protobuf 通过使用 protoc 可以在 Java 中创建等效的映射代码吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要使其正常工作,请向生成的 .proto 文件添加一条新消息,如下所示。
然后protoc就能为Java创建相应的代码。
To get this for work add a new message to the generated .proto file that looks like this.
Then protoc will be able to create the corresponding code for Java.
我可以稍后检查(我现在在 iPod 上),但我相信它只是一个“重复”的虚拟类型集,其中成员 key=1 value=2 (使用每个的默认类型 - 所以 c# 字符串映射到原始字符串等)。我正在为 v2 重新实现 GetProto,因此我将尝试确保这些虚拟类型包含在输出中。
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.