Hi,all
I have a simple .proto file like this:
/***********************************/
syntax = "proto3";
package test;
message RowInfo {
string key = 1;
map<string, string> fields = 2;
}
/***********************************/
My program needs to serialize RowInfo and save the serialized string into
queue. Consumer will get the serialized string and deserialize it as
RowInfo . In my test.cpp :
/****************test.cpp*******************/
#include "prototest/test.pb.h"
#include <stdio.h>
int main()
{
using namespace test;
RowInfo row;
row.set_key("mykey");
::google::protobuf::Map< ::std::string, ::std::string >* pmap =
row.mutable_fields();
(*pmap)["name"]="jimmy";
(*pmap)["age"]="21";
std::string str;
row.SerializeToString(&str); // 将对象序列化到字符串
printf("%s\n",str.c_str());
RTRowInfo test;
test.ParseFromString(str);
::google::protobuf::Map< ::std::string, ::std::string > map1 =
test.fields();
printf("key:%s name:%s\n",test.key().c_str(),map1["name"].c_str());
return 0;
}
/***********************************/
I have to use mutable_fields method which return a
::google::protobuf::Map< ::std::string, ::std::string >* to
assign RowInfo, is there a way to assign RowInfo more convenient and
efficient.
Another question is about deserialization, it seems work in my test.cpp
, is this a right way to do deserialization.
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.