【问题标题】:Serializing arrays of objects with protobuf-net for C++ consumption使用 protobuf-net 序列化对象数组以供 C++ 使用
【发布时间】:2014-10-23 19:46:10
【问题描述】:

我已经按照以下答案here 中的说明构建了一些类,以便能够序列化通用对象列表;例如 KeyValuePair 和 KeyValuePair 的实例。

不幸的是,GetProto() 方法生成的 .proto 文件似乎没有生成可以为 C++ 正确解析的文件。为数组生成的子类型消息以“[]”为后缀。为 C++ 编译时,protoc.exe 在“[]”上阻塞。

由于 protobuf 的消息名称似乎是任意的(也就是说,它们实际上并未包含在数据流中),因此可以告诉 protobuf-net 在命名时使用“_Array”而不是“[]”子类型?还是我应该采取其他途径以便生成的 .proto 文件可以被 C++ 应用程序使用?

谢谢,

下面是相关代码和生成的proto文件。

基类是:

[DataContract]
[ProtoInclude(101, typeof(KeyValuePairResponse<string>))]
[ProtoInclude(102, typeof(KeyValuePairResponse<int>))]
[ProtoInclude(103, typeof(KeyValuePairResponse<double>))]
[ProtoInclude(111, typeof(KeyValuePairResponse<string[]>))]
[ProtoInclude(112, typeof(KeyValuePairResponse<int[]>))]
[ProtoInclude(113, typeof(KeyValuePairResponse<double[]>))]
public abstract class KeyValuePairResponse
{
    protected KeyValuePairResponse() { }

    [DataMember(Order = 1, IsRequired = true)]
    public string Key { get; set; }

    public object Value
    {
        get
        {
            return this.ValueImplementation;
        }

        set
        {
            this.ValueImplementation = value;
        }
    }

    protected abstract object ValueImplementation { get; set; }

    public static KeyValuePairResponse<T> Create<T>(string key, T value)
    {
        return new KeyValuePairResponse<T>(key, value);
    }
}

通用类是:

[DataContract]
public sealed class KeyValuePairResponse<T> : KeyValuePairResponse
{
    public KeyValuePairResponse()
    {
    }

    public KeyValuePairResponse(string key, T value)
    {
        this.Key = key;
        this.Value = value;
    }

    [DataMember(Order = 2, IsRequired = true)]
    public new T Value { get; set; }

    protected override object ValueImplementation
    {
        get
        {
            return this.Value;
        }

        set
        {
            this.Value = (T)value;
        }
    }
}

GetProto&lt;KeyValuePairResponse&gt;() 创建的 .proto 文件如下所示:

message KeyValuePairResponse {
   required string Key = 1;
   // the following represent sub-types; at most 1 should have a value
   optional KeyValuePairResponse_String KeyValuePairResponse_String = 101;
   optional KeyValuePairResponse_Int32 KeyValuePairResponse_Int32 = 102;
   optional KeyValuePairResponse_Double KeyValuePairResponse_Double = 103;
   optional KeyValuePairResponse_String[] KeyValuePairResponse_String[] = 111;
   optional KeyValuePairResponse_Int32[] KeyValuePairResponse_Int32[] = 112;
   optional KeyValuePairResponse_Double[] KeyValuePairResponse_Double[] = 113;
}
message KeyValuePairResponse_Double {
   required double Value = 2 [default = 0];
}
message KeyValuePairResponse_Double[] {
   repeated double Value = 2;
}
message KeyValuePairResponse_Int32 {
   required int32 Value = 2 [default = 0];
}
message KeyValuePairResponse_Int32[] {
   repeated int32 Value = 2;
}
message KeyValuePairResponse_String {
   required string Value = 2;
}
message KeyValuePairResponse_String[] {
   repeated string Value = 2;
}

【问题讨论】:

    标签: c# c++ protobuf-net


    【解决方案1】:

    这只是 GetProto 中的一个错误。我建议将其记录在 github protobuf-net 列表中,或者如果您喜欢冒险,甚至可以提交拉取请求。

    现在:Ctrl+h(查找和替换)可能是你的朋友。

    【讨论】:

    • 谢谢马克。一般来说,我是 protobuf 的新手,但怀疑这些名称很容易被替换。当我有时间(大声笑)时,我会查看 github 存储库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多