https://github.com/juku/vertx-binary-serialization
A simple binary serialization method for vertx which uses annotations & reflection like spring jpa / hibernate for databases.
https://github.com/juku/vertx-binary-serialization
java java10 java8 network networking serialization vertx
Last synced: 21 days ago
JSON representation
A simple binary serialization method for vertx which uses annotations & reflection like spring jpa / hibernate for databases.
- Host: GitHub
- URL: https://github.com/juku/vertx-binary-serialization
- Owner: JuKu
- License: mit
- Created: 2018-09-26T13:20:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-22T17:51:07.000Z (about 7 years ago)
- Last Synced: 2025-02-24T11:14:00.088Z (12 months ago)
- Topics: java, java10, java8, network, networking, serialization, vertx
- Language: Java
- Homepage:
- Size: 219 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vertx-binary-serialization
A simple binary serialization method for vertx which uses annotations & reflection like spring jpa for databases.
[](https://travis-ci.org/JuKu/vertx-binary-serialization)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard/index/com.jukusoft%3Avertx-binary-serializer-parent)
[](https://sonarcloud.io/dashboard?id=com.jukusoft%3Avertx-binary-serializer-parent)
## Requirements
- Java 8+
- [vert.x](http://vertx.io)
## Maven Coordinates
[](https://search.maven.org/search?q=g:%22com.jukusoft%22%20AND%20a:%22vertx-binary-serializer%22)
```xml
com.jukusoft
vertx-binary-serializer
1.0.8
com.jukusoft
vertx-binary-serializer-connection
1.0.8
```
## HowTo
First create some message objects which contains some datatypes:
```java
@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:
```java
//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
```bash
clean org.jacoco:jacoco-maven-plugin:prepare-agent package sonar:sonar -Dsonar.host.url=https://sonarcloud.io -Dsonar.organization=jukusoft -Dsonar.login=
```
## Support
If you have questions, found a bug or have a feature request:\
Please **open an [issue](https://github.com/JuKu/vertx-binary-serialization/issues)**! I try to answer as fast as possible.