27
27
import com .arangodb .entity .BaseDocument ;
28
28
import com .arangodb .mapping .ArangoJack ;
29
29
import com .arangodb .model .DocumentCreateOptions ;
30
+ import com .arangodb .util .ArangoSerialization ;
31
+ import com .arangodb .velocypack .VPackSlice ;
32
+ import com .fasterxml .jackson .core .JsonGenerator ;
33
+ import com .fasterxml .jackson .core .JsonParser ;
34
+ import com .fasterxml .jackson .databind .DeserializationContext ;
35
+ import com .fasterxml .jackson .databind .JsonDeserializer ;
36
+ import com .fasterxml .jackson .databind .JsonNode ;
37
+ import com .fasterxml .jackson .databind .JsonSerializer ;
38
+ import com .fasterxml .jackson .databind .SerializerProvider ;
39
+ import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
40
+ import com .fasterxml .jackson .databind .module .SimpleModule ;
30
41
import org .junit .AfterClass ;
31
42
import org .junit .BeforeClass ;
32
43
import org .junit .Test ;
33
44
45
+ import java .io .IOException ;
34
46
import java .math .BigInteger ;
35
47
import java .util .Collections ;
36
48
import java .util .HashMap ;
37
49
import java .util .UUID ;
38
50
51
+ import static com .arangodb .internal .util .ArangoSerializationFactory .Serializer .CUSTOM ;
39
52
import static com .fasterxml .jackson .databind .DeserializationFeature .USE_BIG_INTEGER_FOR_INTS ;
40
53
import static com .fasterxml .jackson .databind .SerializationFeature .WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED ;
41
54
import static org .hamcrest .MatcherAssert .assertThat ;
49
62
public class CustomSerdeTest {
50
63
51
64
private static final String COLLECTION_NAME = "collection" ;
65
+ private static final String PERSON_SERIALIZER_ADDED_PREFIX = "MyNameIs" ;
66
+ private static final String PERSON_DESERIALIZER_ADDED_PREFIX = "Hello" ;
52
67
68
+ private static ArangoDB arangoDB ;
53
69
private static ArangoDatabase db ;
54
70
private static ArangoCollection collection ;
55
71
72
+ static class PersonSerializer extends JsonSerializer <Person > {
73
+ @ Override
74
+ public void serialize (Person value , JsonGenerator gen , SerializerProvider serializers ) throws IOException {
75
+ gen .writeStartObject ();
76
+ gen .writeFieldName ("name" );
77
+ gen .writeString (PERSON_SERIALIZER_ADDED_PREFIX + value .name );
78
+ gen .writeEndObject ();
79
+ }
80
+ }
81
+
82
+ static class PersonDeserializer extends JsonDeserializer <Person > {
83
+ @ Override
84
+ public Person deserialize (JsonParser parser , DeserializationContext ctxt ) throws IOException {
85
+ Person person = new Person ();
86
+ JsonNode rootNode = parser .getCodec ().readTree (parser );
87
+ JsonNode nameNode = rootNode .get ("name" );
88
+ if (nameNode != null && nameNode .isTextual ()) {
89
+ person .name = PERSON_DESERIALIZER_ADDED_PREFIX + nameNode .asText ();
90
+ }
91
+ return person ;
92
+ }
93
+ }
94
+
95
+ @ JsonSerialize (using = PersonSerializer .class )
96
+ public static class Person {
97
+ public String name ;
98
+ }
99
+
56
100
@ BeforeClass
57
101
public static void init () {
58
102
ArangoJack arangoJack = new ArangoJack ();
59
103
arangoJack .configure ((mapper ) -> {
60
104
mapper .configure (WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED , true );
61
105
mapper .configure (USE_BIG_INTEGER_FOR_INTS , true );
106
+ SimpleModule module = new SimpleModule ("PersonModule" );
107
+ module .addDeserializer (Person .class , new PersonDeserializer ());
108
+ mapper .registerModule (module );
62
109
});
63
- ArangoDB arangoDB = new ArangoDB .Builder ().serializer (arangoJack ).build ();
110
+ arangoDB = new ArangoDB .Builder ().serializer (arangoJack ).build ();
64
111
65
112
String TEST_DB = "custom-serde-test" ;
66
113
db = arangoDB .db (TEST_DB );
@@ -80,6 +127,27 @@ public static void shutdown() {
80
127
db .drop ();
81
128
}
82
129
130
+ @ Test
131
+ public void customPersonDeserializer () {
132
+ Person person = new Person ();
133
+ person .name = "Joe" ;
134
+ Person result = collection .insertDocument (
135
+ person ,
136
+ new DocumentCreateOptions ().returnNew (true )
137
+ ).getNew ();
138
+ assertThat (result .name , is (PERSON_DESERIALIZER_ADDED_PREFIX + PERSON_SERIALIZER_ADDED_PREFIX + person .name ));
139
+ }
140
+
141
+ @ Test
142
+ public void manualCustomPersonDeserializer () {
143
+ Person person = new Person ();
144
+ person .name = "Joe" ;
145
+ ArangoSerialization serialization = arangoDB .util (CUSTOM );
146
+ VPackSlice serializedPerson = serialization .serialize (person );
147
+ Person deserializedPerson = serialization .deserialize (serializedPerson , Person .class );
148
+ assertThat (deserializedPerson .name , is (PERSON_DESERIALIZER_ADDED_PREFIX + PERSON_SERIALIZER_ADDED_PREFIX + person .name ));
149
+ }
150
+
83
151
@ Test
84
152
public void aqlSerialization () {
85
153
String key = "test-" + UUID .randomUUID ().toString ();
0 commit comments