1
1
package com .fasterxml .jackson .module .afterburner .deser ;
2
2
3
+ import java .util .List ;
4
+
3
5
import com .fasterxml .jackson .annotation .JsonProperty ;
4
6
import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
5
-
6
7
import com .fasterxml .jackson .databind .ObjectMapper ;
7
-
8
8
import com .fasterxml .jackson .module .afterburner .AfterburnerTestBase ;
9
9
10
10
public class TestSimpleDeserialize extends AfterburnerTestBase
@@ -138,21 +138,26 @@ public BigBeanWithNonVoidPropertySetter setStringField(String username) {
138
138
public String getBogus5 () { return "" ; }
139
139
}
140
140
141
+ // for [module-afterburner#60]
142
+ static class Issue60Pojo {
143
+ public List <Object > foos ;
144
+ }
145
+
141
146
/*
142
147
/**********************************************************************
143
148
/* Test methods, method access
144
149
/**********************************************************************
145
150
*/
146
151
152
+ private final ObjectMapper MAPPER = mapperWithModule ();
153
+
147
154
public void testIntMethod () throws Exception {
148
- ObjectMapper mapper = mapperWithModule ();
149
- IntBean bean = mapper .readValue ("{\" x\" :13}" , IntBean .class );
155
+ IntBean bean = MAPPER .readValue ("{\" x\" :13}" , IntBean .class );
150
156
assertEquals (13 , bean ._x );
151
157
}
152
158
153
159
public void testMultiIntMethod () throws Exception {
154
- ObjectMapper mapper = mapperWithModule ();
155
- IntsBean bean = mapper .readValue ("{\" c\" :3,\" a\" :9,\" b\" :111,\" e\" :-9999,\" d\" :1}" , IntsBean .class );
160
+ IntsBean bean = MAPPER .readValue ("{\" c\" :3,\" a\" :9,\" b\" :111,\" e\" :-9999,\" d\" :1}" , IntsBean .class );
156
161
assertEquals (9 , bean ._a );
157
162
assertEquals (111 , bean ._b );
158
163
assertEquals (3 , bean ._c );
@@ -161,20 +166,17 @@ public void testMultiIntMethod() throws Exception {
161
166
}
162
167
163
168
public void testLongMethod () throws Exception {
164
- ObjectMapper mapper = mapperWithModule ();
165
- LongBean bean = mapper .readValue ("{\" x\" :-1}" , LongBean .class );
169
+ LongBean bean = MAPPER .readValue ("{\" x\" :-1}" , LongBean .class );
166
170
assertEquals (-1 , bean ._x );
167
171
}
168
172
169
173
public void testStringMethod () throws Exception {
170
- ObjectMapper mapper = mapperWithModule ();
171
- StringBean bean = mapper .readValue ("{\" x\" :\" zoobar\" }" , StringBean .class );
174
+ StringBean bean = MAPPER .readValue ("{\" x\" :\" zoobar\" }" , StringBean .class );
172
175
assertEquals ("zoobar" , bean ._x );
173
176
}
174
177
175
178
public void testObjectMethod () throws Exception {
176
- ObjectMapper mapper = mapperWithModule ();
177
- EnumBean bean = mapper .readValue ("{\" x\" :\" A\" }" , EnumBean .class );
179
+ EnumBean bean = MAPPER .readValue ("{\" x\" :\" A\" }" , EnumBean .class );
178
180
assertEquals (MyEnum .A , bean ._x );
179
181
}
180
182
@@ -185,37 +187,32 @@ public void testObjectMethod() throws Exception {
185
187
*/
186
188
187
189
public void testIntField () throws Exception {
188
- ObjectMapper mapper = mapperWithModule ();
189
- IntFieldBean bean = mapper .readValue ("{\" value\" :-92}" , IntFieldBean .class );
190
+ IntFieldBean bean = MAPPER .readValue ("{\" value\" :-92}" , IntFieldBean .class );
190
191
assertEquals (-92 , bean .x );
191
192
}
192
193
193
194
public void testLongField () throws Exception {
194
- ObjectMapper mapper = mapperWithModule ();
195
- LongFieldBean bean = mapper .readValue ("{\" value\" :-92}" , LongFieldBean .class );
195
+ LongFieldBean bean = MAPPER .readValue ("{\" value\" :-92}" , LongFieldBean .class );
196
196
assertEquals (-92 , bean .value );
197
197
}
198
198
199
199
public void testStringField () throws Exception {
200
- ObjectMapper mapper = mapperWithModule ();
201
- StringFieldBean bean = mapper .readValue ("{\" x\" :\" \" }" , StringFieldBean .class );
200
+ StringFieldBean bean = MAPPER .readValue ("{\" x\" :\" \" }" , StringFieldBean .class );
202
201
assertEquals ("" , bean .x );
203
202
204
203
// also, null handling:
205
- bean = mapper .readValue ("{\" x\" :null}" , StringFieldBean .class );
204
+ bean = MAPPER .readValue ("{\" x\" :null}" , StringFieldBean .class );
206
205
assertNull (bean .x );
207
206
}
208
207
209
208
public void testEnumField () throws Exception {
210
- ObjectMapper mapper = mapperWithModule ();
211
- EnumFieldBean bean = mapper .readValue ("{\" x\" :\" C\" }" , EnumFieldBean .class );
209
+ EnumFieldBean bean = MAPPER .readValue ("{\" x\" :\" C\" }" , EnumFieldBean .class );
212
210
assertEquals (MyEnum .C , bean .x );
213
211
}
214
212
215
213
// Verify [Issue#10], so that nulls do not get coerced to String "null"
216
214
public void testStringAsObjectField () throws Exception {
217
- ObjectMapper mapper = mapperWithModule ();
218
- StringAsObject bean = mapper .readValue ("{\" value\" :null}" , StringAsObject .class );
215
+ StringAsObject bean = MAPPER .readValue ("{\" value\" :null}" , StringAsObject .class );
219
216
assertNotNull (bean );
220
217
assertNull (bean .value );
221
218
}
@@ -228,21 +225,19 @@ public void testStringAsObjectField() throws Exception {
228
225
229
226
public void testFiveMinuteDoc () throws Exception
230
227
{
231
- ObjectMapper abMapper = mapperWithModule ();
232
228
FiveMinuteUser input = new FiveMinuteUser ("First" , "Name" , true ,
233
229
FiveMinuteUser .Gender .FEMALE , new byte [] { 1 } );
234
- String jsonAb = abMapper .writeValueAsString (input );
230
+ String jsonAb = MAPPER .writeValueAsString (input );
235
231
236
- FiveMinuteUser output = abMapper .readValue (jsonAb , FiveMinuteUser .class );
232
+ FiveMinuteUser output = MAPPER .readValue (jsonAb , FiveMinuteUser .class );
237
233
if (!output .equals (input )) {
238
234
fail ("Round-trip test failed: intermediate JSON = " +jsonAb );
239
235
}
240
236
}
241
237
242
238
public void testMixed () throws Exception
243
239
{
244
- ObjectMapper mapper = mapperWithModule ();
245
- MixedBean bean = mapper .readValue ("{"
240
+ MixedBean bean = MAPPER .readValue ("{"
246
241
+"\" stringField\" :\" a\" ,"
247
242
+"\" string\" :\" b\" ,"
248
243
+"\" intField\" :3,"
@@ -268,8 +263,7 @@ public void testNonVoidProperty() throws Exception
268
263
{
269
264
final String json = "{ \" stringField\" : \" zoobar\" , \" stringField2\" : \" barzoo\" }" ;
270
265
271
- ObjectMapper mapper = new ObjectMapper ();
272
- BeanWithNonVoidPropertySetter bean = mapper .readValue (json , BeanWithNonVoidPropertySetter .class );
266
+ BeanWithNonVoidPropertySetter bean = MAPPER .readValue (json , BeanWithNonVoidPropertySetter .class );
273
267
assertEquals ("zoobar" , bean .getStringField ());
274
268
275
269
ObjectMapper abMapper = mapperWithModule (); // if I don't do this, the module won't be picked up
@@ -284,8 +278,7 @@ public void testBigNonVoidProperty() throws Exception
284
278
{
285
279
final String json = "{ \" stringField\" : \" zoobar\" }" ;
286
280
287
- ObjectMapper mapper = new ObjectMapper ();
288
- BigBeanWithNonVoidPropertySetter bean = mapper .readValue (json , BigBeanWithNonVoidPropertySetter .class );
281
+ BigBeanWithNonVoidPropertySetter bean = MAPPER .readValue (json , BigBeanWithNonVoidPropertySetter .class );
289
282
assertEquals ("zoobar" , bean .getStringField ());
290
283
291
284
ObjectMapper abMapper = mapperWithModule (); // if I don't do this, the module won't be picked up
@@ -297,18 +290,38 @@ public void testBigNonVoidProperty() throws Exception
297
290
// NOTE: failed with databind-2.5.0; fixed for 2.5.1
298
291
public void testStringBuilder () throws Exception
299
292
{
300
- ObjectMapper abMapper = mapperWithModule ();
301
- StringBuilder sb = abMapper .readValue (quote ("foobar" ), StringBuilder .class );
293
+ StringBuilder sb = MAPPER .readValue (quote ("foobar" ), StringBuilder .class );
302
294
assertEquals ("foobar" , sb .toString ());
303
295
}
304
296
305
297
public void testBooleans () throws Exception
306
298
{
307
- ObjectMapper mapper = mapperWithModule ();
308
- BooleansBean bean = mapper .readValue (aposToQuotes ("{'a':true, 'b':true}" ),
299
+ BooleansBean bean = MAPPER .readValue (aposToQuotes ("{'a':true, 'b':true}" ),
309
300
BooleansBean .class );
310
301
assertNotNull (bean );
311
302
assertTrue (bean .a );
312
303
assertEquals (Boolean .TRUE , bean ._b );
313
304
}
305
+
306
+ // for [module-afterburner#60]
307
+ public void testProblemWithIndentation () throws Exception {
308
+ final String JSON = "{\n "
309
+ +" \" foos\" :\n "
310
+ +" [\n "
311
+ +" ]\n "
312
+ +"}" ;
313
+
314
+ // First: read from String directly
315
+
316
+ Issue60Pojo pojo = MAPPER .readValue (JSON , Issue60Pojo .class );
317
+ assertNotNull (pojo );
318
+ assertNotNull (pojo .foos );
319
+ assertEquals (0 , pojo .foos .size ());
320
+
321
+ // and then as bytes via InputStream
322
+ pojo = MAPPER .readValue (JSON .getBytes ("UTF-8" ), Issue60Pojo .class );
323
+ assertNotNull (pojo );
324
+ assertNotNull (pojo .foos );
325
+ assertEquals (0 , pojo .foos .size ());
326
+ }
314
327
}
0 commit comments