Tutorials     About     RSS
Tech and Media Labs

RionObjectWriter

Jakob Jenkov
Last update: 2020-01-21

The RION Ops RionObjectWriter class makes it easy to write Java objects as a RION field to a byte array. In other words, the RionObjectWriter can serialize Java objects to RION. In this Java RionObjectWriter tutorial we will take a closer look at how to use the RionObjectWriter in RION Ops for Java.

Create a RionObjectWriter

To use the RionObjectWriter you must first create an instance of the RionObjectWriter class, com.nanosai.rionops.rion.object.RionObjectWriter. The easiest way to create a RionObjectWriter instance is via the RionObjectWriterBuilder class. Here is an example of using a RionObjectWriterBuilder to create a RionObjectWriter instance:

RionObjectWriterBuilder writerBuilder = new RionObjectWriterBuilder();
writerBuilder.addDeclaredFields(Pojo.class);

RionObjectWriter rionObjectWriter = writerBuilder.build();

The RionObjectWriterBuilder is designed for "method chaining", so the above example can be written a bit more densely:

RionObjectWriter writer =
        new RionObjectWriterBuilder().addDeclaredFields(Pojo.class).build();

These examples build a RionObjectWriter which will serialize all fields (private, protected, default and public) found in the Pojo class to RION. The RionObjectWriterBuilder inspects the Pojo Class via Java Reflection to see what fields it contains, and create field writers for each of the fields.

Once created, the RionObjectWriter instance then only serialize objects of the given class. To serialize objects of multiple classes you will have to create a RionObjectWriter for each class.

Note: You only need one RionObjectWriter per root class you want to serialize. If a given class represents an object graph consisting of other classes referenced from within the root class (or any of its field's classes recursively), all of these classes will be serialized too.

To elaborate, in the example above, if the Pojo class contained fields of class Pojo2 and Pojo3 internally, these would be serialized too because they are part of an object graph rooted by a Pojo object. But the RionObjectWriter created in the example above could not serialize a Pojo2 or Pojo3 directly - as root of an object graph. For that you would have to create a separate RionObjectWriter for each of Pojo2 and Pojo3.

Write Object

The RionObjectWriter has a method named writeObject() which can write a single object serialized to RION into a byte array. The object must represent an acyclic object graph. If not, the writeObject() method will enter an infinite recursion and eventually crash. Here is an example of writing an object using the RionObjectWriter writeObject() method:

Pojo pojo = new Pojo();
pojo.setId(999);
pojo.setName("Pojo 1");

byte[] dest = new byte[128];

RionObjectWriterBuilder writerBuilder = new RionObjectWriterBuilder();
writerBuilder.addDeclaredFields(Pojo.class);

RionObjectWriter rionObjectWriter = writerBuilder.build();

int bytesWritten = rionObjectWriter.writeObject(pojo, 1, dest, 0);

The bytesWritten variable will contain the value 20 after running this code, as the serialized Pojo object takes 20 bytes to represent as RION.

The serialized RION looks like this (in hexadecimal notation):

C1 12                (Object)
    E2 6964          (Key Short: id)
    22 03E7          (Int64Pos: 999)
    E4 6E616D65      (Key Short: name)
    66 506F6A6F2031  (UTF Short: Pojo 1)

Jakob Jenkov

Right 1
Right 2
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next