source: dialogmanager/src/test/java/JacksonTest.java@ 484

Last change on this file since 484 was 431, checked in by wouter, 22 months ago

#137 for now don't use @JsonValue.

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