Protobuf-net 对 Dictionary/KeyValuePair 的支持是如何工作的?

发布于 2024-09-26 12:23:46 字数 883 浏览 3 评论 0原文

我试图了解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我乃一代侩神 2024-10-03 12:23:46

要使其正常工作,请向生成的 .proto 文件添加一条新消息,如下所示。

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

然后protoc就能为Java创建相应的代码。

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.

青巷忧颜 2024-10-03 12:23:46

我可以稍后检查(我现在在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文