/
SimpleObjectExample.java
60 lines (49 loc) · 1.5 KB
/
SimpleObjectExample.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import io.activej.serializer.BinarySerializer;
import io.activej.serializer.SerializerBuilder;
import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
/**
* Example of serialization and deserialization of a simple object with no {@code null}
* fields, generics or complex objects (such as maps or arrays) as fields.
*/
public final class SimpleObjectExample {
public static void main(String[] args) {
//[START REGION_2]
Person john = new Person(34, "Jim");
john.setSurname("Smith");
byte[] buffer = new byte[200];
BinarySerializer<Person> serializer = SerializerBuilder.create()
.build(Person.class);
//[END REGION_2]
//[START REGION_3]
serializer.encode(buffer, 0, john);
Person johnCopy = serializer.decode(buffer, 0);
//[END REGION_3]
//[START REGION_4]
System.out.println(john.age + " " + johnCopy.age);
System.out.println(john.name + " " + johnCopy.name);
System.out.println(john.getSurname() + " " + johnCopy.getSurname());
//[END REGION_4]
}
//[START REGION_1]
public static class Person {
public Person(@Deserialize("age") int age,
@Deserialize("name") String name) {
this.age = age;
this.name = name;
}
@Serialize(order = 0)
public final int age;
@Serialize(order = 1)
public final String name;
private String surname;
@Serialize(order = 2)
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
//[END REGION_1]
}