反序列化协议缓冲区c ++中的字符串数组

Deserialize string arrays in protocol buffer c++

我正在使用 rabbitmq 将包含 2 个字符串数组的对象从 C# 程序发送到 C++ 程序。 C#程序中的class是这样的:

namespace LPRRabbitMq.Class
{
    [ProtoContract(SkipConstructor = true)]
    public class BlackAndWhiteList
    {
        [ProtoMember(1)]
        public string[] BlackList { get; set; }

        [ProtoMember(2)]
        public string[] WhiteList { get; set; }
   }
}

C#对象序列化代码:

 byte[] data;

 using (var ms = new MemoryStream())
 {
     Serializer.Serialize(ms, blackAndWhite);
     data = ms.ToArray();
 }

现在我想在C++程序中获取数据。我创建了一个原型文件:

syntax = "proto2";
package Protobuf;

message BlackAndWhiteList {
    optional bytes BlackList = 1;
    optional bytes WhiteList = 2;
}

我在 C++ 程序上收到消息,但我如何反序列化数据以及如何最终将每个字符串数组保存在单独的数组中?

你最好的办法是请求图书馆帮助你:

var proto = Serializer.GetProto<BlackAndWhiteList>(ProtoSyntax.Proto2);

这给你:

syntax = "proto2";
package LPRRabbitMq.Class;

message BlackAndWhiteList {
   repeated string BlackList = 1;
   repeated string WhiteList = 2;
}

它告诉您如何最好地表示它。通过在此处使用 repeated,您应该能够正确识别 C++ 代码中的各个元素。通过使用 string,它应该作为适合 C++ 的类型出现。