Home Docs FAQ Group Download
Protocol Buffers
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.
This documentation is aimed at Java, C++, or Python developers who want to use protocol buffers in their applications.
This overview introduces protocol buffers and tells you what you need to do to get started – you can then go on to follow
the tutorials or delve deeper into protocol buffer encoding. API reference documentation is also provided for all three
languages, as well as language and style guides for writing .proto files.
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller,
faster, and simpler. You define how you want your data to be structured once, then you can use special generated source
code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
You can even update your data structure without breaking deployed programs that are compiled against the "old" format.
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:
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.
Once you've defined your messages, you run the protocol buffer compiler for your application's language on
your .proto file to generate data access classes. These provide simple accessors for each field (like query() and
set_query()) as well as methods to serialize/parse the whole structure to/from raw bytes – so, for instance, if your
chosen language is C++, running the compiler on the above example will generate a class called Person. You can then
use this class in your application to populate, serialize, and retrieve Person protocol buffer messages. You might then
write some code like this:
Developer Guide
What are protocol buffers?
How do they work?
- 1
- 2
前往页