Flat Buffers - Language Independence



Overview

Till now, we have been using Java to serialize and deserialize the Movie Theater data. However, one of the key features that Google Flat buffers provides is "language independence". In this chapter, we will see how to serialize using Java and deserialize using Python.

Continuing with our theater example from Flat Buffers - String chapter, following is the schema that we are using in this example −

theater.fbs

namespace com.tutorialspoint.theater;

table Theater {
   name:string;
   address:string;
}
root_type Theater;

Serialization using Java

To use Flat Buffers, we will now have to use flatc binary to create the required classes from this ".fbs" file. Let us see how to do that −

flatc --java theater.fbs

This will create a Theater.java class in com > tutorialspoint > theater folder in current directory. We're using this class in our application similar to as done in Flat Buffers - Schema chapter.

Using Java Classes created from fbs File

First let's create a writer to write the theater information −

TheaterWriter.java

package com.tutorialspoint.theater;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.google.flatbuffers.FlatBufferBuilder;

public class TheaterWriter {
   public static void main(String[] args) throws FileNotFoundException, IOException {
      // create a flat buffer builder
      // it will be used to create Theater FlatBuffer
      FlatBufferBuilder builder = new FlatBufferBuilder(1024);

      int name = builder.createString("Silver Screener");
      int address = builder.createString("212, Maple Street, LA, California");

      // create theater FlatBuffers using startTheater() method
      Theater.startTheater(builder);
      // add the name and address to the Theater FlatBuffer
      Theater.addName(builder, name);
      Theater.addAddress(builder, address);

      // mark end of data being entered in Greet FlatBuffer
      int theater = Theater.endTheater(builder);

      // finish the builder
      builder.finish(theater);

      // get the bytes to be stored
      byte[] data = builder.sizedByteArray();

      String filename = "theater_flatbuffers_output";
      System.out.println("Saving theater to file: " + filename);
      // write the builder content to the file named theater_flatbuffers_output
      try(FileOutputStream output = new FileOutputStream(filename)){
         output.write(data);
      }
      System.out.println("Saved theater with following data to disk: \n" + 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\flatbuffers-tutorial-1.0.jar com.tutorialspoint.theater.TheaterWriter

Saving theater to file: theater_flatbuffers_output
Saved theater with following data to disk:
72

Deserialize the Serialized Object Using Python

Generate Python classes from proto file

Let us generate the python code for Theater class −

flatc  --python theater.fbs

Post execution of this command, you will notice an auto-generated class Theater.py in com > tutorialspoint > theater folder in current directory. This class would help us with deserialization of the Theater object.

Using Generated Python Classes

Now, let us write the reader of the data, which will read the file containing serialized object using java. −

theaterReader.py

import Theater

filename = "E:/theater_flatbuffers_output";
print("Reading from file: " + filename)

theater = Theater.Theater()

f = open(filename, "rb")
buf = f.read()
buf = bytearray(buf)
theater = theater.GetRootAs(buf);
f.close()

print("Name: " + theater.Name().decode("utf-8"))
print("Address: " + theater.Address().decode("utf-8"))

And then, let us execute the reader.

py theaterReader.py

Reading from file: E:/theater_flatbuffers_output
Name: Silver Screener
Address: 212, Maple Street, LA, California

So, as we see, all the values which were written by the Java client were correctly deserialized and read by our Python client which effectively means Flat Buffers is language independent.

Advertisements