序列化与反序列化
bool SerializeToString(string* output) const;: serializes the message and stores the bytes in the given string. Note that the bytes are binary, not text; we only use the string class as a convenient container.
bool ParseFromString(const string& data);: parses a message from the given string.
bool SerializeToOstream(ostream* output) const;: writes the message to the given C++ ostream.
bool ParseFromIstream(istream* input);: parses a message from the given C++ istream.
操作protobufAPI
bool IsInitialized() const;: checks if all the required fields have been set.
string DebugString() const;: returns a human-readable representation of the message, particularly useful for debugging.
void CopyFrom(const Person& from);: overwrites the message with the given message's values.
void Clear();: clears all the elements back to the empty state.
常用小技巧
1 一般的get函数的接口跟你的列名一样即get函数 fieldnum,并且返回的是const 类型 。set函数的接口的是set_fieldnum。
2 mutable_fieldnum 如果存在则返回存在的,不存在返回一个空的对象
3 Protobuf 只要fieldnum 不改,即使该列的类型名更改也不影响兼容,老代码同样能兼容新pb文件
更改前
a.proto
message Iner_a {
}
message b {
optional Iner_a field_name;
}
更改后
message Iner_b {
}
message b {
optional Iner_b field_name;
}