2727import com .arangodb .entity .BaseDocument ;
2828import com .arangodb .mapping .ArangoJack ;
2929import 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 ;
3041import org .junit .AfterClass ;
3142import org .junit .BeforeClass ;
3243import org .junit .Test ;
3344
45+ import java .io .IOException ;
3446import java .math .BigInteger ;
3547import java .util .Collections ;
3648import java .util .HashMap ;
3749import java .util .UUID ;
3850
51+ import static com .arangodb .internal .util .ArangoSerializationFactory .Serializer .CUSTOM ;
3952import static com .fasterxml .jackson .databind .DeserializationFeature .USE_BIG_INTEGER_FOR_INTS ;
4053import static com .fasterxml .jackson .databind .SerializationFeature .WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED ;
4154import static org .hamcrest .MatcherAssert .assertThat ;
4962public class CustomSerdeTest {
5063
5164 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" ;
5267
68+ private static ArangoDB arangoDB ;
5369 private static ArangoDatabase db ;
5470 private static ArangoCollection collection ;
5571
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+
56100 @ BeforeClass
57101 public static void init () {
58102 ArangoJack arangoJack = new ArangoJack ();
59103 arangoJack .configure ((mapper ) -> {
60104 mapper .configure (WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED , true );
61105 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 );
62109 });
63- ArangoDB arangoDB = new ArangoDB .Builder ().serializer (arangoJack ).build ();
110+ arangoDB = new ArangoDB .Builder ().serializer (arangoJack ).build ();
64111
65112 String TEST_DB = "custom-serde-test" ;
66113 db = arangoDB .db (TEST_DB );
@@ -80,6 +127,27 @@ public static void shutdown() {
80127 db .drop ();
81128 }
82129
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+
83151 @ Test
84152 public void aqlSerialization () {
85153 String key = "test-" + UUID .randomUUID ().toString ();
0 commit comments