vertx-binary-serialization
A simple binary serialization method for vertx which uses annotations & reflection like spring jpa for databases.
Requirements
- Java 8+
- vert.x
Maven Coordinates
<dependency>
<groupId>com.jukusoft</groupId>
<artifactId>vertx-binary-serializer</artifactId>
<version>1.0.8</version>
</dependency>
<!-- If you want to use TCPServer and TCPClient, you need also this dependency -->
<dependency>
<groupId>com.jukusoft</groupId>
<artifactId>vertx-binary-serializer-connection</artifactId>
<version>1.0.8</version>
</dependency>
HowTo
First create some message objects which contains some datatypes:
@MessageType(type = 0x01)
@ProtocolVersion(1)
public class Message implements SerializableObject {
@SInteger
public int test = 0;
}
@MessageType(type = 0x02)
@ProtocolVersion(2)
public class SecondMessage implements SerializableObject {
@SFloat
public float a = 0f;
@SString
public String myString = null;
}
You have to add the annotations MessageType
with the type (1 byte as type, 1 byte as extended type) and ProtocolVersion
to check, if Serializer on other side can unserialize this object.
Then you can serialize and unserialize this object easely:
//first, register this new message types
TypeLookup.register(Message.class);
TypeLookup.register(SecondMessage.class);
//create message object which implements SerializableObject
Message msg = new Message();
msg.test = 20;
SecondMessage msg1 = new SecondMessage();
msg1.a = 0.2f;
msg1.myString = "my-new-string";
//serialize object into byte buffer
Buffer buffer = Serializer.serialize(msg);
//unserialize object from byte buffer
Message obj1 = Serializer.unserialize(buffer);
//get value
System.out.println("test value: " + obj1.test);
//second message
//serialize object into byte buffer
Buffer buffer = Serializer.serialize(msg1);
//unserialize object from byte buffer
SecondMessage obj2 = Serializer.unserialize(buffer);
//get value(s)
System.out.println("float value: " + obj2.a);
System.out.println("string value: " + obj2.myString);
NOTICE: public
variables aren't required, they can also be private
or protected
instead. But to avoid getters & setters here, we have accessed them directly in this example.
Protocol Header
Before adding the payload to buffer, there is adding a header with these fields:
- maybe: 4x byte (integer) length of message (so it can check, if full message was received or we have to wait for other traffic)
- 1x byte type
- 1x byte extended byte (so you can use 65,536 different types, instead of 256 bytes)
- 2x byte version (to check compatibility)
- after that: payload data
Supported datatypes
All primitive datatypes in Java are supported:
- byte (
@SByte
) - short (
@SShort
) - int (
@SInteger
) - long (
@SLong
) - float (
@SFloat
) - double (
@SDouble
) - boolean (
@SBoolean
) - char (
@SChar
) - Vertx. Buffer (
@SBuffer
) - byte array (max 4.294.967.296 bytes in an array,
@SBytes
) - json object & json array (
@SJsonObject
and@SJsonArray
)
Complex datatypes (objects) are not supported!
Run Sonarcloud
clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=jukusoft -Dsonar.login=<Sonar-Token>
Support
If you have questions, found a bug or have a feature request:
Please open an issue! I try to answer as fast as possible.