Trying_To_Understand
Trying_To_Understand

Reputation: 161

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. The class in the C# program looks like this:

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#:

 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. I created a proto file:

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?

Upvotes: 3

Views: 1126

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062945

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. And by using string, it should come out as an appropriate type for C++.

Upvotes: 2

Related Questions