Fundamentals 8 min read

Getting Started with Protocol Buffers: Serialize and Deserialize Objects in Java

This guide explains what Protocol Buffers are, compares them with XML, shows how to define a .proto schema, compile it with protoc, add the generated classes to a Java Gradle project, and demonstrates object serialization and deserialization with concrete code examples.

The Dominant Programmer
The Dominant Programmer
The Dominant Programmer
Getting Started with Protocol Buffers: Serialize and Deserialize Objects in Java

Protocol Buffers overview

Protocol Buffers (protobuf) is a language‑independent, platform‑independent, extensible method for serializing structured data. Compared with XML, protobuf messages are 3–10 × smaller, 20–100 × faster to parse, and have a simpler syntax.

Defining a .proto file

A .proto file declares the data structures to be serialized. Each message type contains fields identified by unique numeric tags (e.g., =1). In protobuf 2 the keywords required and optional indicate mandatory or optional fields; protobuf 3 removes these keywords. Example of a simple Person message (protobuf 2 syntax):

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;
}

Compiling the .proto file

The protobuf compiler protoc translates a .proto file into language‑specific source files. The compiler can be downloaded from the official releases page:

https://github.com/protocolbuffers/protobuf/releases

After extracting the archive, add protoc.exe (or the platform binary) to the system PATH. Running protoc without arguments prints the usage message, confirming a successful installation.

Java project setup

Create a Gradle (or Maven) project and add the protobuf Java runtime libraries. Example build.gradle snippet (version 3.13.0):

dependencies {
    compile ([group: 'com.google.protobuf', name: 'protobuf-java', version: '3.13.0'],
             [group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.13.0'])
}

The dependencies are resolved from Maven Central.

Student.proto example

Place the schema file under src/main/protobuf with the following content:

syntax = "proto3";
package com.badao.protobuf;
option optimize_for = SPEED;
option java_package = "com.badao.protobuf";
option java_outer_classname = "DataInfo";

message Student {
    string name = 1;
    int32 age = 2;
    string address = 3;
}

The optimize_for = SPEED option generates code prioritising runtime speed (at the cost of larger binary size). java_package sets the generated Java package, and java_outer_classname defines the outer class name ( DataInfo).

Generating Java classes

From the project’s terminal run:

protoc --java_out=src/main/java src/main/protobuf/Student.proto

The command creates DataInfo.java containing the nested class Student in the directory src/main/java/com/badao/protobuf.

Serialization and deserialization

Example Java program that builds a Student instance, serializes it to a byte array, and parses it back:

package com.badao.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;

public class ProtobufTest {
    public static void main(String[] args) throws InvalidProtocolBufferException {
        DataInfo.Student student = DataInfo.Student.newBuilder()
                .setName("公众号:霸道的程序猿")
                .setAge(100)
                .setAddress("中国")
                .build();
        byte[] bytes = student.toByteArray();
        DataInfo.Student student1 = DataInfo.Student.parseFrom(bytes);
        System.out.println(student1.getName());
        System.out.println(student1.getAge());
        System.out.println(student1.getAddress());
    }
}

The builder pattern is required to set fields. toByteArray() produces the protobuf binary representation; parseFrom reconstructs the object from the byte array. Running the program prints the original field values, confirming successful round‑trip serialization.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaserializationProtobufGradleDeserializationProtocol Buffers
The Dominant Programmer
Written by

The Dominant Programmer

Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.