1+ package org .baeldung .gson .advance ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import com .google .gson .Gson ;
7+ import com .google .gson .GsonBuilder ;
8+ import com .google .gson .reflect .TypeToken ;
9+ import java .lang .reflect .Type ;
10+ import java .util .ArrayList ;
11+ import java .util .List ;
12+ import org .baeldung .gson .entities .Animal ;
13+ import org .baeldung .gson .entities .Cow ;
14+ import org .baeldung .gson .entities .Dog ;
15+ import org .baeldung .gson .entities .MyClass ;
16+ import org .baeldung .gson .serialization .AnimalDeserializer ;
17+ import org .junit .Test ;
18+
19+ public class GsonAdvanceUnitTest {
20+
21+ @ Test public void givenListOfMyClass_whenSerializing_thenCorrect () {
22+ List <MyClass > list = new ArrayList <>();
23+ list .add (new MyClass ());
24+ list .add (new MyClass ());
25+
26+ Gson gson = new Gson ();
27+ String jsonString = gson .toJson (list );
28+ String expectedString = "[{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]},{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]}]" ;
29+
30+ assertEquals (expectedString , jsonString );
31+ }
32+
33+ @ Test (expected = ClassCastException .class )
34+ public void givenJsonString_whenIncorrectDeserializing_thenThrowClassCastException () {
35+ String inputString = "[{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]},{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]}]" ;
36+
37+ Gson gson = new Gson ();
38+ List <MyClass > list = gson .fromJson (inputString , ArrayList .class );
39+
40+ assertEquals (2 , list .size ());
41+ int id = list .get (0 ).getId ();
42+ }
43+
44+ @ Test public void givenJsonString_whenDeserializing_thenReturnListOfMyClass () {
45+ String inputString = "[{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]},{\" id\" :1,\" strings\" :[\" a\" ,\" b\" ]}]" ;
46+ Type listOfMyClassObject = new TypeToken <ArrayList <MyClass >>() {}.getType ();
47+
48+ Gson gson = new Gson ();
49+ List <MyClass > list = gson .fromJson (inputString , listOfMyClassObject );
50+
51+ assertEquals (2 , list .size ());
52+ assertEquals (1 , list .get (0 ).getId ());
53+ }
54+
55+ @ Test public void givenPolymorphicList_whenSerializeWithTypeAdapter_thenCorrect () {
56+ String expectedString = "[{\" petName\" :\" Milo\" ,\" type\" :\" Dog\" },{\" breed\" :\" Jersey\" ,\" type\" :\" Cow\" }]" ;
57+
58+ List <Animal > inList = new ArrayList <>();
59+ inList .add (new Dog ());
60+ inList .add (new Cow ());
61+
62+ String jsonString = new Gson ().toJson (inList );
63+
64+ assertEquals (expectedString , jsonString );
65+ }
66+
67+ @ Test public void givenPolymorphicList_whenDeserializeWithTypeAdapter_thenCorrect () {
68+ String inputString = "[{\" petName\" :\" Milo\" ,\" type\" :\" Dog\" },{\" breed\" :\" Jersey\" ,\" type\" :\" Cow\" }]" ;
69+
70+ AnimalDeserializer deserializer = new AnimalDeserializer ("type" );
71+ deserializer .registerBarnType ("Dog" , Dog .class );
72+ deserializer .registerBarnType ("Cow" , Cow .class );
73+ Gson gson = new GsonBuilder ()
74+ .registerTypeAdapter (Animal .class , deserializer )
75+ .create ();
76+
77+ List <Animal > outList = gson .fromJson (inputString , new TypeToken <List <Animal >>(){}.getType ());
78+
79+ assertEquals (2 , outList .size ());
80+ assertTrue (outList .get (0 ) instanceof Dog );
81+ }
82+
83+ @ Test public void givenPolymorphicList_whenSerializeWithRuntimeTypeAdapter_thenCorrect () {
84+ String expectedString = "[{\" petName\" :\" Milo\" ,\" type\" :\" Dog\" },{\" breed\" :\" Jersey\" ,\" type\" :\" Cow\" }]" ;
85+
86+ List <Animal > inList = new ArrayList <>();
87+ inList .add (new Dog ());
88+ inList .add (new Cow ());
89+ String jsonString = new Gson ().toJson (inList );
90+
91+ assertEquals (expectedString , jsonString );
92+ }
93+
94+ @ Test public void givenPolymorphicList_whenDeserializeWithRuntimeTypeAdapter_thenCorrect () {
95+ String inputString = "[{\" petName\" :\" Milo\" ,\" type\" :\" Dog\" },{\" breed\" :\" Jersey\" ,\" type\" :\" Cow\" }]" ;
96+
97+ Type listOfAnimals = new TypeToken <ArrayList <Animal >>() {}.getType ();
98+
99+ RuntimeTypeAdapterFactory <Animal > adapter = RuntimeTypeAdapterFactory .of (Animal .class )
100+ .registerSubtype (Dog .class )
101+ .registerSubtype (Cow .class );
102+
103+ Gson gson = new GsonBuilder ().registerTypeAdapterFactory (adapter ).create ();
104+
105+ List <Animal > outList = gson .fromJson (inputString , listOfAnimals );
106+
107+ assertEquals (2 , outList .size ());
108+ assertTrue (outList .get (0 ) instanceof Dog );
109+ }
110+ }
0 commit comments