88
99import java .lang .reflect .Type ;
1010import java .util .ArrayList ;
11- import java .util .Arrays ;
1211import java .util .Collection ;
13- import java .util .List ;
1412
1513import org .baeldung .gson .deserialization .Foo ;
1614import org .baeldung .gson .deserialization .FooDeserializerFromJsonWithDifferentFields ;
1715import org .baeldung .gson .deserialization .GenericFoo ;
1816import org .junit .Test ;
1917
18+ import com .google .common .collect .Lists ;
2019import com .google .gson .Gson ;
2120import com .google .gson .GsonBuilder ;
2221import com .google .gson .JsonElement ;
@@ -29,57 +28,66 @@ public class GsonDeserializationTest {
2928 // tests - single element
3029
3130 @ Test
32- public final void givenJsonHasExtraValuesButGsonIsIgnoringExtras_whenDeserializing_thenCorrect () {
33- final String serializedSourceObject = "{\" intValue\" :1,\" stringValue\" :\" one\" ,\" extraString\" :\" two\" ,\" extraFloat\" :2.2}" ;
34- final Foo targetObject = new Gson ().fromJson (serializedSourceObject , Foo .class );
31+ public final void whenDeserializingToSimpleObject_thenCorrect () {
32+ final String json = "{\" intValue\" :1,\" stringValue\" :\" one\" }" ;
33+
34+ final Foo targetObject = new Gson ().fromJson (json , Foo .class );
3535
3636 assertEquals (targetObject .intValue , 1 );
3737 assertEquals (targetObject .stringValue , "one" );
3838 }
3939
4040 @ Test
41- public final void givenJsonHasNonMatchingFieldNames_whenDeserializingWithCustomDeserializer_thenCorrect () {
42- final String jsonSourceObject = "{\" valueInt\" :7,\" valueString\" :\" seven\" }" ;
41+ public final void givenJsonHasExtraValues_whenDeserializing_thenCorrect () {
42+ final String json = "{\" intValue\" :1,\" stringValue\" :\" one\" ,\" extraString\" :\" two\" ,\" extraFloat\" :2.2}" ;
43+ final Foo targetObject = new Gson ().fromJson (json , Foo .class );
44+
45+ assertEquals (targetObject .intValue , 1 );
46+ assertEquals (targetObject .stringValue , "one" );
47+ }
48+
49+ @ Test
50+ public final void givenJsonHasNonMatchingFields_whenDeserializingWithCustomDeserializer_thenCorrect () {
51+ final String json = "{\" valueInt\" :7,\" valueString\" :\" seven\" }" ;
4352
4453 final GsonBuilder gsonBldr = new GsonBuilder ();
4554 gsonBldr .registerTypeAdapter (Foo .class , new FooDeserializerFromJsonWithDifferentFields ());
46- final Foo targetObject = gsonBldr .create ().fromJson (jsonSourceObject , Foo .class );
55+ final Foo targetObject = gsonBldr .create ().fromJson (json , Foo .class );
4756
4857 assertEquals (targetObject .intValue , 7 );
4958 assertEquals (targetObject .stringValue , "seven" );
5059 }
5160
5261 @ Test
53- public final void givenUsingGson_whenDeserializingGeneric_thenCorrect () {
54- final Type genericTargetClassType = new TypeToken <GenericFoo <Integer >>() {
62+ public final void whenDeserializingToGenericObject_thenCorrect () {
63+ final Type typeToken = new TypeToken <GenericFoo <Integer >>() {
5564 }.getType ();
56- final String serializedSourceObject = "{\" intField \" :1}" ;
65+ final String json = "{\" theValue \" :1}" ;
5766
58- final GenericFoo <Integer > targetObject = new Gson ().fromJson (serializedSourceObject , genericTargetClassType );
67+ final GenericFoo <Integer > targetObject = new Gson ().fromJson (json , typeToken );
5968
60- assertEquals (targetObject .intField , new Integer (1 ));
69+ assertEquals (targetObject .theValue , new Integer (1 ));
6170 }
6271
6372 // tests - multiple elements
6473
6574 @ Test
66- public final void givenJsonArrayOfFoos_whenDeserializingToList_thenCorrect () {
67- final String jsonSourceObject = "[{\" intValue\" :1,\" stringValue\" :\" one\" },{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
68- final Foo [] objectsAsArray = new GsonBuilder ().create ().fromJson (jsonSourceObject , Foo [].class );
69- final List <Foo > targetList = Arrays .asList (objectsAsArray );
70-
71- assertThat (targetList , hasItem (new Foo (1 , "one" )));
72- assertThat (targetList , hasItem (new Foo (2 , "two" )));
73- assertThat (targetList , not (hasItem (new Foo (1 , "two" ))));
75+ public final void givenJsonArrayOfFoos_whenDeserializingToArray_thenCorrect () {
76+ final String json = "[{\" intValue\" :1,\" stringValue\" :\" one\" }," + "{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
77+ final Foo [] targetArray = new GsonBuilder ().create ().fromJson (json , Foo [].class );
78+
79+ assertThat (Lists .newArrayList (targetArray ), hasItem (new Foo (1 , "one" )));
80+ assertThat (Lists .newArrayList (targetArray ), hasItem (new Foo (2 , "two" )));
81+ assertThat (Lists .newArrayList (targetArray ), not (hasItem (new Foo (1 , "two" ))));
7482 }
7583
7684 @ Test
77- public final void givenUsingGson_whenDeserializingCollection_thenCorrect () {
78- final String serializedSourceCollection = "[{\" intValue\" :1,\" stringValue\" :\" one\" },{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
85+ public final void givenJsonArrayOfFoos_whenDeserializingCollection_thenCorrect () {
86+ final String json = "[{\" intValue\" :1,\" stringValue\" :\" one\" },{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
7987 final Type targetClassType = new TypeToken <ArrayList <Foo >>() {
8088 }.getType ();
8189
82- final Collection <Foo > targetCollection = new Gson ().fromJson (serializedSourceCollection , targetClassType );
90+ final Collection <Foo > targetCollection = new Gson ().fromJson (json , targetClassType );
8391 assertThat (targetCollection , instanceOf (ArrayList .class ));
8492 }
8593
0 commit comments