
- Protocol Buffers - Home
- Protocol Buffers - Introduction
- Protocol Buffers - Environment Setup
- Protocol Buffers - Basic App
- Protocol Buffers - Constructs
- Protocol Buffers - message
- Protocol Buffers - string
- Protocol Buffers - Numbers
- Protocol Buffers - bool
- Protocol Buffers - enum
- Protocol Buffers - repeated
- Protocol Buffers - map
- Protocol Buffers - Nested Class
- Protocol Buffers - Optionality & Defaults
- Protocol Buffers - Language Independence
- Protocol Buffers - Compound Data Types
- Protocol Buffers - Command Line Usage
- Protocol Buffers - Rules to Update Definition
- Protocol Buffers - Integration with Kafka
- Protocol Buffers - In Other Languages
- Protocol Buffers Useful Resources
- Protocol Buffers - Quick Guide
- Protocol Buffers - Useful Resources
- Protocol Buffers - Discussion
Protocol Buffers - Nested Class
Overview
Here, we will see how to create a nested class. Protobuf translates this to a nested Java class.
Continuing with our theater example from Protocol Buffers - String chapter, following is the syntax that we need to have to instruct Protobuf that we will be creating a repeated −
theater.proto
syntax = "proto3"; package theater; option java_package = "com.tutorialspoint.theater"; message Theater { TheaterOwner owner = 10; } message TheaterOwner{ string name = 1; string address = 2; }
Now our message class contains a nested class, i.e., information about the owner of the theater.
Creating Java Classes from Proto File
To use Protobuf, we will now have to use protoc binary to create the required classes from this ".proto" file. Let us see how to do that −
protoc --java_out=. theater.proto
This will create a TheaterOuterClass.java class in com > tutorialspoint > theater folder in current directory. We're using this class in our application similar to as done in Protocol Buffers - Basic App chapter.
Using Java Classes created from Proto File
TheaterWriter.java
package com.tutorialspoint.theater; import java.io.FileOutputStream; import java.io.IOException; import com.tutorialspoint.theater.TheaterOuterClass.Theater; import com.tutorialspoint.theater.TheaterOuterClass.TheaterOwner; public class TheaterWriter{ public static void main(String[] args) throws IOException { TheaterOwner owner = TheaterOwner.newBuilder() .setName("Anthony Gonsalves") .setAddress("513, St Paul Street, West Coast, California") .build(); Theater theater = Theater.newBuilder() .setOwner(owner) .build(); String filename = "theater_protobuf_output"; System.out.println("Saving theater information to file: " + filename); try(FileOutputStream output = new FileOutputStream(filename)){ theater.writeTo(output); } System.out.println("Saved theater information with following data to disk: \n" + theater); } }
Next, we will have a reader to read the theater information −
TheaterReader.java
package com.tutorialspoint.theater; import java.io.FileInputStream; import java.io.IOException; import com.tutorialspoint.theater.TheaterOuterClass.Theater; import com.tutorialspoint.theater.TheaterOuterClass.Theater.Builder; public class TheaterReader{ public static void main(String[] args) throws IOException { Builder theaterBuilder = Theater.newBuilder(); String filename = "theater_protobuf_output"; System.out.println("Reading from file " + filename); try(FileInputStream input = new FileInputStream(filename)) { Theater theater = theaterBuilder.mergeFrom(input).build(); System.out.println(theater); } } }
Compile the project
Now that we have set up the reader and the writer, let us compile the project.
mvn clean install
Serialize the Java Object
Now, post compilation, let us execute the writer first −
> java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater information to file: theater_protobuf_output Saved theater information with following data to disk: owner { name: "Anthony Gonsalves" address: "513, St Paul Street, West Coast, California" }
Deserialize the Serialized Object
Now, let us execute the reader to read from the same file −
java -cp .\target\protobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_protobuf_output owner { name: "Anthony Gonsalves" address: "513, St Paul Street, West Coast, California" }
So, as we see, we are able to read the serialized nested class by deserializing the binary data to Theater object.