使用protobuf-net序列化对象数组以供C++使用
Serializing arrays of objects with protobuf-net for C++ consumption
我已经按照下面的答案中的指示构建了一些类,以便能够序列化泛型对象的列表;例如KeyValuePair和KeyValuePaire的实例。
不幸的是,GetProto()方法生成的.proto文件似乎没有生成一个可以为C++正确解析的文件。为数组生成的子类型消息以"[]"作为后缀。为C++编译时,protocol.exe阻塞了"[]"。
由于protobuf的消息名称似乎是任意的(也就是说,它们实际上不包括在数据流中),有可能告诉protobuf-net在命名子类型时使用"_Array"而不是"[]"吗?或者,我是否应该采取其他途径,以便C++应用程序可以使用生成的.proto文件?
谢谢,
以下是相关代码和生成的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中的一个bug。我建议将其登录到github protobuf网络列表中,如果你有冒险精神,甚至可以提交一个拉取请求。
目前:Ctrl+h(查找并替换)可能是你的朋友。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用新行和不使用新行读取文件
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 使用CMake创建QML插件
- 使用strcpy将char数组的元素复制到另一个数组
- 在c++中使用nlohmann从类到json的转换
- 使用指针从C++中的数组中获取最大值
- 使用不带参数的函数访问结构元素