1+ package org .baeldung .gson .serialization .test ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import java .lang .reflect .Type ;
7+ import java .util .Collection ;
8+ import java .util .Date ;
9+
10+ import org .baeldung .gson .serialization .DifferentNameSerializer ;
11+ import org .baeldung .gson .serialization .IgnoringFieldsNotMatchingCriteriaSerializer ;
12+ import org .baeldung .gson .serialization .IgnoringFieldsSerializer ;
13+ import org .baeldung .gson .serialization .SourceClass ;
14+ import org .junit .Test ;
15+
16+ import com .google .common .collect .Lists ;
17+ import com .google .gson .Gson ;
18+ import com .google .gson .GsonBuilder ;
19+ import com .google .gson .reflect .TypeToken ;
20+
21+ public class GsonSerializationTest {
22+
23+ @ Test
24+ public void givenArrayOfObjects_whenSerializing_thenCorrect () {
25+ final SourceClass [] sourceArray = { new SourceClass (1 , "one" ), new SourceClass (2 , "two" ) };
26+ final String jsonString = new Gson ().toJson (sourceArray );
27+
28+ // test
29+ final String expectedResult = "[{\" intValue\" :1,\" stringValue\" :\" one\" },{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
30+ assertEquals (expectedResult , jsonString );
31+ }
32+
33+ @ Test
34+ public void givenCollection_whenSerializing_thenCorrect () {
35+ final Collection <SourceClass > sourceCollection = Lists .newArrayList (new SourceClass (1 , "one" ), new SourceClass (2 , "two" ));
36+ final Type sourceCollectionType = new TypeToken <Collection <SourceClass >>() {
37+ }.getType ();
38+ final String jsonCollection = new Gson ().toJson (sourceCollection , sourceCollectionType );
39+
40+ final String expectedResult = "[{\" intValue\" :1,\" stringValue\" :\" one\" },{\" intValue\" :2,\" stringValue\" :\" two\" }]" ;
41+ assertEquals (expectedResult , jsonCollection );
42+ }
43+
44+ @ Test
45+ public void givenUsingCustomSerializer_whenChangingNameOfFieldOnSerializing_thenCorrect () {
46+ final SourceClass sourceObject = new SourceClass (7 , "seven" );
47+ final GsonBuilder gsonBuildr = new GsonBuilder ();
48+ gsonBuildr .registerTypeAdapter (SourceClass .class , new DifferentNameSerializer ());
49+ final String jsonString = gsonBuildr .create ().toJson (sourceObject );
50+
51+ final String expectedResult = "{\" otherIntValue\" :7,\" otherStringValue\" :\" seven\" }" ;
52+ assertEquals (expectedResult , jsonString );
53+ }
54+
55+ @ Test
56+ public void givenIgnoringAField_whenSerializingWithCustomSerializer_thenFieldIgnored () {
57+ final SourceClass sourceObject = new SourceClass (7 , "seven" );
58+ final GsonBuilder gsonBuildr = new GsonBuilder ();
59+ gsonBuildr .registerTypeAdapter (SourceClass .class , new IgnoringFieldsSerializer ());
60+ final String jsonString = gsonBuildr .create ().toJson (sourceObject );
61+
62+ final String expectedResult = "{\" intValue\" :7}" ;
63+ assertEquals (expectedResult , jsonString );
64+ }
65+
66+ @ Test
67+ public void givenDate_whenSerializing_thenCorrect () {
68+ Date sourceDate = new Date (1000000L );
69+ Gson gson = new Gson ();
70+ Type sourceDateType = new TypeToken <Date >() {
71+ }.getType ();
72+ String jsonDate = gson .toJson (sourceDate , sourceDateType );
73+ // test
74+ System .out .println ("jsonDate:\n " + jsonDate );
75+ String estimatedResult = "\" Jan 1, 1970 3:16:40 AM\" " ;
76+ assertTrue (jsonDate .equals (estimatedResult ));
77+ }
78+
79+ @ Test
80+ public void givenUsingCustomDeserializer_whenFieldNotMatchesCriteria_thenIgnoringIt () {
81+ SourceClass sourceObject = new SourceClass (-1 , "minus 1" );
82+ GsonBuilder gsonBuildr = new GsonBuilder ();
83+ gsonBuildr .registerTypeAdapter (SourceClass .class , new IgnoringFieldsNotMatchingCriteriaSerializer ());
84+ Gson gson = gsonBuildr .create ();
85+ Type sourceObjectType = new TypeToken <SourceClass >() {
86+ }.getType ();
87+ String jsonString = gson .toJson (sourceObject , sourceObjectType );
88+ // test
89+ String estimatedResult = "{\" stringValue\" :\" minus 1\" }" ;
90+ assertEquals (estimatedResult , jsonString );
91+ }
92+
93+ }
0 commit comments