1 |
|
---|
2 | import static org.junit.Assert.assertEquals;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 |
|
---|
6 | import org.junit.Ignore;
|
---|
7 | import org.junit.Test;
|
---|
8 |
|
---|
9 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
---|
10 | import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
|
---|
11 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
12 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
13 | import com.fasterxml.jackson.annotation.JsonSubTypes;
|
---|
14 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
---|
15 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
16 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * #137 There are serious issues with some jackson versions. We need to check
|
---|
20 | * what we are up to to ensure our code works properly. For the moment the rest
|
---|
21 | * of our code avoid @JsonValue because of this, which causes the json
|
---|
22 | * serialiation to be a bit awkward
|
---|
23 | *
|
---|
24 | */
|
---|
25 | @Ignore
|
---|
26 | public class JacksonTest {
|
---|
27 | final ObjectMapper jackson = new ObjectMapper();
|
---|
28 | final N n = new N();
|
---|
29 |
|
---|
30 | @Test
|
---|
31 | public void testN() throws IOException {
|
---|
32 | doTest(new N());
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Test
|
---|
36 | public void testYN() throws IOException {
|
---|
37 | doTest(new Y(n));
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void testYYN() throws IOException {
|
---|
42 | doTest(new Y(new Y(n)));
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Test
|
---|
46 | public void testZYN() throws IOException {
|
---|
47 | doTest(new Z(new Y(n)));
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Test
|
---|
51 | public void testZN() throws IOException {
|
---|
52 | doTest(new Z(n));
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Test
|
---|
56 | public void testWN() throws IOException {
|
---|
57 | doTest(new W(n));
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void doTest(X val) throws IOException {
|
---|
61 | System.out.println("testing " + val);
|
---|
62 | String str = jackson.writeValueAsString(val);
|
---|
63 | System.out.println("To json: " + str);
|
---|
64 | X obj = jackson.readValue(str, X.class);
|
---|
65 | System.out.println("reparsed: " + obj);
|
---|
66 |
|
---|
67 | assertEquals(val.toString(), obj.toString());
|
---|
68 |
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
|
---|
73 | @JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
|
---|
74 | @JsonSubTypes({ @JsonSubTypes.Type(value = Y.class),
|
---|
75 | @JsonSubTypes.Type(value = Z.class),
|
---|
76 | @JsonSubTypes.Type(value = W.class),
|
---|
77 | @JsonSubTypes.Type(value = N.class) })
|
---|
78 | interface X {
|
---|
79 | }
|
---|
80 |
|
---|
81 | class N implements X {
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public String toString() {
|
---|
85 | return "N";
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | class Y implements X {
|
---|
91 |
|
---|
92 | // NOT @JsonValue - works
|
---|
93 | private final X value;
|
---|
94 |
|
---|
95 | @JsonCreator
|
---|
96 | public Y(@JsonProperty("value") X val) {
|
---|
97 | this.value = val;
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public String toString() {
|
---|
102 | return "Y(" + value + ")";
|
---|
103 | }
|
---|
104 |
|
---|
105 | }
|
---|
106 |
|
---|
107 | class Z implements X {
|
---|
108 | @JsonValue
|
---|
109 | private final X value;
|
---|
110 |
|
---|
111 | @JsonCreator
|
---|
112 | public Z(@JsonProperty("value") X value) {
|
---|
113 | this.value = value;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public String toString() {
|
---|
118 | return "Z(" + value + ")";
|
---|
119 | }
|
---|
120 |
|
---|
121 | }
|
---|
122 |
|
---|
123 | @JsonAutoDetect(fieldVisibility = Visibility.ANY)
|
---|
124 | class W implements X {
|
---|
125 | private final X value;
|
---|
126 |
|
---|
127 | @JsonCreator
|
---|
128 | public W(@JsonProperty("value") X value) {
|
---|
129 | this.value = value;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @JsonValue
|
---|
133 | public X getValue() {
|
---|
134 | return value;
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public String toString() {
|
---|
139 | return "W(" + value + ")";
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|