Gson - Class

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 

Gson()
Constructs a Gson object with default configuration.
<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.
<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.
<T> T fromJson(JsonReader reader, Type typeOfT)
Reads the next JSON value from reader and convert it to an object of type typeOfT.
<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.
<T> T fromJson(Reader json, Type typeOfT)
This method deserializes the Json read from the specified reader into an object of the specified type.
<T> T fromJson(String json, Class<T> classOfT)
This method deserializes the specified Json into an object of the specified class.
<T> T fromJson(String json, Type typeOfT)
This method deserializes the specified Json into an object of the specified type.
<T> TypeAdapter<T> getAdapter(Class<T> type)
Returns the type adapter for type.
<T> TypeAdapter<T> getAdapter(TypeToken<T> type)
Returns the type adapter for type.
<T> TypeAdapter<T> getDelegateAdapter(TypeAdapterFactory skipPast, TypeToken<T> type)
This method is used to get an alternate type adapter for the specified type.
String toJson(JsonElement jsonElement)
Converts a tree of JsonElements into its equivalent JSON representation.
void toJson(JsonElement jsonElement, Appendable writer)
Writes out the equivalent JSON for a tree of JsonElements.
void toJson(JsonElement jsonElement, JsonWriter writer)
Writes the JSON for jsonElement to writer.
String toJson(Object src)
This method serializes the specified object into its equivalent Json representation.
void toJson(Object src, Appendable writer)
This method serializes the specified object into its equivalent Json representation.
String toJson(Object src, Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its equivalent Json representation.
void toJson(Object src, Type typeOfSrc, Appendable writer)
This method serializes the specified object, including those of generic types, into its equivalent Json representation.
void toJson(Object src, Type typeOfSrc, JsonWriter writer)
Writes the JSON representation of src of type typeOfSrc to writer.
JsonElement toJsonTree(Object src)
This method serializes the specified object into its equivalent representation as a tree of JsonElements.
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.
String toString()
This class inherits methods from the following class −
java.lang.Object
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 
}

 Print
 Add Notes
 Bookmark this page