heechan.yang

[ProtoBuf] The Surface of Protocol Buffer 본문

Library | Tool

[ProtoBuf] The Surface of Protocol Buffer

heechan.yang 2023. 10. 11. 16:44

Protocol Buffer is mechanism to serialalize structured data to a raw byte stream and also parse a raw byte stream to a structure data.

A structure is defined in a file with .proto extension. Compiling this file enables the user to construct a class that is capable of parsing a raw byte stream to its form of structure and back to raw byte stream.

 

Example

// .proto file
message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;
}
// C++ code
Person john;
fstream input(argv[1], ios::in | ios::binary);
john.ParseFromIstream(&input);
int id = john.id();
std::string name = john.name();
std::string email = john.email();

References

[1] Protocol Buffer by Google