协议缓冲区
协议缓冲区是与语言无关、与平台无关的可扩展机制,用于序列化结构化数据。
message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
}
// Java code
Person john = Person.newBuilder()
.setId(1234)
.setName("John Doe")
.setEmail("[email protected]")
.build();
output = new FileOutputStream(args[0]);
john.writeTo(output);
// C++ code
Person john;
fstream input(argv[1],
ios::in | ios::binary);
john.ParseFromIstream(&input);
id = john.id();
name = john.name();
email = john.email();