Protocol Buffers
Protocol Buffers are language-neutral, platform-neutral extensible mechanisms for serializing structured data.
What Are Protocol Buffers?
Protocol buffers are Google’s language-neutral, platform-neutral, extensiblemechanism for serializing structured data – think XML, but smaller, faster, andsimpler. You define how you want your data to be structured once, then you canuse special generated source code to easily write and read your structured datato and from a variety of data streams and using a variety of languages.
Pick Your Favorite Language
Protocol buffers support generated code in C++, C#, Dart, Go, Java,Kotlin,Objective-C, Python, and Ruby. With proto3, you can also work with PHP.
Example Implementation
edition="2024";messagePerson{stringname=1;int32id=2;stringemail=3;}Figure 1. A proto definition.
// Java codePersonjohn=Person.newBuilder().setId(1234).setName("John Doe").setEmail("jdoe@example.com").build();output=newFileOutputStream(args[0]);john.writeTo(output);Figure 2. Using a generated class to persist data.
// C++ codePersonjohn;fstreaminput(argv[1],ios::in|ios::binary);john.ParseFromIstream(&input);id=john.id();name=john.name();email=john.email();Figure 3. Using a generated class to parse persisted data.
How Do I Start?
- Downloadand install the protocol buffer compiler.
- Read theoverview.
- Try thetutorial for yourchosen language.