简体   繁体   English

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

[英]Deserialize string arrays in protocol buffer c++

I am sending an object that contain 2 string arrays from C# program to a C++ program with rabbitmq. 我发送一个包含2个字符串数组的对象从C#程序到带有rabbitmq的C ++程序。 The class in the C# program looks like this: C#程序中的类看起来像这样:

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

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

The code for the serialization of the object in C#: C#中对象序列化的代码:

 byte[] data;

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

Now i want to get the data in the C++ program. 现在我想在C ++程序中获取数据。 I created a proto file: 我创建了一个proto文件:

syntax = "proto2";
package Protobuf;

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

I am receiving the message on the C++ program, but how can i deserialize the data and how to save eventually each string array in a seperate array? 我收到了关于C ++程序的消息,但是我如何反序列化数据以及如何最终将每个字符串数组保存在一个单独的数组中?

Your best bet here is to ask the library to help you out: 这里最好的选择是请图书馆帮助你:

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

This gives you: 这给你:

syntax = "proto2";
package LPRRabbitMq.Class;

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

which tells you how best to represent it. 它告诉你如何最好地代表它。 By using repeated here, you should be able to correctly identify the individual elements in the C++ code. 通过在此处repeated使用,您应该能够正确识别C ++代码中的各个元素。 And by using string , it should come out as an appropriate type for C++. 通过使用string ,它应该作为C ++的适当类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM