Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then, toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.
Following is the declaration for com.google.gson.Gson class −
public final class Gson extends Object
Sr.No | Constructor & Description |
---|---|
1 |
Gson() Constructs a Gson object with default configuration. |
Sr.No | Method & Description |
---|---|
1 |
<T> T fromJson(JsonElement json, Class<T> classOfT) This method deserializes the Json read from the specified parse tree into an object of the specified type. |
2 |
<T> T fromJson(JsonElement json, Type typeOfT) This method deserializes the Json read from the specified parse tree into an object of the specified type. |
3 |
<T> T fromJson(JsonReader reader, Type typeOfT) Reads the next JSON value from reader and convert it to an object of type typeOfT. |
4 |
<T> T fromJson(Reader json, Class<T> classOfT) This method deserializes the Json read from the specified reader into an object of the specified class. |
5 |
<T> T fromJson(Reader json, Type typeOfT) This method deserializes the Json read from the specified reader into an object of the specified type. |
6 |
<T> T fromJson(String json, Class<T> classOfT) This method deserializes the specified Json into an object of the specified class. |
7 |
<T> T fromJson(String json, Type typeOfT) This method deserializes the specified Json into an object of the specified type. |
8 |
<T> TypeAdapter<T> getAdapter(Class<T> type) Returns the type adapter for type. |
9 |
<T> TypeAdapter<T> getAdapter(TypeToken<T> type) Returns the type adapter for type. |
10 |
<T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) This method is used to get an alternate type adapter for the specified type. |
11 |
String toJson(JsonElement jsonElement) Converts a tree of JsonElements into its equivalent JSON representation. |
12 |
void toJson(JsonElement jsonElement, Appendable writer) Writes out the equivalent JSON for a tree of JsonElements. |
13 |
void toJson(JsonElement jsonElement, JsonWriter writer) Writes the JSON for jsonElement to writer. |
14 |
String toJson(Object src) This method serializes the specified object into its equivalent Json representation. |
15 |
void toJson(Object src, Appendable writer) This method serializes the specified object into its equivalent Json representation. |
16 |
String toJson(Object src, Type typeOfSrc) This method serializes the specified object, including those of generic types, into its equivalent Json representation. |
17 |
void toJson(Object src, Type typeOfSrc, Appendable writer) This method serializes the specified object, including those of generic types, into its equivalent Json representation. |
18 |
void toJson(Object src, Type typeOfSrc, JsonWriter writer) Writes the JSON representation of src of type typeOfSrc to writer. |
19 |
JsonElement toJsonTree(Object src) This method serializes the specified object into its equivalent representation as a tree of JsonElements. |
20 |
JsonElement toJsonTree(Object src, Type typeOfSrc) This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements. |
21 |
String toString() |
This class inherits methods from the following class −
Create the following Java program using any editor of your choice, and save it at, say, 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) { String jsonString = "{\"name\":\"Mahesh\", \"age\":21}"; GsonBuilder builder = new GsonBuilder(); builder.setPrettyPrinting(); Gson gson = builder.create(); Student student = gson.fromJson(jsonString, Student.class); System.out.println(student); jsonString = gson.toJson(student); System.out.println(jsonString); } } 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+ " ]"; } }
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: 21 ] { "name" : "Mahesh", "age" : 21 }