我是protobuf的新手,我已开始考虑以下微不足道的例子。
即使“id”字段是必需的,但由于未设置它,序列化缓冲区的大小为0(??)。
当我反序列化消息时,发生错误:
message Entry {
required int32 id = 1;
}
被C++代码使用
#include <iostream>
#include "example.pb.h"
int main() {
std::string mySerialized;
{
Entry myEntry;
std::cout << "Serialization succesfull "
<< myEntry.SerializeToString(&mySerialized) << std::endl;
std::cout << mySerialized.size() << std::endl;
}
Entry myEntry;
std::cout << "Deserialization successfull "
<< myEntry.ParseFromString(mySerialized) << std::endl;
}
即使“id”字段是必需的,但由于未设置它,序列化缓冲区的大小为0(??)。
当我反序列化消息时,发生错误:
[libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "Entry" because it is missing required fields: id
这是正常的行为吗?
Francesco
附言- 如果我将"id"初始化为0,行为会有所不同
附言2- SerializeToString
函数返回true,ParseFromString
函数返回false