簡體   English   中英

使用protobuf-net序列化對象數組以使用C ++

[英]Serializing arrays of objects with protobuf-net for C++ consumption

我已經建立了一些類按指示在下面的答案在這里 ,為了能夠通用序列化對象的列表; 例如KeyValuePair和KeyValuePair的實例。

不幸的是,似乎由GetProto()方法生成的.proto文件沒有生成可以為C ++正確解析的文件。 為數組生成的子類型消息后綴為“ []”。 為C ++編譯時,protoc.exe在“ []”上cho住。

由於消息名稱對於protobuf似乎是任意的(也就是說,它們實際上並未包含在數據流中),因此可以在命名子類型時告訴protobuf-net使用“ _Array”而不是“ []” ? 還是我應該采取其他方法,以便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<KeyValuePairResponse>()創建的.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;
}

這只是GetProto中的錯誤。 我建議將其記錄在github protobuf-net列表上,如果您喜歡冒險,甚至可以提交請求請求。

現在: Ctrl + h (查找並替換)可能是您的朋友。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM