ProtoBuf
Welcome to the developer documentation for protocol buffers – a language-neutral, platform-neutral, extensible way of serializing structured data for use in communications protocols, data storage, and more.
protocol buffers 是一种语言无关、平台无关、可扩展的序列化结构数据的方法。它可用于(数据)通信协议、数据存储等。
官方网址:https://developers.google.cn/protocol-buffers/
Guide:https://developers.google.cn/protocol-buffers/docs/overview
github地址:https://github.com/protocolbuffers/protobuf
支持的语言
Protocol buffers currently support generated code in Java, Python, Objective-C, and C++. With our new proto3 language version, you can also work with Dart, Go, Ruby, and C#, with more languages to come.
它支持以下语言:
- Java
- Python
- Objective-C
- C++
在新的proto3版本中,又支持了:
- Dart
- Go
- Ruby
- C#
proto是如何工作的(C++举例)?
You specify how you want the information you’re serializing to be structured by defining protocol buffer message types in .proto files. Each protocol buffer message is a small logical record of information, containing a series of name-value pairs. Here’s a very basic example of a .proto file that defines a message containing information about a person:
在类型为.proto的文件中定义你想要如何序列化结构数据的方法,举个例子如下:
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
As you can see, the message format is simple – each message type has one or more uniquely numbered fields, and each field has a name and a value type, where value types can be numbers (integer or floating-point), booleans, strings, raw bytes, or even (as in the example above) other protocol buffer message types, allowing you to structure your data hierarchically. You can specify optional fields, required fields, and repeated fields. You can find more information about writing .proto files in the Protocol Buffer Language Guide.
消息格式很简单,每个消息类型都有一个或多个字段,每个字段都有一个名字和它的值类型,这个值类型可以是很多(整数,浮点,布尔,字符串,原始字节,