Introduction to GSON
GSON is simple to use!
And very powerful !
GSON is a java library created by Google to convert Java objects into their Json representation and vice versa.
We may get JSON string from a REST api and want to convert them in to Java objects for further processing.
Other way round we have populated Java object and want to send them as JSON string so that other workloads can process them.
Above 2 operations are performed multiple times in various projects hence GSON is used widely and has
become very popular among developers.
Personally I have used it many times in my Spring Boot applications and have found it very useful.
How to use JSON library?
You can add GSON to your project by including following dependency(or which ever is latest)
1
2
3
4
5
< dependency >
< groupId > com . google . code . gson </ groupId >
< artifactId > gson </ artifactId >
< version > 2.8.5 </ version >
</ dependency >
Create a JSON string from Java Object using GSON
Below code shows how Employee object is converted into a JSON string
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
package com.harishgowda.demo ;
import com.google.gson.Gson ;
public class App {
public static void main ( String [] args ) {
Gson gson = new Gson ();
Employee emp = new Employee ( 1 , "Harish" );
String nums = gson . toJson ( emp );
System . out . println ( nums );
}
}
class Employee {
public int empid ;
public String empName ;
public Employee ( int empid , String empName ) {
this . empid = empid ;
this . empName = empName ;
}
public int getEmpid () {
return empid ;
}
public void setEmpid ( int empid ) {
this . empid = empid ;
}
public String getEmpName () {
return empName ;
}
public void setEmpName ( String empName ) {
this . empName = empName ;
}
}
Output is:
1
{ "empid" : 1 , "empName" : "Harish" }
Create a JSON object from the given string using GSON
To convert JSON to a Employee object, invoke fromJson() method of the Gson class
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
package com.harishgowda.demo ;
import com.google.gson.Gson ;
public class App {
public static void main ( String [] args ) {
Gson gson = new Gson ();
String data = "{\"empid\":1,\"empName\":\"Harish\"}" ;
Employee emp = gson . fromJson ( data , Employee . class );
System . out . println ( emp . toString ());
}
}
class Employee {
public int empid ;
public String empName ;
public Employee ( int empid , String empName ) {
this . empid = empid ;
this . empName = empName ;
}
public int getEmpid () {
return empid ;
}
public void setEmpid ( int empid ) {
this . empid = empid ;
}
public String getEmpName () {
return empName ;
}
public void setEmpName ( String empName ) {
this . empName = empName ;
}
@Override
public String toString () {
return "Employee{" +
"empid=" + empid +
", empName='" + empName + '\'' +
'}' ;
}
}
Output:
1
Employee { empid = 1 , empName = ' Harish ' }
Summary
GSON helps you to easily parse JSON data from external REST APIs or files into Java objects.
GSON helps you to serialize Java objects into JSON strings which can be easily consumed by other applications.
GSON can be helpful in testing and debugging applications that use JSON data. You can use GSON API to convert JSON strings into Java objects, making it easier to examine the data and debug any issues.
References:
Gson Docs