Skip to content

Serialization

Rajab Davudov edited this page Apr 7, 2019 · 7 revisions

Serialization

Bio Objects are serialized strongly according to dictionary definitions. code values for objects and tags are used in marking bytes and tag type is also used converting value to bytes and vice versa. For serialization BioObjectBinaryParser class should be used.

Car c = new Car() ;
c.set(Car.PRODUCER, "Toyota") ;
c.set(Car.YEAR_OF_PRODUCTION, 2019) ;
c.set("undefined tag", "Hello World") ;

BioObjectBinaryParser parser = new BioObjectBinaryParser() ;
byte[] encoded = parser.encode(c) ;

Bio Object only serializes tags and values. No other class information is serialized. We rely on dictionary definition on the other side which will decode same bytes.

Car c2 = (Car) parser.decode(encoded) ;

Note that only tags having definition will be encoded. Therefore, "undefined tag" will be lost during these steps.

Note that tags having isEncodable=false property are also will not be encoded.

Here is full implementation of encode()

public byte[] encode(Object object, boolean isCompressed, boolean isLossless, boolean isEncrypted) {
...
}

Compression

You can specify compressor for compression of bytes and set compression flag during encoding as follows:

byte[] encoded = parser.encode(c, true) ;

First boolean is flag indicates isCompressed.

Lossless

There can be a case where we have lots of additional undefined tags but still want to encode them. In this case BioObjectBinarParser supports lossless encoding which actually exports Bio Object to XML and serializes XML to bytes with a custom flag. Otherwise, all undefined tags will be removed during serialization. While decoding, parser knows that these bytes are output of XML and reverse same operation and you get exactly same Bio Object as before.

byte[] encoded = parser.encode(c, false, true) ;

Second boolean is flag indicates isLossless.

Encryption

You can specify encrypter for encryption and set encryption flag during encoding as follows:

byte[] encoded = parser.encode(c, true, false, true) ;

Third boolean is flag indicates isEncrypted.

Arrays and Lists

You can also encode Bio Object arrays or lists. Nothing changes from coding side.

Car[] cArray = new Car[3] ;
for (int i=0; i < cArray.length; i++) {
	cArray[i] = new Car() ;
	cArray[i].set(Car.PRODUCER, "Toyota") ;
	cArray[i].set(Car.YEAR_OF_PRODUCTION, 2010 + i) ;
}

BioObjectBinaryParser parser = new BioObjectBinaryParser() ;
byte[] encoded = parser.encode(cArray) ;
Car[] cArray2 = (Car[]) parser.decode(encoded) ;