Protobuf Format Specification & Converter Info
Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.
Introduction
Developed by Google, Protobuf provides a language-neutral, platform-neutral, extensible 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.
Key Features
- Compact: Binary format is much smaller than XML or JSON.
- Fast: Parsing and serialization are highly optimized.
- Schema-Driven: Requires a
.protofile to define the structure of data. - Language Neutral: Supports C++, C#, Java, Python, Go, Ruby, and many more.
Syntax
Protobuf data is structured as messages. Each message is a small logical record of information, containing a series of name-value pairs called fields.
Example (.proto schema)
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}Example (Text Format)
name: "John Doe"
id: 123
email: "jdoe@example.com"
phones {
number: "555-4321"
type: HOME
}Data Types
Common scalar data types in Protobuf include:
- double, float: Floating point numbers.
- int32, int64: Integers.
- bool: Boolean values.
- string: A string must always contain UTF-8 encoded or 7-bit ASCII text.
- bytes: May contain any arbitrary sequence of bytes.
Use Cases
- gRPC: High performance RPC framework that uses Protobuf by default.
- Microservices: Efficient communication between internal services.
- Data Storage: Storing data in a compact, versioned format.
Comparison with JSON
While JSON is text-based and easy to read, Protobuf is binary and more efficient. JSON is self-describing, whereas Protobuf requires a schema.