@@ -23,14 +23,19 @@ static class NullContentAsEmpty<T> {
23
23
public T values ;
24
24
}
25
25
26
+ static class NullContentSkip <T > {
27
+ @ JsonSetter (contentNulls =JsonSetter .Nulls .SKIP )
28
+ public T values ;
29
+ }
30
+
31
+ private final ObjectMapper MAPPER = new ObjectMapper ();
32
+
26
33
/*
27
34
/**********************************************************
28
- /* Test methods
35
+ /* Test methods, fail-on-null
29
36
/**********************************************************
30
37
*/
31
38
32
- private final ObjectMapper MAPPER = new ObjectMapper ();
33
-
34
39
public void testFailOnNull () throws Exception
35
40
{
36
41
TypeReference <?> typeRef = new TypeReference <NullContentFail <List <Integer >>>() { };
@@ -44,7 +49,7 @@ public void testFailOnNull() throws Exception
44
49
45
50
// and then see that nulls are not ok for non-nullable.
46
51
47
- // First: List<Integer>
52
+ // List<Integer>
48
53
final String JSON = aposToQuotes ("{'noNulls':[null]}" );
49
54
try {
50
55
MAPPER .readValue (JSON , typeRef );
@@ -53,30 +58,59 @@ public void testFailOnNull() throws Exception
53
58
verifyException (e , "property \" noNulls\" " );
54
59
}
55
60
56
- // Then: List<String>
61
+ // List<String>
57
62
try {
58
63
MAPPER .readValue (JSON , new TypeReference <NullContentFail <List <String >>>() { });
59
64
fail ("Should not pass" );
60
65
} catch (InvalidNullException e ) {
61
66
verifyException (e , "property \" noNulls\" " );
62
67
}
68
+ }
63
69
64
- // Then: Object[]
70
+ public void testFailOnNullWithArrays () throws Exception
71
+ {
72
+ final String JSON = aposToQuotes ("{'noNulls':[null]}" );
73
+ // Object[]
65
74
try {
66
75
MAPPER .readValue (JSON , new TypeReference <NullContentFail <Object []>>() { });
67
76
fail ("Should not pass" );
68
77
} catch (InvalidNullException e ) {
69
78
verifyException (e , "property \" noNulls\" " );
70
79
}
71
80
72
- // Then: String[]
81
+ // String[]
73
82
try {
74
83
MAPPER .readValue (JSON , new TypeReference <NullContentFail <String []>>() { });
75
84
fail ("Should not pass" );
76
85
} catch (InvalidNullException e ) {
77
86
verifyException (e , "property \" noNulls\" " );
78
87
}
88
+ }
79
89
90
+ /*
91
+ public void testFailOnNullWithPrimitiveArrays() throws Exception
92
+ {
93
+ final String JSON = aposToQuotes("{'noNulls':[null]}");
94
+ // boolean[]
95
+ try {
96
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<boolean[]>>() { });
97
+ fail("Should not pass");
98
+ } catch (InvalidNullException e) {
99
+ verifyException(e, "property \"noNulls\"");
100
+ }
101
+
102
+ // int[]
103
+ try {
104
+ MAPPER.readValue(JSON, new TypeReference<NullContentFail<int[]>>() { });
105
+ fail("Should not pass");
106
+ } catch (InvalidNullException e) {
107
+ verifyException(e, "property \"noNulls\"");
108
+ }
109
+ }
110
+ */
111
+
112
+ public void testFailOnNullWithMaps () throws Exception
113
+ {
80
114
// Then: Map<String,String>
81
115
try {
82
116
final String MAP_JSON = aposToQuotes ("{'noNulls':{'a':null}}" );
@@ -96,6 +130,12 @@ public void testFailOnNull() throws Exception
96
130
}
97
131
}
98
132
133
+ /*
134
+ /**********************************************************
135
+ /* Test methods, null-as-empty
136
+ /**********************************************************
137
+ */
138
+
99
139
public void testNullsAsEmpty () throws Exception
100
140
{
101
141
final String JSON = aposToQuotes ("{'values':[null]}" );
@@ -115,8 +155,12 @@ public void testNullsAsEmpty() throws Exception
115
155
assertEquals (1 , result .values .size ());
116
156
assertEquals ("" , result .values .get (0 ));
117
157
}
158
+ }
118
159
160
+ public void testNullsAsEmptyWithArrays () throws Exception
161
+ {
119
162
// Note: skip `Object[]`, no default empty value at this point
163
+ final String JSON = aposToQuotes ("{'values':[null]}" );
120
164
121
165
// Then: String[]
122
166
{
@@ -125,7 +169,39 @@ public void testNullsAsEmpty() throws Exception
125
169
assertEquals (1 , result .values .length );
126
170
assertEquals ("" , result .values [0 ]);
127
171
}
172
+ }
173
+
174
+ public void testNullsAsEmptyWithPrimitiveArrays () throws Exception
175
+ {
176
+ final String JSON = aposToQuotes ("{'values':[null]}" );
177
+
178
+ // int[]
179
+ {
180
+ NullContentAsEmpty <int []> result = MAPPER .readValue (JSON ,
181
+ new TypeReference <NullContentAsEmpty <int []>>() { });
182
+ assertEquals (1 , result .values .length );
183
+ assertEquals (0 , result .values [0 ]);
184
+ }
185
+
186
+ // long[]
187
+ {
188
+ NullContentAsEmpty <long []> result = MAPPER .readValue (JSON ,
189
+ new TypeReference <NullContentAsEmpty <long []>>() { });
190
+ assertEquals (1 , result .values .length );
191
+ assertEquals (0L , result .values [0 ]);
192
+ }
128
193
194
+ // boolean[]
195
+ {
196
+ NullContentAsEmpty <boolean []> result = MAPPER .readValue (JSON ,
197
+ new TypeReference <NullContentAsEmpty <boolean []>>() { });
198
+ assertEquals (1 , result .values .length );
199
+ assertEquals (false , result .values [0 ]);
200
+ }
201
+ }
202
+
203
+ public void testNullsAsEmptyWithMaps () throws Exception
204
+ {
129
205
// Then: Map<String,String>
130
206
final String MAP_JSON = aposToQuotes ("{'values':{'A':null}}" );
131
207
{
@@ -145,4 +221,111 @@ public void testNullsAsEmpty() throws Exception
145
221
assertEquals ("" , result .values .entrySet ().iterator ().next ().getValue ());
146
222
}
147
223
}
224
+
225
+ /*
226
+ /**********************************************************
227
+ /* Test methods, skip-nulls
228
+ /**********************************************************
229
+ */
230
+
231
+ /*
232
+ public void testNullsSkip() throws Exception
233
+ {
234
+ // List<Integer>
235
+ {
236
+ final String JSON = aposToQuotes("{'values':[1,null,2]}");
237
+ NullContentSkip<List<Integer>> result = MAPPER.readValue(JSON,
238
+ new TypeReference<NullContentSkip<List<Integer>>>() { });
239
+ assertEquals(2, result.values.size());
240
+ assertEquals(Integer.valueOf(1), result.values.get(0));
241
+ assertEquals(Integer.valueOf(2), result.values.get(1));
242
+ }
243
+
244
+ // List<String>
245
+ {
246
+ final String JSON = aposToQuotes("{'values':['ab',null,'xy']}");
247
+ NullContentSkip<List<String>> result = MAPPER.readValue(JSON,
248
+ new TypeReference<NullContentSkip<List<String>>>() { });
249
+ assertEquals(2, result.values.size());
250
+ assertEquals("ab", result.values.get(0));
251
+ assertEquals("xy", result.values.get(1));
252
+ }
253
+ }
254
+
255
+ public void testNullsSkipWithArrays() throws Exception
256
+ {
257
+ final String JSON = aposToQuotes("{'values':['a',null,'xy']}");
258
+ // Object[]
259
+ {
260
+ NullContentSkip<Object[]> result = MAPPER.readValue(JSON,
261
+ new TypeReference<NullContentSkip<Object[]>>() { });
262
+ assertEquals(2, result.values.length);
263
+ assertEquals("a", result.values[0]);
264
+ assertEquals("xy", result.values[1]);
265
+ }
266
+ // String[]
267
+ {
268
+ NullContentSkip<String[]> result = MAPPER.readValue(JSON,
269
+ new TypeReference<NullContentSkip<String[]>>() { });
270
+ assertEquals(2, result.values.length);
271
+ assertEquals("a", result.values[0]);
272
+ assertEquals("xy", result.values[1]);
273
+ }
274
+ }
275
+
276
+ public void testNullsSkipWithPrimitiveArrays() throws Exception
277
+ {
278
+ // int[]
279
+ {
280
+ final String JSON = aposToQuotes("{'values':[3,null,7]}");
281
+ NullContentSkip<int[]> result = MAPPER.readValue(JSON,
282
+ new TypeReference<NullContentSkip<int[]>>() { });
283
+ assertEquals(2, result.values.length);
284
+ assertEquals(3, result.values[0]);
285
+ assertEquals(7, result.values[1]);
286
+ }
287
+
288
+ // long[]
289
+ {
290
+ final String JSON = aposToQuotes("{'values':[-13,null,999]}");
291
+ NullContentSkip<long[]> result = MAPPER.readValue(JSON,
292
+ new TypeReference<NullContentSkip<long[]>>() { });
293
+ assertEquals(2, result.values.length);
294
+ assertEquals(-13L, result.values[0]);
295
+ assertEquals(-999L, result.values[0]);
296
+ }
297
+
298
+ // boolean[]
299
+ {
300
+ final String JSON = aposToQuotes("{'values':[true,null,true]}");
301
+ NullContentSkip<boolean[]> result = MAPPER.readValue(JSON,
302
+ new TypeReference<NullContentSkip<boolean[]>>() { });
303
+ assertEquals(2, result.values.length);
304
+ assertEquals(true, result.values[0]);
305
+ assertEquals(true, result.values[1]);
306
+ }
307
+ }
308
+
309
+ public void testNullsSkipWithMaps() throws Exception
310
+ {
311
+ // Then: Map<String,String>
312
+ final String MAP_JSON = aposToQuotes("{'values':{'A':'foo','B':null,'C':'bar'}}");
313
+ {
314
+ NullContentSkip<Map<String,String>> result
315
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentSkip<Map<String,String>>>() { });
316
+ assertEquals(2, result.values.size());
317
+ assertEquals("foo", result.values.get("A"));
318
+ assertEquals("bar", result.values.get("C"));
319
+ }
320
+
321
+ // Then: EnumMap<Enum,String>
322
+ {
323
+ NullContentSkip<EnumMap<ABC,String>> result
324
+ = MAPPER.readValue(MAP_JSON, new TypeReference<NullContentSkip<EnumMap<ABC,String>>>() { });
325
+ assertEquals(2, result.values.size());
326
+ assertEquals("foo", result.values.get(ABC.A));
327
+ assertEquals("bar", result.values.get(ABC.C));
328
+ }
329
+ }
330
+ */
148
331
}
0 commit comments