Category: gRPC
-
Enums in Protocol Buffers
Protocol Buffers (Protobuf) support enum types, which allow you to define a fixed set of named constants. Enums are useful when a field should only take one value from a predefined list, such as status codes, categories, or types. A typical real-world example is a car’s body style, where the value should be one of…
-
Modeling More Complex Data Structures in Protobuf
When you start working with Protocol Buffers, you quickly notice that simple key–value mappings are easy to represent. Protobuf provides a map<K, V> type that works very well when each key corresponds to a single value. However, real-world data is often more complex, and sometimes a single key must be associated with multiple values rather…
-
Using map in Protocol Buffers
In this lesson, we learn how to use the map field type in Protocol Buffers, how it is defined in a .proto file, how code is generated from it, and how we interact with it from Java. The goal is to understand how Protobuf maps behave, how they relate to Java maps, and which helper…
-
Protocol Buffers and Uniqueness in Collections
When developers start using repeated fields in Protocol Buffers, a very natural and frequently asked question appears sooner or later: “If repeated behaves like a list, what should I do when I want only unique items like a Set?” This question is completely valid, but it reveals a deeper conceptual confusion about what Protocol Buffers…
-
Protocol Buffers Collections
So far, we have modeled single objects in Protocol Buffers, such as a Person, Address, or Student. However, real-world systems often need to handle collections of items, such as a list of books in a library, a list of orders in an e-commerce system, or a list of messages in a chat application. In this…
-
Protocol Buffers Composition
1. Introduction In this tutorial, we move to the next important concept in Protocol Buffers: composition, where one message type contains another message type as a field. This is very similar to object composition in Java, where one class holds a reference to another class. 2. What is Composition in Protobuf? Composition in Protobuf means:…
-
Protobuf Serialization and Deserialization in Java
1. Introduction When we use Protocol Buffers in real applications, defining messages is only the first step, because the true power of Protobuf appears when we serialize those messages into a compact binary form for transmission or storage, and later deserialize them back into objects. In this tutorial, we will focus on the essential ideas…
-
Understanding Scalar Types in Protocol Buffers
1. Introduction In Java, we begin learning data modeling with primitive data types such as int, long, float, double, char, and boolean, and these primitives act as the fundamental building blocks from which we construct classes, and then through composition of classes we gradually build richer abstractions that model real-world problems. Protocol Buffers follows a…
-
Equals and clear method with a Protobuf object
In this tutorial, we will explore how a Protocol Buffers (Protobuf) generated Person object behaves in Java, especially in terms of: 1. Reusing the Existing Person Class Assume you already generated a Person class from a .proto file and created a helper method to build a person object. For example: Moving creation logic into a…
-
Generating Separate Java Classes from Protobuf Messages with java_multiple_files
1. Introduction When working with Protobuf in Java projects, one common surprise is the “outer class” that Protobuf generates around your messages. Many developers expect to access their message types directly, but instead they find themselves navigating through a wrapper class. This tutorial explains: We will build a clear mental model so you understand what…
