In this blog we will understand how to serialize and deserialize collections using GSON library.
To get started with basic understanding of GSON you can refer this guide
In our projects we may want to send collection of objects as json string to other components.
Similarly we can receive collection of objects as string and want to map them to list of objects.
Let us add following maven dependency to our project.
1
2
3
4
5
< dependency >
< groupId > com . google . code . gson </ groupId >
< artifactId > gson </ artifactId >
< version > 2.8.5 </ version >
</ dependency >
Serialize collection of objects
In the below example we first create Gson object using GsonBuilder.
We will create 3 employee objects and add them to the list.
To serialise list of employee objects we will call toJson() method on Gson object created and pass list of employee objects as argument to it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.harishgowda.demo ;
import com.google.gson.Gson ;
import com.google.gson.GsonBuilder ;
import com.google.gson.reflect.TypeToken ;
import java.util.Arrays ;
import java.util.List ;
public class App {
public static void main ( String [] args ) {
Gson gson = new GsonBuilder ()
. setPrettyPrinting ()
. create ();
Employee emp1 = new Employee ();
emp1 . setId ( 100 );
emp1 . setFirstName ( "John" );
emp1 . setLastName ( "Doe" );
emp1 . setEmail ( "[email protected] " );
emp1 . setPhone ( "(123)456-7890" );
emp1 . setJobTitle ( "Software engineer" );
emp1 . setHireDate ( "2020-01-03" );
emp1 . setSalary ( 3000 );
emp1 . setDepartment ( "Technical Group" );
Employee emp2 = new Employee ();
emp2 . setId ( 101 );
emp2 . setFirstName ( "Tim" );
emp2 . setLastName ( "Brooks" );
emp2 . setEmail ( "[email protected] " );
emp2 . setPhone ( "(123)921-7890" );
emp2 . setJobTitle ( "Tech Lead" );
emp2 . setHireDate ( "2018-05-06" );
emp2 . setSalary ( 6000 );
emp2 . setDepartment ( "Technical Group" );
Employee emp3 = new Employee ();
emp3 . setId ( 102 );
emp3 . setFirstName ( "Amy" );
emp3 . setLastName ( "Perry" );
emp3 . setEmail ( "[email protected] " );
emp3 . setPhone ( "(123)765-1920" );
emp3 . setJobTitle ( "Test engineer" );
emp3 . setHireDate ( "2019-08-01" );
emp3 . setSalary ( 4000 );
emp3 . setDepartment ( "Technical Group" );
List < Employee > employees = Arrays . asList ( emp1 , emp2 , emp3 );
String data = gson . toJson ( employees );
System . out . println ( data );
}
}
class Employee {
public int id ;
public String firstName ;
public String lastName ;
public String email ;
public String phone ;
public String jobTitle ;
public String hireDate ;
public double salary ;
public String department ;
public int getId () {
return id ;
}
public void setId ( int id ) {
this . id = id ;
}
public String getFirstName () {
return firstName ;
}
public void setFirstName ( String firstName ) {
this . firstName = firstName ;
}
public String getLastName () {
return lastName ;
}
public void setLastName ( String lastName ) {
this . lastName = lastName ;
}
public String getEmail () {
return email ;
}
public void setEmail ( String email ) {
this . email = email ;
}
public String getPhone () {
return phone ;
}
public void setPhone ( String phone ) {
this . phone = phone ;
}
public String getJobTitle () {
return jobTitle ;
}
public void setJobTitle ( String jobTitle ) {
this . jobTitle = jobTitle ;
}
public String getHireDate () {
return hireDate ;
}
public void setHireDate ( String hireDate ) {
this . hireDate = hireDate ;
}
public double getSalary () {
return salary ;
}
public void setSalary ( double salary ) {
this . salary = salary ;
}
public String getDepartment () {
return department ;
}
public void setDepartment ( String department ) {
this . department = department ;
}
@Override
public String toString () {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", jobTitle='" + jobTitle + '\'' +
", hireDate='" + hireDate + '\'' +
", salary=" + salary +
", department='" + department + '\'' +
'}' ;
}
}
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[
{
"id" : 100 ,
"firstName" : "John" ,
"lastName" : "Doe" ,
"email" : "[email protected] " ,
"phone" : "(123)456-7890" ,
"jobTitle" : "Software engineer" ,
"hireDate" : "2020-01-03" ,
"salary" : 3000.0 ,
"department" : "Technical Group"
},
{
"id" : 101 ,
"firstName" : "Tim" ,
"lastName" : "Brooks" ,
"email" : "[email protected] " ,
"phone" : "(123)921-7890" ,
"jobTitle" : "Tech Lead" ,
"hireDate" : "2018-05-06" ,
"salary" : 6000.0 ,
"department" : "Technical Group"
},
{
"id" : 102 ,
"firstName" : "Amy" ,
"lastName" : "Perry" ,
"email" : "[email protected] " ,
"phone" : "(123)765-1920" ,
"jobTitle" : "Test engineer" ,
"hireDate" : "2019-08-01" ,
"salary" : 4000.0 ,
"department" : "Technical Group"
}
]
Deserialize collection of objects
In this case we are given json string which contains list of employee objects in an array.
Now we want this JSON to be translated/mapped to list of employee objects in Java.
If it was only Employee object to be mapped we could have done something like this.
1
gson . fromJson ( jsonString , Employee . class );
But we have list of employee objects.
It is not possible to specify something like below
1
gson . fromJson ( jsonString , List < Employee >. class );
To specify type at runtime we have to use TypeToken class(from GSON library) of type List<Employee> and invoke getType() method to specify correct data type required(List <Employee>) as shown in below snippet.
1
new TypeToken < List < Employee >>(){}. getType ()
Code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.harishgowda.demo ;
import com.google.gson.Gson ;
import com.google.gson.GsonBuilder ;
import com.google.gson.reflect.TypeToken ;
import java.util.Arrays ;
import java.util.List ;
public class App {
public static void main ( String [] args ) {
Gson gson = new GsonBuilder ()
. setPrettyPrinting ()
. create ();
String restData = "[\n" +
" {\n" +
" \"id\": 100,\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\": \"Doe\",\n" +
" \"email\": \"[email protected] \",\n" +
" \"phone\": \"(123)456-7890\",\n" +
" \"jobTitle\": \"Software engineer\",\n" +
" \"hireDate\": \"2020-01-03\",\n" +
" \"salary\": 3000.0,\n" +
" \"department\": \"Technical Group\"\n" +
" },\n" +
" {\n" +
" \"id\": 101,\n" +
" \"firstName\": \"Tim\",\n" +
" \"lastName\": \"Brooks\",\n" +
" \"email\": \"[email protected] \",\n" +
" \"phone\": \"(123)921-7890\",\n" +
" \"jobTitle\": \"Tech Lead\",\n" +
" \"hireDate\": \"2018-05-06\",\n" +
" \"salary\": 6000.0,\n" +
" \"department\": \"Technical Group\"\n" +
" },\n" +
" {\n" +
" \"id\": 102,\n" +
" \"firstName\": \"Amy\",\n" +
" \"lastName\": \"Perry\",\n" +
" \"email\": \"[email protected] \",\n" +
" \"phone\": \"(123)765-1920\",\n" +
" \"jobTitle\": \"Test engineer\",\n" +
" \"hireDate\": \"2019-08-01\",\n" +
" \"salary\": 4000.0,\n" +
" \"department\": \"Technical Group\"\n" +
" }\n" +
"]" ;
List < Employee > restDataEmployees = gson . fromJson ( restData , new TypeToken < List < Employee >>(){}. getType ());
System . out . println ( restDataEmployees );
}
}
class Employee {
public int id ;
public String firstName ;
public String lastName ;
public String email ;
public String phone ;
public String jobTitle ;
public String hireDate ;
public double salary ;
public String department ;
public int getId () {
return id ;
}
public void setId ( int id ) {
this . id = id ;
}
public String getFirstName () {
return firstName ;
}
public void setFirstName ( String firstName ) {
this . firstName = firstName ;
}
public String getLastName () {
return lastName ;
}
public void setLastName ( String lastName ) {
this . lastName = lastName ;
}
public String getEmail () {
return email ;
}
public void setEmail ( String email ) {
this . email = email ;
}
public String getPhone () {
return phone ;
}
public void setPhone ( String phone ) {
this . phone = phone ;
}
public String getJobTitle () {
return jobTitle ;
}
public void setJobTitle ( String jobTitle ) {
this . jobTitle = jobTitle ;
}
public String getHireDate () {
return hireDate ;
}
public void setHireDate ( String hireDate ) {
this . hireDate = hireDate ;
}
public double getSalary () {
return salary ;
}
public void setSalary ( double salary ) {
this . salary = salary ;
}
public String getDepartment () {
return department ;
}
public void setDepartment ( String department ) {
this . department = department ;
}
@Override
public String toString () {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
", jobTitle='" + jobTitle + '\'' +
", hireDate='" + hireDate + '\'' +
", salary=" + salary +
", department='" + department + '\'' +
'}' ;
}
}
1
[ Employee { id = 100 , firstName = ' John ' , lastName = ' Doe ' , email = ' john . doe @example.com ' , phone = ' ( 123 ) 456 - 7890 ' , jobTitle = ' Software engineer ' , hireDate = ' 2020 - 01 - 03 ' , salary = 3000.0 , department = ' Technical Group ' }, Employee { id = 101 , firstName = ' Tim ' , lastName = ' Brooks ' , email = ' tim . brooks @example.com ' , phone = ' ( 123 ) 921 - 7890 ' , jobTitle = ' Tech Lead ' , hireDate = ' 2018 - 05 - 06 ' , salary = 6000.0 , department = ' Technical Group ' }, Employee { id = 102 , firstName = ' Amy ' , lastName = ' Perry ' , email = ' amy . perry @example.com ' , phone = ' ( 123 ) 765 - 1920 ' , jobTitle = ' Test engineer ' , hireDate = ' 2019 - 08 - 01 ' , salary = 4000.0 , department = ' Technical Group ' }]
In the above output you could list of all employees in json string is mapped to List<Employee> as required.