I'm working on a C++17 project that uses protobuf for data serialization, but I come across a problem.
I tried to serialize an object defined in protobuf into std::string and the object has only one enum field, when accidentally the value of the field is set to 0, the function SerializeAsString() or SerializToString(std::string*) will return true but I always get an empty string.
I checked out google api reference but got nothing helpful, can any one help me? I wrote some code to illustrate the problem.
Suppose we want to define a kind of message in testProtobuf.proto
syntax = "proto3";
package pb;
message testPackage{
enum testEnums{
TEST_0=0;
TEST_1=1;
TEST_2=2;
}
testEnums enumValue=1;
}
I compile it using
protoc --cpp_out=./ testProtobuf.proto
And in testProtobuf.cpp we have:
#include<iostream>
#include<string>
#include "testProtobuf.pb.h"
int main(){
pb::testPackage t0,t1,t2;
t0.set_enumvalue(pb::testPackage::TEST_0);
t1.set_enumvalue(pb::testPackage::TEST_1);
t2.set_enumvalue(pb::testPackage::TEST_2);
std::cout<<t0.ShortDebugString()<<"\n"<<t1.ShortDebugString()<<"\n"<<t2.ShortDebugString()<<std::endl;
std::string temp;
std::cout<<"Success? "<<t0.SerializeToString(&temp)<<", Length: "<<temp.length()<<std::endl;
std::cout<<"Success? "<<t1.SerializeToString(&temp)<<", Length: "<<temp.length()<<std::endl;
std::cout<<"Success? "<<t2.SerializeToString(&temp)<<", Length: "<<temp.length()<<std::endl;
return 0;
}
Then we compile the files:
g++ testProtobuf.cpp testProtobuf.pb.cc -lprotobuf -lpthread -o test.exe
When running the executable, it prints out these with the first line left blank:
enumValue: TEST_1
enumValue: TEST_2
Success? 1, Length: 0
Success? 1, Length: 2
Success? 1, Length: 2
I doubt this has something to do with '\0', but I don't have a better idea to make it work. Even SerializeToArray() returns true and leave the buffer I passed in untouched. All I need is to get the serialized form of the object. Can any one help me? Great thanks!!
The version of protoc is 3.6.1, the version of g++ is 9.3.0, and the host system is Ubuntu 20.04.3