/
FixedSizeFieldsExample.java
44 lines (37 loc) · 1.39 KB
/
FixedSizeFieldsExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.SerializerBuilder;
import io.activej.serializer.annotations.Serialize;
import io.activej.serializer.annotations.SerializeFixedSize;
import io.activej.serializer.annotations.SerializeNullable;
import java.util.Arrays;
/**
* Example of serialization and deserialization of an object with fixed size and nullable fields.
*/
public final class FixedSizeFieldsExample {
//[START REGION_1]
public static class Storage {
@Serialize
public @SerializeNullable String @SerializeFixedSize(3) [] strings;
@Serialize
public byte @SerializeFixedSize(4) [] bytes;
}
//[END REGION_1]
public static void main(String[] args) {
//[START REGION_2]
Storage storage = new Storage();
storage.strings = new String[]{"abc", null, "123", "superfluous"};
storage.bytes = new byte[]{1, 2, 3, 4, 5, 6};
byte[] buffer = new byte[200];
BinarySerializer<Storage> serializer = SerializerBuilder.create()
.build(Storage.class);
//[END REGION_2]
//[START REGION_3]
serializer.encode(buffer, 0, storage);
Storage limitedStorage = serializer.decode(buffer, 0);
//[END REGION_3]
//[START REGION_4]
System.out.println(Arrays.toString(storage.strings) + " -> " + Arrays.toString(limitedStorage.strings));
System.out.println(Arrays.toString(storage.bytes) + " -> " + Arrays.toString(limitedStorage.bytes));
//[END REGION_4]
}
}