Ques 2. What are the two ways to create Gson objects?
Gson object can be created in two ways. First way gives you a quick Gson object ready for faster coding, while second way uses GsonBuilder to build a more sophisticated Gson object.
//First way to create a Gson object for faster coding
Gson gson = new Gson();
//Second way to create a Gson object using GsonBuilder
Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.serializeNulls()
.create();
When using GsonBuilder, there are plenty of other useful options you can provide to Gson object.
Ques 3. How to convert Java objects to JSON format?
To convert the java objects to JSON format, use toJson() method.
Employee employee = new Employee();
employee.setId(1);
employee.setFirstName("Arindam");
employee.setLastName("Ghosh");
employee.setRoles(Arrays.asList("FINANCE", "MANAGER"));
Gson gson = new Gson();
System.out.println(gson.toJson(employee));
Output:
{
"id":1,
"firstName":"Rob",
"lastName":"Bosch",
"roles":["FIINANCE","MANAGER"]
}
Ques 4. How to convert JSON to Java Objects?
To convert the JSON to java object, use fromJson() method.
Gson gson = new Gson();
System.out.println(
gson.fromJson("{'id':1,'firstName':'Arindam','lastName':'Ghosh','roles':['FINANCE','MANAGER']}",
Employee.class));
Output:
Employee [id=1, firstName=Arindam, lastName=Ghosh, roles=[FINANCE, MANAGER]]
Ques 5. What is Instance Creator? Why and when do we require this?
In most of the cases, Gson library is smart enough to create instances even if any class does not provide default no-args constructor. But, if you found any problem using a class having no no-args constructor, you can use InstanceCreator support. You need to register the InstanceCreator of a java class type with Gson first before using it.
For example, Department.java does not have any default constructor.
public class Department
{
public Department(String deptName)
{
this.deptName = deptName;
}
private String deptName;
public String getDeptName()
{
return deptName;
}
public void setDeptName(String deptName)
{
this.deptName = deptName;
}
@Override
public String toString()
{
return "Department [deptName="+deptName+"]";
}
}
And our Employee class has reference of Department as:
public class Employee
{
private Integer id;
private String firstName;
private String lastName;
private List<String> roles;
private Department department; //Department reference
//Other setters and getters
}
To use Department class correctly, you need to register an InstanceCreator for Department.java as below:
class DepartmentInstanceCreator implements InstanceCreator<Department> {
public Department createInstance(Type type)
{
return new Department("None");
}
}
//Now use the above InstanceCreator as below
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Department.class, new DepartmentInstanceCreator());
Gson gson = gsonBuilder.create();
System.out.println(
gson.fromJson("{'id':1,'firstName':'Arindam','lastName':'Ghosh','roles':['FINANCE','MANAGER'],'department':{'deptName':'Finance'}}",
Employee.class));
Output:
Employee [id=1, firstName=Arindam, lastName=Ghosh, roles=[FINANCE, MANAGER], department=Department [deptName=Finance]]