Tutorials     About     RSS
Tech and Media Labs

RionObjectReader

Jakob Jenkov
Last update: 2020-05-20

The RION Ops RionObjectReader class can read a RION Object field into a Java object. In other words, the RionObjectReader can deserialize RION into Java objects. In this RionObjectReader tutorial I will explain how to use the RION Ops RionObjectReader.

Create a RionObjectReader

To use the RionObjectReader you must first create an instance of it. The easiest way to create an instance of the RionObjectReader is via the RionObjectReaderBuilder class. This class makes it easier to configure and create a RionObjectReader instance. Here is an example of creating a RionObjectReader via the RionObjectReaderBuilder class:

RionObjectReaderBuilder readerBuilder = new RionObjectReaderBuilder();

readerBuilder.setTypeClass(Pojo.class);
readerBuilder.addDeclaredFieldsForTypeClass();

RionObjectReader reader = readerBuilder.build();

The RionObjectReaderBuilder is designed for "method chaining", so the above code could be written a bit clearer like this:

RionObjectReader reader =
        new RionObjectReaderBuilder()
            .setTypeClass(Pojo.class)
            .addDeclaredFieldsForTypeClass()
            .build();

Read an Object

To read a Java object via the RionObjectReader you must first have a RION Object field stored in a Java byte array. Once you have that, you can read that RION Object field via the RionObjectReader.

The RionObjectReader can read both acyclic and cyclic object graphs. Reading an acyclic object graph is simpler than reading a cyclic object graph. Therefore the the RionObjectReader has two read methods - one for reading acyclic object graphs and one for reading cyclic object graphs. Both are covered in the following sections.

readAcyclic()

The RionObjectReader readAcyclic() method reads an acylic object graph. An acyclic object graph is a graph of objects where each object is only referenced once by any other object in the graph.

Here is an example that first writes a Pojo instance as RION into a Java byte array, and then reads it into a Java object again:

Pojo pojo = new Pojo();

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

byte[] data = new byte[1024];
int bytesWritten = writer.writeAcyclic(pojo, 2, data, 0);


RionObjectReaderBuilder readerBuilder = new RionObjectReaderBuilder();
readerBuilder.setTypeClass(Pojo.class);
readerBuilder.addDeclaredFieldsForTypeClass();
RionObjectReader reader = readerBuilder.build();

Pojo pojoRead = (Pojo) reader.readAcyclic(data, 0);

readCyclic()

The RionObjectReader readCyclic() method reads a cyclic object graph stored as RION in a byte array. A cyclic object graph is an object graph where each object may be referenced more than once by other objects in the graph.

Here is an example that first writes a Pojo instance as RION into a Java byte array as a cyclic object graph, and then reads the object graph back again. Note: In this example you cannot really see that the Pojo object serialized to RION represents a cyclic object graph, so just imagine that it does.

Pojo pojo = new Pojo();

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

byte[] data = new byte[1024];
int bytesWritten = writer.writeCyclic(pojo, 2, data, 0);


RionObjectReaderBuilder readerBuilder = new RionObjectReaderBuilder();
readerBuilder.setTypeClass(Pojo.class);
readerBuilder.addDeclaredFieldsForTypeClass();
RionObjectReader reader = readerBuilder.build();

Pojo pojoRead = (Pojo) reader.readCyclic(data, 0);

Jakob Jenkov

Featured Videos

















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next