Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Google Gson Interview Questions and Answers

Freshers / Beginner level questions & answers

Ques 1. What is Google-Gson?

Google Gson is a Java library that can be used to convert Java Objects into respective JSON format. In another way, it can used to convert the JSON into equivalent java objects. There are some other java libraries also capable of doing this conversion, but Gson stands among very few which do not require any pre-annotated java classes OR sourcecode of java classes in any way.
Gson also support the old java classes which had not support of generics in them for type information. It just work with these legacy classes smoothly.

Is it helpful? Add Comment View Comments
 

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.

Is it helpful? Add Comment View Comments
 

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"]

}

Is it helpful? Add Comment View Comments
 

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]]

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook