Gson – Discussion

Discuss Gson ”; Previous Next Google Gson is an open source, Java-based library developed by Google. It facilitates serialization of Java objects to JSON and vice versa. This tutorial adopts a simple and intuitive way to describe the basic-to-advanced concepts of Google Gson and how to use its APIs. Print Page Previous Next Advertisements ”;

Gson – Tree Model

Gson – Tree Model ”; Previous Next Tree Model prepares an in-memory tree representation of the JSON document. It builds a tree of JsonObject nodes. It is a flexible approach and is analogous to DOM parser for XML. Create Tree from JSON JsonParser provides a pointer to the root node of the tree after reading the JSON. Root Node can be used to traverse the complete tree. Consider the following code snippet to get the root node of a provided JSON String. //Create an JsonParser instance JsonParser parser = new JsonParser(); String jsonString = “{“name”:”Mahesh Kumar”, “age”:21,”verified”:false,”marks”: [100,90,85]}”; //create tree from JSON JsonElement rootNode = parser.parse(jsonString); Traversing Tree Model Get each node using relative path to the root node while traversing the tree and process the data. The following code snippet shows how you can traverse a tree. JsonObject details = rootNode.getAsJsonObject(); JsonElement nameNode = details.get(“name”); System.out.println(“Name: ” +nameNode.getAsString()); JsonElement ageNode = details.get(“age”); System.out.println(“Age: ” + ageNode.getAsInt()); Example Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonPrimitive; public class GsonTester { public static void main(String args[]) { String jsonString = “{“name”:”Mahesh Kumar”, “age”:21,”verified”:false,”marks”: [100,90,85]}”; JsonParser parser = new JsonParser(); JsonElement rootNode = parser.parse(jsonString); if (rootNode.isJsonObject()) { JsonObject details = rootNode.getAsJsonObject(); JsonElement nameNode = details.get(“name”); System.out.println(“Name: ” +nameNode.getAsString()); JsonElement ageNode = details.get(“age”); System.out.println(“Age: ” + ageNode.getAsInt()); JsonElement verifiedNode = details.get(“verified”); System.out.println(“Verified: ” + (verifiedNode.getAsBoolean() ? “Yes”:”No”)); JsonArray marks = details.getAsJsonArray(“marks”); for (int i = 0; i < marks.size(); i++) { JsonPrimitive value = marks.get(i).getAsJsonPrimitive(); System.out.print(value.getAsInt() + ” “); } } } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output. Name: Mahesh Kumar Age: 21 Verified: No 100 90 85 Print Page Previous Next Advertisements ”;

Gson – Custom Type Adapters

Gson – Custom Type Adapters ”; Previous Next Gson performs the serialization/deserialization of objects using its inbuilt adapters. It also supports custom adapters. Let’s discuss how you can create a custom adapter and how you can use it. Create a Custom Adapter Create a custom adapter by extending the TypeAdapter class and passing it the type of object targeted. Override the read and write methods to do perform custom deserialization and serialization respectively. class StudentAdapter extends TypeAdapter<Student> { @Override public Student read(JsonReader reader) throws IOException { … } @Override public void write(JsonWriter writer, Student student) throws IOException { } } Register the Custom Adapter Register the custom adapter using GsonBuilder and create a Gson instance using GsonBuilder. GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Student.class, new StudentAdapter()); Gson gson = builder.create(); Use the Adapter Gson will now use the custom adapter to convert Json text to object and vice versa. String jsonString = “{“name”:”Mahesh”, “rollNo”:1}”; Student student = gson.fromJson(jsonString, Student.class); System.out.println(student); jsonString = gson.toJson(student); System.out.println(jsonString); Example Let”s see an example of custom type adapter in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import java.io.IOException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; public class GsonTester { public static void main(String args[]) { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Student.class, new StudentAdapter()); builder.setPrettyPrinting(); Gson gson = builder.create(); String jsonString = “{“name”:”Mahesh”, “rollNo”:1}”; Student student = gson.fromJson(jsonString, Student.class); System.out.println(student); jsonString = gson.toJson(student); System.out.println(jsonString); } } class StudentAdapter extends TypeAdapter<Student> { @Override public Student read(JsonReader reader) throws IOException { Student student = new Student(); reader.beginObject(); String fieldname = null; while (reader.hasNext()) { JsonToken token = reader.peek(); if (token.equals(JsonToken.NAME)) { //get the current token fieldname = reader.nextName(); } if (“name”.equals(fieldname)) { //move to next token token = reader.peek(); student.setName(reader.nextString()); } if(“rollNo”.equals(fieldname)) { //move to next token token = reader.peek(); student.setRollNo(reader.nextInt()); } } reader.endObject(); return student; } @Override public void write(JsonWriter writer, Student student) throws IOException { writer.beginObject(); writer.name(“name”); writer.value(student.getName()); writer.name(“rollNo”); writer.value(student.getRollNo()); writer.endObject(); } } class Student { private int rollNo; private String name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return “Student[ name = “+name+”, roll no: “+rollNo+ “]”; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output. Student[ name = Mahesh, roll no: 1] { “name”: “Mahesh”, “rollNo”: 1 } Print Page Previous Next Advertisements ”;

Excluding fields from Serialization

Gson – Excluding fields from Serialization ”; Previous Next By default, GSON excludes transient and static fields from the serialization/deserialization process. Let’s take a look at the following example. Example Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTester { public static void main(String args[]) { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); Student student = new Student(); student.setRollNo(1); student.setName(“Mahesh Kumar”); student.setVerified(true); student.setId(1); student.className = “VI”; String jsonString = gson.toJson(student); System.out.println(jsonString); } } class Student { private int rollNo; private String name; private boolean verified; private transient int id; public static String className; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setVerified(boolean verified) { this.verified = verified; } public boolean isVerified() { return verified; } public int getId() { return id; } public void setId(int id) { this.id = id; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output {“rollNo”:1,”name”:”Mahesh Kumar”,”verified”:true} Using excludeFieldsWithModifiers GsonBuilder provides control over excluding fields with particular modifier using excludeFieldsWithModifiers() method from serialization/deserialization process. See the following example. Example Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import java.lang.reflect.Modifier; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTester { public static void main(String args[]) { GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithModifiers(Modifier.TRANSIENT); Gson gson = builder.create(); Student student = new Student(); student.setRollNo(1); student.setName(“Mahesh Kumar”); student.setVerified(true); student.setId(1); student.className = “VI”; String jsonString = gson.toJson(student); System.out.println(jsonString); } } class Student { private int rollNo; private String name; private boolean verified; private transient int id; public static String className; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setVerified(boolean verified) { this.verified = verified; } public boolean isVerified() { return verified; } public int getId() { return id; } public void setId(int id) { this.id = id; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output {“rollNo”:1,”name”:”Mahesh Kumar”,”verified”:true,”className”:”VI”} Using @Expose Annotation Gson provides @Expose annotation to control the Json serialization/deserialization of a class based on its scope. Consider the following class with a variable having @Expose support. In this class, name and rollno variables are to be exposed for serialization. Then we”ve used the GsonBuilder.excludeFieldsWithoutExposeAnnotation() method to indicate that only exposed variables are to be serialized/deserialized. See the following example. Example Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; public class GsonTester { public static void main(String args[]) { GsonBuilder builder = new GsonBuilder(); builder.excludeFieldsWithoutExposeAnnotation(); Gson gson = builder.create(); Student student = new Student(); student.setRollNo(1); student.setName(“Mahesh Kumar”); student.setVerified(true); student.setId(1); student.className = “VI”; String jsonString = gson.toJson(student); System.out.println(jsonString); } } class Student { @Expose private int rollNo; @Expose private String name; private boolean verified; private int id; public static String className; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setVerified(boolean verified) { this.verified = verified; } public boolean isVerified() { return verified; } public int getId() { return id; } public void setId(int id) { this.id = id; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output {“rollNo”:1,”name”:”Mahesh Kumar”} Print Page Previous Next Advertisements ”;

Gson – Environment Setup

Gson – Environment Setup ”; Previous Next Local Environment Setup If you still want to set up a local environment for Java programming language, then this section will guide you on how to download and set up Java on your machine. Please follow the steps given below, to set up the environment. Java SE is freely available from the link Download Java. So you download a version based on your operating system. Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set the environment variables to point to their correct installation directories. Setting up the Path in Windows 2000/XP Assuming you have installed Java in c:Program Filesjavajdk directory − Right-click on ”My Computer” and select ”Properties”. Click on the ”Environment variables” button under the ”Advanced” tab. Next, alter the ”Path” variable so that it also contains the path to the Java executable. For example, if the path is currently set to ”C:WINDOWSSYSTEM32”, then change your path to read ”C:WINDOWSSYSTEM32;c:Program Filesjavajdkbin”. Setting up the Path in Windows 95 / 98 / ME Assuming you have installed Java in c:Program Filesjavajdk directory − Edit the ”C:autoexec.bat” file and add the following line at the end: ”SET PATH=%PATH%;C:Program Filesjavajdkbin” Setting up the Path for Linux, UNIX, Solaris, FreeBSD The environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this. For example, if you use bash as your shell, then you would add the following line to the end of your ”.bashrc: export PATH=/path/to/java:$PATH” Popular Java Editors To write your Java programs, you will need a text editor. There are quite a few sophisticated IDEs available in the market. But for now, you can consider one of the following − Notepad − On Windows, you can use any simple text editor like Notepad (Recommended for this tutorial) or TextPad. Netbeans − It is a Java IDE that is open-source and free which can be downloaded from https://netbeans.org/index.html. Eclipse − It is also a Java IDE developed by the Eclipse open-source community and can be downloaded from https://www.eclipse.org/. Download Gson Archive Download the latest version of Gson jar file from gson-2.3.1.jar. At the time of writing this tutorial, we downloaded gson-2.3.1.jar and copied it into C:>gson folder. OS Archive name Windows gson-2.3.1.jar Linux gson-2.3.1.jar Mac gson-2.3.1.jar Set Gson Environment Set the GSON_HOME environment variable to point to the base directory location where Gson jar is stored on your machine. OS Output Windows Set the environment variable GSON_HOME to C:gson Linux export GSON_HOME=/usr/local/gson Mac export GSON_HOME=/Library/gson Set CLASSPATH variable Set the CLASSPATH environment variable to point to the Gson jar location. OS Output Windows Set the environment variable CLASSPATH to %CLASSPATH%;%GSON_HOME%gson-2.3.1.jar;.; Linux export CLASSPATH=$CLASSPATH:$GSON_HOME/gson-2.3.1.jar:. Mac export CLASSPATH=$CLASSPATH:$GSON_HOME/gson-2.3.1.jar:. Print Page Previous Next Advertisements ”;

Gson – Streaming

Gson – Streaming ”; Previous Next Streaming API is used to read JSON token by token. It reads and writes JSON content as discrete events. JsonReader and JsonWriter read/write the data as token, referred as JsonToken. It is the most powerful approach among the three approaches to process JSON. It has the lowest overhead and it is quite fast in read/write operations. It is analogous to Stax parser for XML. In this chapter, we will showcase the usage of GSON streaming APIs to read JSON data. Streaming API works with the concept of token and every details of Json is to be handled carefully. //create JsonReader object and pass it the json source or json text. JsonReader reader = new JsonReader(new StringReader(jsonString)); //start reading json reader.beginObject(); //get the next token JsonToken token = reader.peek(); //check the type of the token if (token.equals(JsonToken.NAME)) { //get the current token fieldname = reader.nextName(); } Example Let”s see JsonReader in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File – GsonTester.java import java.io.IOException; import java.io.StringReader; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; public class GsonTester { public static void main(String args[]) { String jsonString = “{“name”:”Mahesh Kumar”, “age”:21,”verified”:false,”marks”: [100,90,85]}”; JsonReader reader = new JsonReader(new StringReader(jsonString)); try { handleJsonObject(reader); } catch (IOException e) { e.printStackTrace(); } } private static void handleJsonObject(JsonReader reader) throws IOException { reader.beginObject(); String fieldname = null; while (reader.hasNext()) { JsonToken token = reader.peek(); if (token.equals(JsonToken.BEGIN_ARRAY)) { System.out.print(“Marks [ “); handleJsonArray(reader); System.out.print(“]”); } else if (token.equals(JsonToken.END_OBJECT)) { reader.endObject(); return; } else { if (token.equals(JsonToken.NAME)) { //get the current token fieldname = reader.nextName(); } if (“name”.equals(fieldname)) { //move to next token token = reader.peek(); System.out.println(“Name: “+reader.nextString()); } if(“age”.equals(fieldname)) { //move to next token token = reader.peek(); System.out.println(“Age:” + reader.nextInt()); } if(“verified”.equals(fieldname)) { //move to next token token = reader.peek(); System.out.println(“Verified:” + reader.nextBoolean()); } } } } private static void handleJsonArray(JsonReader reader) throws IOException { reader.beginArray(); String fieldname = null; while (true) { JsonToken token = reader.peek(); if (token.equals(JsonToken.END_ARRAY)) { reader.endArray(); break; } else if (token.equals(JsonToken.BEGIN_OBJECT)) { handleJsonObject(reader); } else if (token.equals(JsonToken.END_OBJECT)) { reader.endObject(); } else { System.out.print(reader.nextInt() + ” “); } } } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output. Name: Mahesh Kumar Age:21 Verified:false Marks [ 100 90 85 ] Print Page Previous Next Advertisements ”;

Gson – Useful Resources

Gson – Useful Resources ”; Previous Next The following resources contain additional information on Gson JSON Processor library. Please use them to get more in-depth knowledge on this topic. Useful Links on Gson Google Gson Project Home − Google Gson Project Home page Google Gson Wiki − Wikipedia Reference for Google Gson Useful Books on Gson To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;

Gson – Object Serialization

Gson – Object Serialization ”; Previous Next Let”s serialize a Java object to a Json file and then read that Json file to get the object back. In this example, we”ve created a Student class. We”ll create a student.json file which will have a json representation of Student object. Example Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File – GsonTester.java import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class GsonTester { public static void main(String args[]) { GsonTester tester = new GsonTester(); try { Student student = new Student(); student.setAge(10); student.setName(“Mahesh”); tester.writeJSON(student); Student student1 = tester.readJSON(); System.out.println(student1); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } private void writeJSON(Student student) throws IOException { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); FileWriter writer = new FileWriter(“student.json”); writer.write(gson.toJson(student)); writer.close(); } private Student readJSON() throws FileNotFoundException { GsonBuilder builder = new GsonBuilder(); Gson gson = builder.create(); BufferedReader bufferedReader = new BufferedReader( new FileReader(“student.json”)); Student student = gson.fromJson(bufferedReader, Student.class); return student; } } class Student { private String name; private int age; public Student(){} public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return “Student [ name: “+name+”, age: “+ age+ ” ]”; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the Output Student [ name: Mahesh, age: 10 ] Print Page Previous Next Advertisements ”;

Gson – Overview

Gson – Overview ”; Previous Next Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google. The following points highlight why you should be using this library − Standardized − Gson is a standardized library that is managed by Google. Efficient − It is a reliable, fast, and efficient extension to the Java standard library. Optimized − The library is highly optimized. Support Generics − It provides extensive support for generics. Supports complex inner classes − It supports complex objects with deep inheritance hierarchies. Features of Gson Here is a list of some of the most prominent features of Gson − Easy to use − Gson API provides a high-level facade to simplify commonly used use-cases. No need to create mapping − Gson API provides default mapping for most of the objects to be serialized. Performance − Gson is quite fast and is of low memory footprint. It is suitable for large object graphs or systems. Clean JSON − Gson creates a clean and compact JSON result which is easy to read. No Dependency − Gson library does not require any other library apart from JDK. Open Source − Gson library is open source; it is freely available. Three Ways of Processing JSON Gson provides three alternative ways to process JSON − Streaming API It reads and writes JSON content as discrete events. JsonReader and JsonWriter read/write the data as token, referred as JsonToken. It is the most powerful approach among the three approaches to process JSON. It has the lowest overhead and it is quite fast in read/write operations. It is analogous to Stax parser for XML. Tree Model It prepares an in-memory tree representation of the JSON document. It builds a tree of JsonObject nodes. It is a flexible approach and is analogous to DOM parser for XML. Data Binding It converts JSON to and from POJO (Plain Old Java Object) using property accessor. Gson reads/writes JSON using data type adapters. It is analogous to JAXB parser for XML. Print Page Previous Next Advertisements ”;

Gson – Serializing Inner Classes

Gson – Serializing Inner Classes ”; Previous Next In this chapter, we will explain serialization/deserialization of classes having inner classes. Nested Inner Class example Student student = new Student(); student.setRollNo(1); Student.Name name = student.new Name(); name.firstName = “Mahesh”; name.lastName = “Kumar”; student.setName(name); //serialize inner class object String nameString = gson.toJson(name); System.out.println(nameString); //deserialize inner class object name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); Example Let”s see an example of serialization/de-serialization of class with an inner class in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import com.google.gson.Gson; public class GsonTester { public static void main(String args[]) { Student student = new Student(); student.setRollNo(1); Student.Name name = student.new Name(); name.firstName = “Mahesh”; name.lastName = “Kumar”; student.setName(name); Gson gson = new Gson(); String jsonString = gson.toJson(student); System.out.println(jsonString); student = gson.fromJson(jsonString, Student.class); System.out.println(“Roll No: “+ student.getRollNo()); System.out.println(“First Name: “+ student.getName().firstName); System.out.println(“Last Name: “+ student.getName().lastName); String nameString = gson.toJson(name); System.out.println(nameString); name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); System.out.println(“First Name: “+ name.firstName); System.out.println(“Last Name: “+ name.lastName); } } class Student { private int rollNo; private Name name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } class Name { public String firstName; public String lastName; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output. {“rollNo”:1,”name”:{“firstName”:”Mahesh”,”lastName”:”Kumar”}} Roll No: 1 First Name: Mahesh Last Name: Kumar {“firstName”:”Mahesh”,”lastName”:”Kumar”} class Student$Name First Name: Mahesh Last Name: Kumar Nested Static Inner Class Example Student student = new Student(); student.setRollNo(1); Student.Name name = new Student.Name(); name.firstName = “Mahesh”; name.lastName = “Kumar”; student.setName(name); //serialize static inner class object String nameString = gson.toJson(name); System.out.println(nameString); //deserialize static inner class object name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); Example Let”s see an example of serialization/de-serialization of class with a static inner class in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE. File − GsonTester.java import com.google.gson.Gson; public class GsonTester { public static void main(String args[]) { Student student = new Student(); student.setRollNo(1); Student.Name name = new Student.Name(); name.firstName = “Mahesh”; name.lastName = “Kumar”; student.setName(name); Gson gson = new Gson(); String jsonString = gson.toJson(student); System.out.println(jsonString); student = gson.fromJson(jsonString, Student.class); System.out.println(“Roll No: “+ student.getRollNo()); System.out.println(“First Name: “+ student.getName().firstName); System.out.println(“Last Name: “+ student.getName().lastName); String nameString = gson.toJson(name); System.out.println(nameString); name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); System.out.println(“First Name: “+ name.firstName); System.out.println(“Last Name: “+ name.lastName); } } class Student { private int rollNo; private Name name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } static class Name { public String firstName; public String lastName; } } Verify the result Compile the classes using javac compiler as follows − C:GSON_WORKSPACE>javac GsonTester.java Now run the GsonTester to see the result − C:GSON_WORKSPACE>java GsonTester Verify the output. {“rollNo”:1,”name”:{“firstName”:”Mahesh”,”lastName”:”Kumar”}} Roll No: 1 First Name: Mahesh Last Name: Kumar {“firstName”:”Mahesh”,”lastName”:”Kumar”} class Student$Name First Name: Mahesh Last Name: Kumar Print Page Previous Next Advertisements ”;