@@ -188,22 +188,22 @@ public ObjectIdResolver newForDeserialization(Object c)
188
188
/*****************************************************
189
189
*/
190
190
191
- private final ObjectMapper mapper = new ObjectMapper ();
191
+ private final ObjectMapper MAPPER = new ObjectMapper ();
192
192
193
193
private final static String EXP_SIMPLE_INT_CLASS = "{\" id\" :1,\" value\" :13,\" next\" :1}" ;
194
194
195
195
public void testSimpleDeserializationClass () throws Exception
196
196
{
197
197
// then bring back...
198
- Identifiable result = mapper .readValue (EXP_SIMPLE_INT_CLASS , Identifiable .class );
198
+ Identifiable result = MAPPER .readValue (EXP_SIMPLE_INT_CLASS , Identifiable .class );
199
199
assertEquals (13 , result .value );
200
200
assertSame (result , result .next );
201
201
}
202
202
203
203
// Should be ok NOT to have Object id, as well
204
204
public void testMissingObjectId () throws Exception
205
205
{
206
- Identifiable result = mapper .readValue (aposToQuotes ("{'value':28, 'next':{'value':29}}" ),
206
+ Identifiable result = MAPPER .readValue (aposToQuotes ("{'value':28, 'next':{'value':29}}" ),
207
207
Identifiable .class );
208
208
assertNotNull (result );
209
209
assertEquals (28 , result .value );
@@ -222,10 +222,10 @@ public void testSimpleUUIDForClassRoundTrip() throws Exception
222
222
child2 .parent = root ;
223
223
child1 .first = child2 ;
224
224
225
- String json = mapper .writeValueAsString (root );
225
+ String json = MAPPER .writeValueAsString (root );
226
226
227
227
// and should come back the same too...
228
- UUIDNode result = mapper .readValue (json , UUIDNode .class );
228
+ UUIDNode result = MAPPER .readValue (json , UUIDNode .class );
229
229
assertEquals (1 , result .value );
230
230
UUIDNode result2 = result .first ;
231
231
UUIDNode result3 = result .second ;
@@ -244,15 +244,15 @@ public void testSimpleUUIDForClassRoundTrip() throws Exception
244
244
245
245
public void testSimpleDeserializationProperty () throws Exception
246
246
{
247
- IdWrapper result = mapper .readValue (EXP_SIMPLE_INT_PROP , IdWrapper .class );
247
+ IdWrapper result = MAPPER .readValue (EXP_SIMPLE_INT_PROP , IdWrapper .class );
248
248
assertEquals (7 , result .node .value );
249
249
assertSame (result .node , result .node .next .node );
250
250
}
251
251
252
252
// Another test to ensure ordering is not required (i.e. can do front references)
253
253
public void testSimpleDeserWithForwardRefs () throws Exception
254
254
{
255
- IdWrapper result = mapper .readValue ("{\" node\" :{\" value\" :7,\" next\" :{\" node\" :1}, \" @id\" :1}}"
255
+ IdWrapper result = MAPPER .readValue ("{\" node\" :{\" value\" :7,\" next\" :{\" node\" :1}, \" @id\" :1}}"
256
256
,IdWrapper .class );
257
257
assertEquals (7 , result .node .value );
258
258
assertSame (result .node , result .node .next .node );
@@ -265,7 +265,7 @@ public void testForwardReference()
265
265
+ "{\" id\" :1,\" name\" :\" First\" ,\" manager\" :2,\" reports\" :[]},"
266
266
+ "{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :null,\" reports\" :[1]}"
267
267
+ "]}" ;
268
- Company company = mapper .readValue (json , Company .class );
268
+ Company company = MAPPER .readValue (json , Company .class );
269
269
assertEquals (2 , company .employees .size ());
270
270
Employee firstEmployee = company .employees .get (0 );
271
271
Employee secondEmployee = company .employees .get (1 );
@@ -282,7 +282,7 @@ public void testForwardReferenceInCollection()
282
282
+ "{\" id\" :1,\" name\" :\" First\" ,\" manager\" :null,\" reports\" :[2]},"
283
283
+ "{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :1,\" reports\" :[]}"
284
284
+ "]}" ;
285
- Company company = mapper .readValue (json , Company .class );
285
+ Company company = MAPPER .readValue (json , Company .class );
286
286
assertEquals (2 , company .employees .size ());
287
287
Employee firstEmployee = company .employees .get (0 );
288
288
Employee secondEmployee = company .employees .get (1 );
@@ -297,7 +297,7 @@ public void testForwardReferenceInMap()
297
297
+ "\" 2\" : 2,"
298
298
+ "\" 3\" :{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :1,\" reports\" :[]}"
299
299
+ "}}" ;
300
- MappedCompany company = mapper .readValue (json , MappedCompany .class );
300
+ MappedCompany company = MAPPER .readValue (json , MappedCompany .class );
301
301
assertEquals (3 , company .employees .size ());
302
302
Employee firstEmployee = company .employees .get (1 );
303
303
Employee secondEmployee = company .employees .get (3 );
@@ -315,7 +315,7 @@ private void assertEmployees(Employee firstEmployee, Employee secondEmployee)
315
315
316
316
public void testForwardReferenceAnySetterCombo () throws Exception {
317
317
String json = "{\" @id\" :1, \" foo\" :2, \" bar\" :{\" @id\" :2, \" foo\" :1}}" ;
318
- AnySetterObjectId value = mapper .readValue (json , AnySetterObjectId .class );
318
+ AnySetterObjectId value = MAPPER .readValue (json , AnySetterObjectId .class );
319
319
assertSame (value .values .get ("bar" ), value .values .get ("foo" ));
320
320
}
321
321
@@ -327,7 +327,7 @@ public void testUnresolvedForwardReference()
327
327
+ "{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :3,\" reports\" :[]}"
328
328
+ "]}" ;
329
329
try {
330
- mapper .readValue (json , Company .class );
330
+ MAPPER .readValue (json , Company .class );
331
331
fail ("Should have thrown." );
332
332
} catch (UnresolvedForwardReference exception ) {
333
333
// Expected
@@ -345,7 +345,7 @@ public void testUnresolvedForwardReference()
345
345
// [databind#299]: Allow unresolved ids to become nulls
346
346
public void testUnresolvableAsNull () throws Exception
347
347
{
348
- IdWrapper w = mapper .reader (IdWrapper .class )
348
+ IdWrapper w = MAPPER .reader (IdWrapper .class )
349
349
.without (DeserializationFeature .FAIL_ON_UNRESOLVED_OBJECT_IDS )
350
350
.readValue (aposToQuotes ("{'node':123}" ));
351
351
assertNotNull (w );
@@ -358,7 +358,7 @@ public void testKeepCollectionOrdering() throws Exception
358
358
+ "{\" id\" :1,\" name\" :\" First\" ,\" manager\" :null,\" reports\" :[2]},"
359
359
+ "{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :1,\" reports\" :[]}"
360
360
+ "]}" ;
361
- Company company = mapper .readValue (json , Company .class );
361
+ Company company = MAPPER .readValue (json , Company .class );
362
362
assertEquals (4 , company .employees .size ());
363
363
// Deser must keep object ordering.
364
364
Employee firstEmployee = company .employees .get (1 );
@@ -376,7 +376,7 @@ public void testKeepMapOrdering()
376
376
+ "\" 3\" :{\" id\" :1,\" name\" :\" First\" ,\" manager\" :null,\" reports\" :[2]},"
377
377
+ "\" 4\" :{\" id\" :2,\" name\" :\" Second\" ,\" manager\" :1,\" reports\" :[]}"
378
378
+ "}}" ;
379
- MappedCompany company = mapper .readValue (json , MappedCompany .class );
379
+ MappedCompany company = MAPPER .readValue (json , MappedCompany .class );
380
380
assertEquals (4 , company .employees .size ());
381
381
Employee firstEmployee = company .employees .get (2 );
382
382
Employee secondEmployee = company .employees .get (1 );
@@ -401,7 +401,7 @@ public void testKeepMapOrdering()
401
401
public void testCustomDeserializationClass () throws Exception
402
402
{
403
403
// then bring back...
404
- IdentifiableCustom result = mapper .readValue (EXP_CUSTOM_VIA_CLASS , IdentifiableCustom .class );
404
+ IdentifiableCustom result = MAPPER .readValue (EXP_CUSTOM_VIA_CLASS , IdentifiableCustom .class );
405
405
assertEquals (-900 , result .value );
406
406
assertSame (result , result .next );
407
407
}
@@ -411,7 +411,7 @@ public void testCustomDeserializationClass() throws Exception
411
411
public void testCustomDeserializationProperty () throws Exception
412
412
{
413
413
// then bring back...
414
- IdWrapperExt result = mapper .readValue (EXP_CUSTOM_VIA_PROP , IdWrapperExt .class );
414
+ IdWrapperExt result = MAPPER .readValue (EXP_CUSTOM_VIA_PROP , IdWrapperExt .class );
415
415
assertEquals (99 , result .node .value );
416
416
assertSame (result .node , result .node .next .node );
417
417
assertEquals (3 , result .node .customId );
@@ -422,21 +422,48 @@ public void testCustomDeserializationProperty() throws Exception
422
422
/* Unit tests, custom id resolver
423
423
/*****************************************************
424
424
*/
425
- public void testCustomPoolResolver ()
426
- throws Exception
425
+
426
+ public void testCustomPoolResolver () throws Exception
427
427
{
428
428
Map <Object ,WithCustomResolution > pool = new HashMap <Object ,WithCustomResolution >();
429
429
pool .put (1 , new WithCustomResolution (1 , 1 ));
430
430
pool .put (2 , new WithCustomResolution (2 , 2 ));
431
431
pool .put (3 , new WithCustomResolution (3 , 3 ));
432
432
pool .put (4 , new WithCustomResolution (4 , 4 ));
433
433
pool .put (5 , new WithCustomResolution (5 , 5 ));
434
- ContextAttributes attrs = mapper .getDeserializationConfig ().getAttributes ().withSharedAttribute (POOL_KEY , pool );
434
+ ContextAttributes attrs = MAPPER .getDeserializationConfig ().getAttributes ().withSharedAttribute (POOL_KEY , pool );
435
435
String content = "{\" data\" :[1,2,3,4,5]}" ;
436
- CustomResolutionWrapper wrapper = mapper .reader (CustomResolutionWrapper .class ).with (attrs ).readValue (content );
436
+ CustomResolutionWrapper wrapper = MAPPER .reader (CustomResolutionWrapper .class ).with (attrs ).readValue (content );
437
437
assertFalse (wrapper .data .isEmpty ());
438
438
for (WithCustomResolution ob : wrapper .data ) {
439
439
assertSame (pool .get (ob .id ), ob );
440
440
}
441
441
}
442
+
443
+ /*
444
+ /*****************************************************
445
+ /* Unit tests, missing/null Object id [databind#742]
446
+ /*****************************************************
447
+ */
448
+
449
+ /*
450
+ private final static String EXP_SIMPLE_INT_CLASS = "{\"id\":1,\"value\":13,\"next\":1}";
451
+ @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
452
+ static class Identifiable
453
+ {
454
+ public int value;
455
+
456
+ public Identifiable next;
457
+ }
458
+ */
459
+
460
+ public void testNullObjectId () throws Exception
461
+ {
462
+ // Ok, so missing Object Id is ok, but so is null.
463
+
464
+ Identifiable value = MAPPER .readValue
465
+ (aposToQuotes ("{'value':3, 'next':null, 'id':null}" ), Identifiable .class );
466
+ assertNotNull (value );
467
+ assertEquals (3 , value .value );
468
+ }
442
469
}
0 commit comments