File tree 4 files changed +80
-7
lines changed
driver/src/test/java/com/arangodb/serde
jackson-serde-json/src/main/java/com/arangodb/serde/jackson
4 files changed +80
-7
lines changed Original file line number Diff line number Diff line change
1
+ package com .arangodb .serde ;
2
+
3
+ import com .arangodb .serde .jackson .Id ;
4
+ import com .arangodb .serde .jackson .Key ;
5
+ import com .fasterxml .jackson .databind .ObjectMapper ;
6
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
7
+ import org .junit .jupiter .api .Test ;
8
+
9
+ import static org .assertj .core .api .Assertions .assertThat ;
10
+
11
+ class JacksonInterferenceTest {
12
+
13
+ private final ObjectMapper mapper = new ObjectMapper ();
14
+
15
+ static class Foo {
16
+ @ Id
17
+ public String myId ;
18
+
19
+ @ Key
20
+ public String myKey ;
21
+
22
+ Foo (String id , String key ) {
23
+ myId = id ;
24
+ myKey = key ;
25
+ }
26
+ }
27
+
28
+ @ Test
29
+ void serialize () {
30
+ Foo foo = new Foo ("foo" , "bar" );
31
+ ObjectNode node = mapper .convertValue (foo , ObjectNode .class );
32
+ // assertThat(node.get("myId").textValue()).isEqualTo("foo");
33
+ assertThat (node .get ("myKey" ).textValue ()).isEqualTo ("bar" );
34
+ }
35
+ }
Original file line number Diff line number Diff line change 1
1
package com .arangodb .serde .jackson ;
2
2
3
- import com .fasterxml .jackson .annotation .JacksonAnnotationsInside ;
4
- import com .fasterxml .jackson .annotation .JsonInclude ;
5
- import com .fasterxml .jackson .annotation .JsonProperty ;
6
-
7
3
import java .lang .annotation .ElementType ;
8
4
import java .lang .annotation .Retention ;
9
5
import java .lang .annotation .RetentionPolicy ;
14
10
*/
15
11
@ Target ({ElementType .FIELD , ElementType .METHOD , ElementType .PARAMETER })
16
12
@ Retention (RetentionPolicy .RUNTIME )
17
- @ JacksonAnnotationsInside
18
- @ JsonProperty ("_key" )
19
- @ JsonInclude (JsonInclude .Include .NON_NULL )
20
13
public @interface Key {
21
14
}
Original file line number Diff line number Diff line change
1
+ package com .arangodb .serde .jackson .internal ;
2
+
3
+ import com .arangodb .serde .jackson .Key ;
4
+ import com .fasterxml .jackson .annotation .JsonInclude ;
5
+ import com .fasterxml .jackson .databind .PropertyName ;
6
+ import com .fasterxml .jackson .databind .introspect .Annotated ;
7
+ import com .fasterxml .jackson .databind .introspect .JacksonAnnotationIntrospector ;
8
+
9
+ class ArangoSerdeAnnotationIntrospector extends JacksonAnnotationIntrospector {
10
+ private static final JsonInclude JSON_INCLUDE_NON_NULL = JsonIncludeNonNull .class .getAnnotation (JsonInclude .class );
11
+
12
+ @ JsonInclude (JsonInclude .Include .NON_NULL )
13
+ private static class JsonIncludeNonNull {
14
+ }
15
+
16
+ @ Override
17
+ public PropertyName findNameForSerialization (Annotated a ) {
18
+ Key kann = _findAnnotation (a , Key .class );
19
+ if (kann != null ) {
20
+ return PropertyName .construct ("_key" );
21
+ }
22
+ return super .findNameForSerialization (a );
23
+ }
24
+
25
+ @ Override
26
+ public PropertyName findNameForDeserialization (Annotated a ) {
27
+ Key kann = _findAnnotation (a , Key .class );
28
+ if (kann != null ) {
29
+ return PropertyName .construct ("_key" );
30
+ } else {
31
+ return super .findNameForDeserialization (a );
32
+ }
33
+ }
34
+
35
+ @ Override
36
+ public JsonInclude .Value findPropertyInclusion (Annotated a ) {
37
+ Key kann = _findAnnotation (a , Key .class );
38
+ if (kann != null ) {
39
+ return new JsonInclude .Value (JSON_INCLUDE_NON_NULL );
40
+ } else {
41
+ return super .findPropertyInclusion (a );
42
+ }
43
+ }
44
+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ public final class JacksonSerdeImpl implements JacksonSerde {
18
18
public JacksonSerdeImpl (final ObjectMapper mapper ) {
19
19
this .mapper = mapper ;
20
20
mapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
21
+ mapper .setAnnotationIntrospector (new ArangoSerdeAnnotationIntrospector ());
21
22
}
22
23
23
24
@ Override
You can’t perform that action at this time.
0 commit comments