- Protobuf - Discussion
- Protobuf - Useful Resources
- Protobuf - Quick Guide
- Protobuf - In Other Languages
- Protobuf - Integration with Kafka
- Protobuf - Rules to Update Definition
- Protobuf - Command Line Usage
- Protobuf - Compound Data Types
- Protobuf - Language Independence
- Protobuf - Optionality & Defaults
- Protobuf - Nested Class
- Protobuf - Map
- Protobuf - List/Repeated
- Protobuf - Enum
- Protobuf - Boolean
- Protobuf - Numbers
- Protobuf - Strings
- Protobuf - Class/Member
- Protobuf - Constructs
- Protobuf - Basic App
- Protobuf - Introduction
- Protobuf - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Protobuf - Boolean
The "bool" data type is one of the basic building blocks of Protobuf. It translates to Boolean in the languages that we use, for example, Java, Python, etc.
Continuing with the theater example, following is the syntax that we need to have to instruct Protobuf that we will be creating a Boolean attribute −
syntax = "proto3"; package theater; option java_package = "com.tutorialspoint.theater"; message Theater { bool drive_in = 6; }
Now our message class contains a Boolean attribute. It also has a position which is what Protobuf uses while seriapzation and deseriapzation. Each attribute of a member needs to have a unique number assigned.
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=java/src/main/java proto_files heater.proto
The above command will create the required files and now we can use it in our Java code. First let s create a writer to write the theater information −
package com.tutorialspoint.theater; import java.io.FileOutputStream; import java.io.IOException; import com.tutorialspoint.theater.TheaterOuterClass.Theater; pubpc class TheaterWriter { pubpc static void main(String[] args) throws IOException { Theater theater = Theater.newBuilder() .setTotalCapcity(320) .setMobile(98234567189L) .setBaseTicketPrice(22.45f) .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: " + theater); } }
Next, we will have a reader to read the theater information −
package com.tutorialspoint.theater; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.tutorialspoint.greeting.Greeting.Greet; import com.tutorialspoint.theater.TheaterOuterClass.Theater; import com.tutorialspoint.theater.TheaterOuterClass.Theater.Builder; pubpc class TheaterReader{ pubpc 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.getBaseTicketPrice()); System.out.println(theater); } } }
Now, post compilation, let us execute the writer first −
> java -cp . argetprotobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter Saving theater information to file: theater_protobuf_output Saved theater information with following data to disk: drive_in: true
Now, let us execute the reader to read from the same file −
java -cp . argetprotobuf-tutorial-1.0.jar com.tutorialspoint.theater.TheaterReader Reading from file theater_protobuf_output drive_in: true
So, as we see, we are able to read the seriapzed Boolean by deseriapzing the binary data to Theater object.
Advertisements