@@ -214,8 +214,8 @@ public void testResponseUsingB2Json() throws B2JsonException, JSONException {
214
214
System .out .println ("obj is: " + obj );
215
215
String expected = "{\n " +
216
216
" \" categories\" : [\n " +
217
- " \" test1 \" ,\n " +
218
- " \" test2 \" \n " +
217
+ " \" test2 \" ,\n " +
218
+ " \" test1 \" \n " +
219
219
" ],\n " +
220
220
" \" localDate\" : \" 20230331\" ,\n " +
221
221
" \" localDateTime\" : \" d20230331_m122100\" ,\n " +
@@ -310,11 +310,11 @@ public void testObjectUsingDefaultValue() throws B2JsonException {
310
310
System .out .println ("about to check the equality between Gson and B2Json results.." );
311
311
assertEquals (fromGson , fromB2Json );
312
312
String fromB2JsonString = b2Json .toJson (fromB2Json );
313
- String fromGsonString = gson .toJson (fromB2Json );
313
+ String fromGsonString = gson .toJson (fromGson );
314
314
String expectedFromGson = "{\n " +
315
315
" \" a\" : 2023,\n " +
316
316
" \" b\" : \" hello\" ,\n " +
317
- " \" c\" : 5 ,\n " +
317
+ " \" c\" : 0 ,\n " +
318
318
" \" d\" : 0\n " +
319
319
"}" ;
320
320
assertEquals (expectedFromGson , fromGsonString );
@@ -401,4 +401,44 @@ public TestRequest3(String zip) {
401
401
}
402
402
}
403
403
404
+ @ Test
405
+ public void testGsonFailure () {
406
+ // there's an unknown field called e in this json string, in this case, GSON library will just ignore it
407
+ String json = "{\n " +
408
+ " \" a\" : 2023,\n " +
409
+ " \" b\" : \" hello\" ,\n " +
410
+ " \" e\" : \" hello\" \n " +
411
+ "}" ;
412
+ System .out .println ("json is: " + json );
413
+ Container fromGson = gson .fromJson (json , Container .class );
414
+ System .out .println ("gson.fromJson(json) is: " + fromGson );
415
+ System .out .println ("about to check the equality between Gson and B2Json results.." );
416
+ String fromGsonString = gson .toJson (fromGson );
417
+ String expectedFromGson = "{\n " +
418
+ " \" a\" : 2023,\n " +
419
+ " \" b\" : \" hello\" ,\n " +
420
+ " \" c\" : 0,\n " +
421
+ " \" d\" : 0\n " +
422
+ "}" ;
423
+ assertEquals (expectedFromGson , fromGsonString );
424
+
425
+ // in this json, the field named a is missing
426
+ json = "{\n " +
427
+ " \" b\" : \" hello\" ,\n " +
428
+ " \" e\" : \" hello\" \n " +
429
+ "}" ;
430
+ System .out .println ("2nd time, json is: " + json );
431
+ fromGson = gson .fromJson (json , Container .class );
432
+ System .out .println ("2nd time, gson.fromJson(json) is: " + fromGson );
433
+ System .out .println ("about to check the equality between Gson and B2Json results.." );
434
+ fromGsonString = gson .toJson (fromGson );
435
+ System .out .println ("2nd time, fromGsonString is: " + fromGsonString );
436
+ expectedFromGson = "{\n " +
437
+ " \" a\" : 0,\n " +
438
+ " \" b\" : \" hello\" ,\n " +
439
+ " \" c\" : 0,\n " +
440
+ " \" d\" : 0\n " +
441
+ "}" ;
442
+ assertEquals (expectedFromGson , fromGsonString );
443
+ }
404
444
}
0 commit comments