1 |
|
---|
2 | import static org.junit.Assert.assertEquals;
|
---|
3 |
|
---|
4 | import org.junit.Test;
|
---|
5 |
|
---|
6 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
7 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
10 | import com.fasterxml.jackson.annotation.JsonSubTypes;
|
---|
11 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
13 | import com.fasterxml.jackson.core.JsonProcessingException;
|
---|
14 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * There are serious issues with some jackson versions. We need to check what we
|
---|
18 | * are up to to ensure our code works properly.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class JacksonTest {
|
---|
22 | final ObjectMapper jackson = new ObjectMapper();
|
---|
23 | final N n = new N();
|
---|
24 |
|
---|
25 | @Test
|
---|
26 | public void testN() throws JsonProcessingException {
|
---|
27 | doTest(new N());
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Test
|
---|
31 | public void testYN() throws JsonProcessingException {
|
---|
32 | doTest(new Y(n));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Test
|
---|
36 | public void testYYN() throws JsonProcessingException {
|
---|
37 | doTest(new Y(new Y(n)));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void testZYN() throws JsonProcessingException {
|
---|
42 | doTest(new Z(new Y(n)));
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Test
|
---|
46 | public void testZN() throws JsonProcessingException {
|
---|
47 | doTest(new Z(n));
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void doTest(X val) throws JsonProcessingException {
|
---|
51 | System.out.println("testing " + val);
|
---|
52 | String str = jackson.writeValueAsString(val);
|
---|
53 | System.out.println("To json: " + str);
|
---|
54 | X obj = jackson.readValue(str, X.class);
|
---|
55 | System.out.println("reparsed: " + obj);
|
---|
56 |
|
---|
57 | assertEquals(val.toString(), obj.toString());
|
---|
58 |
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
|
---|
63 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
64 | @JsonSubTypes({ @JsonSubTypes.Type(value = Y.class),
|
---|
65 | @JsonSubTypes.Type(value = Z.class),
|
---|
66 | @JsonSubTypes.Type(value = N.class) })
|
---|
67 | interface X {
|
---|
68 | }
|
---|
69 |
|
---|
70 | class N implements X {
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public String toString() {
|
---|
74 | return "N";
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | class Y implements X {
|
---|
80 |
|
---|
81 | // NOT @JsonValue - works
|
---|
82 | private final X value;
|
---|
83 |
|
---|
84 | @JsonCreator
|
---|
85 | public Y(@JsonProperty("value") X val) {
|
---|
86 | this.value = val;
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public String toString() {
|
---|
91 | return "Y(" + value + ")";
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | class Z implements X {
|
---|
97 | @JsonValue
|
---|
98 | private final X value;
|
---|
99 |
|
---|
100 | @JsonCreator
|
---|
101 | public Z(X value) {
|
---|
102 | this.value = value;
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public String toString() {
|
---|
107 | return "Z(" + value + ")";
|
---|
108 | }
|
---|
109 |
|
---|
110 | } |
---|