|
1 | | -# serialization / deserialization |
2 | | -## JavaBeans |
3 | | -The driver can serialize/deserialize JavaBeans. They need at least a constructor without parameter. |
4 | | - |
5 | | -``` Java |
6 | | - public class MyObject { |
7 | | - |
8 | | - private String name; |
9 | | - private Gender gender; |
10 | | - private int age; |
11 | | - |
12 | | - public MyObject() { |
13 | | - super(); |
14 | | - } |
15 | | - |
16 | | - } |
17 | | -``` |
18 | | - |
19 | | -## internal fields |
20 | | -To use Arango-internal fields (like _id, _key, _rev, _from, _to) in your JavaBeans, use the annotation `DocumentField`. |
21 | | - |
22 | | -``` Java |
23 | | - public class MyObject { |
24 | | - |
25 | | - @DocumentField(Type.KEY) |
26 | | - private String key; |
27 | | - |
28 | | - private String name; |
29 | | - private Gender gender; |
30 | | - private int age; |
31 | | - |
32 | | - public MyObject() { |
33 | | - super(); |
34 | | - } |
35 | | - |
36 | | - } |
37 | | -``` |
38 | | - |
39 | | -## serialized fieldnames |
40 | | -To use a different serialized name for a field, use the annotation `SerializedName`. |
41 | | - |
42 | | -``` Java |
43 | | - public class MyObject { |
44 | | - |
45 | | - @SerializedName("title") |
46 | | - private String name; |
47 | | - |
48 | | - private Gender gender; |
49 | | - private int age; |
50 | | - |
51 | | - public MyObject() { |
52 | | - super(); |
53 | | - } |
54 | | - |
55 | | - } |
56 | | -``` |
57 | | - |
58 | | -## ignore fields |
59 | | -To ignore fields at serialization/deserialization, use the annotation `Expose` |
60 | | - |
61 | | -``` Java |
62 | | - public class MyObject { |
63 | | - |
64 | | - @Expose |
65 | | - private String name; |
66 | | - @Expose(serialize = true, deserialize = false) |
67 | | - private Gender gender; |
68 | | - private int age; |
69 | | - |
70 | | - public MyObject() { |
71 | | - super(); |
72 | | - } |
73 | | - |
74 | | - } |
75 | | -``` |
76 | | - |
77 | | -## custom de-/serializer |
78 | | -``` Java |
79 | | -final ArangoDB arangoDB = new ArangoDB.Builder() |
80 | | - .registerDeserializer(MyObject.class, new VPackDeserializer<MyObject>() { |
81 | | - @Override |
82 | | - public MyObject deserialize( |
83 | | - final VPackSlice parent, |
84 | | - final VPackSlice vpack, |
85 | | - final VPackDeserializationContext context) throws VPackException { |
86 | | - |
87 | | - final MyObject obj = new MyObject(); |
88 | | - obj.setName(vpack.get("name").getAsString()); |
89 | | - return obj; |
90 | | - } |
91 | | - }).registerSerializer(MyObject.class, new VPackSerializer<MyObject>() { |
92 | | - @Override |
93 | | - public void serialize( |
94 | | - final VPackBuilder builder, |
95 | | - final String attribute, |
96 | | - final MyObject value, |
97 | | - final VPackSerializationContext context) throws VPackException { |
98 | | - |
99 | | - builder.add(attribute, ValueType.OBJECT); |
100 | | - builder.add("name", value.getName()); |
101 | | - builder.close(); |
102 | | - } |
103 | | - }).build(); |
104 | | -``` |
| 1 | +# serialization / deserialization |
| 2 | +## JavaBeans |
| 3 | +The driver can serialize/deserialize JavaBeans. They need at least a constructor without parameter. |
| 4 | + |
| 5 | +``` Java |
| 6 | + public class MyObject { |
| 7 | + |
| 8 | + private String name; |
| 9 | + private Gender gender; |
| 10 | + private int age; |
| 11 | + |
| 12 | + public MyObject() { |
| 13 | + super(); |
| 14 | + } |
| 15 | + |
| 16 | + } |
| 17 | +``` |
| 18 | + |
| 19 | +## internal fields |
| 20 | +To use Arango-internal fields (like _id, _key, _rev, _from, _to) in your JavaBeans, use the annotation `DocumentField`. |
| 21 | + |
| 22 | +``` Java |
| 23 | + public class MyObject { |
| 24 | + |
| 25 | + @DocumentField(Type.KEY) |
| 26 | + private String key; |
| 27 | + |
| 28 | + private String name; |
| 29 | + private Gender gender; |
| 30 | + private int age; |
| 31 | + |
| 32 | + public MyObject() { |
| 33 | + super(); |
| 34 | + } |
| 35 | + |
| 36 | + } |
| 37 | +``` |
| 38 | + |
| 39 | +## serialized fieldnames |
| 40 | +To use a different serialized name for a field, use the annotation `SerializedName`. |
| 41 | + |
| 42 | +``` Java |
| 43 | + public class MyObject { |
| 44 | + |
| 45 | + @SerializedName("title") |
| 46 | + private String name; |
| 47 | + |
| 48 | + private Gender gender; |
| 49 | + private int age; |
| 50 | + |
| 51 | + public MyObject() { |
| 52 | + super(); |
| 53 | + } |
| 54 | + |
| 55 | + } |
| 56 | +``` |
| 57 | + |
| 58 | +## ignore fields |
| 59 | +To ignore fields at serialization/deserialization, use the annotation `Expose` |
| 60 | + |
| 61 | +``` Java |
| 62 | + public class MyObject { |
| 63 | + |
| 64 | + @Expose |
| 65 | + private String name; |
| 66 | + @Expose(serialize = true, deserialize = false) |
| 67 | + private Gender gender; |
| 68 | + private int age; |
| 69 | + |
| 70 | + public MyObject() { |
| 71 | + super(); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | +``` |
| 76 | + |
| 77 | +## custom de-/serializer |
| 78 | +``` Java |
| 79 | + ArangoDB arangoDB = new ArangoDB.Builder() |
| 80 | + .registerDeserializer(MyObject.class, new VPackDeserializer<MyObject>() { |
| 81 | + @Override |
| 82 | + public MyObject deserialize( |
| 83 | + final VPackSlice parent, |
| 84 | + final VPackSlice vpack, |
| 85 | + final VPackDeserializationContext context) throws VPackException { |
| 86 | + |
| 87 | + final MyObject obj = new MyObject(); |
| 88 | + obj.setName(vpack.get("name").getAsString()); |
| 89 | + return obj; |
| 90 | + } |
| 91 | + }).registerSerializer(MyObject.class, new VPackSerializer<MyObject>() { |
| 92 | + @Override |
| 93 | + public void serialize( |
| 94 | + final VPackBuilder builder, |
| 95 | + final String attribute, |
| 96 | + final MyObject value, |
| 97 | + final VPackSerializationContext context) throws VPackException { |
| 98 | + |
| 99 | + builder.add(attribute, ValueType.OBJECT); |
| 100 | + builder.add("name", value.getName()); |
| 101 | + builder.close(); |
| 102 | + } |
| 103 | + }).build(); |
| 104 | +``` |
| 105 | + |
| 106 | +## manually de-/serialization |
| 107 | +To de-/serialize from and to VelocyPack before or after a database call, use the `ArangoUtil` from the method `util()` in `ArangoDB`, `ArangoDatabase`, `ArangoCollection`, `ArangoGraph`, `ArangoEdgeCollection`or `ArangoVertexCollection`. |
| 108 | + |
| 109 | +``` Java |
| 110 | + ArangoDB arangoDB = new ArangoDB.Builder(); |
| 111 | + arangoDB.util().deserialize(vpack, type); |
| 112 | +``` |
0 commit comments