forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPOJOAsArray.java
317 lines (273 loc) · 9.51 KB
/
TestPOJOAsArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package tools.jackson.databind.struct;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import tools.jackson.databind.*;
import tools.jackson.databind.cfg.MapperConfig;
import tools.jackson.databind.exc.MismatchedInputException;
import tools.jackson.databind.introspect.Annotated;
import tools.jackson.databind.introspect.JacksonAnnotationIntrospector;
import tools.jackson.databind.testutil.DatabindTestUtil;
import static org.junit.jupiter.api.Assertions.*;
public class TestPOJOAsArray extends DatabindTestUtil
{
static class PojoAsArrayWrapper
{
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
public PojoAsArray value;
public PojoAsArrayWrapper() { }
public PojoAsArrayWrapper(String name, int x, int y, boolean c) {
value = new PojoAsArray(name, x, y, c);
}
}
@JsonPropertyOrder(alphabetic=true)
static class NonAnnotatedXY {
public int x, y;
public NonAnnotatedXY() { }
public NonAnnotatedXY(int x0, int y0) {
x = x0;
y = y0;
}
}
// note: must be serialized/deserialized alphabetically; fields NOT declared in that order
@JsonPropertyOrder(alphabetic=true)
static class PojoAsArray
{
public int x, y;
public String name;
public boolean complete;
public PojoAsArray() { }
public PojoAsArray(String name, int x, int y, boolean c) {
this.name = name;
this.x = x;
this.y = y;
this.complete = c;
}
}
@JsonPropertyOrder(alphabetic=true)
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
static class FlatPojo
{
public int x, y;
public String name;
public boolean complete;
public FlatPojo() { }
public FlatPojo(String name, int x, int y, boolean c) {
this.name = name;
this.x = x;
this.y = y;
this.complete = c;
}
}
static class ForceArraysIntrospector extends JacksonAnnotationIntrospector
{
private static final long serialVersionUID = 1L;
@Override
public JsonFormat.Value findFormat(MapperConfig<?> config, Annotated a) {
return new JsonFormat.Value().withShape(JsonFormat.Shape.ARRAY);
}
}
static class A {
public B value = new B();
}
@JsonPropertyOrder(alphabetic=true)
static class B {
public int x = 1;
public int y = 2;
}
@JsonFormat(shape=Shape.ARRAY)
static class SingleBean {
public String name = "foo";
}
@JsonPropertyOrder(alphabetic=true)
@JsonFormat(shape=Shape.ARRAY)
static class TwoStringsBean {
public String bar = null;
public String foo = "bar";
}
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
@JsonPropertyOrder(alphabetic=true)
static class AsArrayWithMap
{
public Map<Integer,Integer> attrs;
public AsArrayWithMap() { }
public AsArrayWithMap(int x, int y) {
attrs = new HashMap<Integer,Integer>();
attrs.put(x, y);
}
}
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
static class CreatorWithIndex {
protected int _a, _b;
@JsonCreator
public CreatorWithIndex(@JsonProperty(index=0, value="a") int a,
@JsonProperty(index=1, value="b") int b) {
this._a = a;
this._b = b;
}
}
/*
/*****************************************************
/* Basic tests
/*****************************************************
*/
private final static ObjectMapper MAPPER = newJsonMapper();
/**
* Test that verifies that property annotation works
*/
@Test
public void testReadSimplePropertyValue() throws Exception
{
String json = "{\"value\":[true,\"Foobar\",42,13]}";
PojoAsArrayWrapper p = MAPPER.readValue(json, PojoAsArrayWrapper.class);
assertNotNull(p.value);
assertTrue(p.value.complete);
assertEquals("Foobar", p.value.name);
assertEquals(42, p.value.x);
assertEquals(13, p.value.y);
}
/**
* Test that verifies that Class annotation works
*/
@Test
public void testReadSimpleRootValue() throws Exception
{
String json = "[false,\"Bubba\",1,2]";
FlatPojo p = MAPPER.readValue(json, FlatPojo.class);
assertFalse(p.complete);
assertEquals("Bubba", p.name);
assertEquals(1, p.x);
assertEquals(2, p.y);
}
/**
* Test that verifies that property annotation works
*/
@Test
public void testWriteSimplePropertyValue() throws Exception
{
String json = MAPPER.writeValueAsString(new PojoAsArrayWrapper("Foobar", 42, 13, true));
// will have wrapper POJO, then POJO-as-array..
assertEquals("{\"value\":[true,\"Foobar\",42,13]}", json);
}
/**
* Test that verifies that Class annotation works
*/
@Test
public void testWriteSimpleRootValue() throws Exception
{
String json = MAPPER.writeValueAsString(new FlatPojo("Bubba", 1, 2, false));
// will have wrapper POJO, then POJO-as-array..
assertEquals("[false,\"Bubba\",1,2]", json);
}
// [Issue#223]
@Test
public void testNullColumn() throws Exception
{
assertEquals("[null,\"bar\"]", MAPPER.writeValueAsString(new TwoStringsBean()));
}
/*
/*****************************************************
/* Compatibility with "single-elem as array" feature
/*****************************************************
*/
@Test
public void testSerializeAsArrayWithSingleProperty() throws Exception {
ObjectMapper mapper = jsonMapperBuilder()
.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
.build();
String json = mapper.writeValueAsString(new SingleBean());
assertEquals("\"foo\"", json);
}
@Test
public void testBeanAsArrayUnwrapped() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.build();
SingleBean result = mapper.readValue("[\"foobar\"]", SingleBean.class);
assertNotNull(result);
assertEquals("foobar", result.name);
}
/*
/*****************************************************
/* Round-trip tests
/*****************************************************
*/
@Test
public void testAnnotationOverride() throws Exception
{
// by default, POJOs become JSON Objects;
assertEquals("{\"value\":{\"x\":1,\"y\":2}}", MAPPER.writeValueAsString(new A()));
// but override should change it:
ObjectMapper mapper2 = jsonMapperBuilder()
.annotationIntrospector(new ForceArraysIntrospector())
.build();
assertEquals("[[1,2]]", mapper2.writeValueAsString(new A()));
// and allow reading back, too
}
@Test
public void testWithMaps() throws Exception
{
AsArrayWithMap input = new AsArrayWithMap(1, 2);
String json = MAPPER.writeValueAsString(input);
AsArrayWithMap output = MAPPER.readValue(json, AsArrayWithMap.class);
assertNotNull(output);
assertNotNull(output.attrs);
assertEquals(1, output.attrs.size());
assertEquals(Integer.valueOf(2), output.attrs.get(1));
}
@Test
public void testSimpleWithIndex() throws Exception
{
// as POJO:
// CreatorWithIndex value = MAPPER.readValue(aposToQuotes("{'b':1,'a':2}"),
CreatorWithIndex value = MAPPER.readValue(a2q("[2,1]"),
CreatorWithIndex.class);
assertEquals(2, value._a);
assertEquals(1, value._b);
}
@Test
public void testWithConfigOverrides() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.withConfigOverride(NonAnnotatedXY.class,
o -> o.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.ARRAY)))
.build();
String json = mapper.writeValueAsString(new NonAnnotatedXY(2, 3));
assertEquals("[2,3]", json);
// also, read it back
NonAnnotatedXY result = mapper.readValue(json, NonAnnotatedXY.class);
assertNotNull(result);
assertEquals(3, result.y);
}
/*
/*****************************************************
/* Failure tests
/*****************************************************
*/
@Test
public void testUnknownExtraProp() throws Exception
{
String json = "{\"value\":[true,\"Foobar\",42,13, false]}";
try {
MAPPER.readerFor(PojoAsArrayWrapper.class)
.with(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readValue(json);
fail("should not pass with extra element");
} catch (MismatchedInputException e) {
verifyException(e, "Unexpected JSON values");
}
// but actually fine if skip-unknown set
PojoAsArrayWrapper v = MAPPER.readerFor(PojoAsArrayWrapper.class)
.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readValue(json);
assertNotNull(v);
// note: +1 for both so
assertEquals(v.value.x, 42);
assertEquals(v.value.y, 13);
assertTrue(v.value.complete);
assertEquals("Foobar", v.value.name);
}
}