com.google.gson
Class Gson

java.lang.Object
  extended by com.google.gson.Gson

public final class Gson
extends java.lang.Object

This is the main class for using Gson. Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it.

You can create a Gson instance by invoking new Gson() if the default configuration is all you need. You can also use GsonBuilder to build a Gson instance with various configuration options such as versioning support, pretty printing, custom JsonSerializers, JsonDeserializers, and InstanceCreators.

Here is an example of how Gson is used for a simple Class:

 Gson gson = new Gson(); // Or use new GsonBuilder().create();
 MyType target = new MyType();
 String json = gson.toJson(target); // serializes target to Json
 MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
 

If the object that your are serializing/deserializing is a ParameterizedType (i.e. contains at least one type parameter and may be an array) then you must use the toJson(Object, Type) or fromJson(String, Type) method. Here is an example for serializing and deserialing a ParameterizedType:

 Type listType = new TypeToken<List<String>>() {}.getType();
 List<String> target = new LinkedList<String>();
 target.add("blah");

 Gson gson = new Gson();
 String json = gson.toJson(target, listType);
 List<String> target2 = gson.fromJson(json, listType);
 

See the Gson User Guide for a more complete set of examples.

Author:
Inderjeet Singh, Joel Leitch
See Also:
TypeToken

Constructor Summary
Gson()
          Constructs a Gson object with default configuration.
 
Method Summary
<T> T
fromJson(JsonElement json, java.lang.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, java.lang.reflect.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, java.lang.reflect.Type typeOfT)
          Reads the next JSON value from reader and convert it to an object of type typeOfT.
<T> T
fromJson(java.io.Reader json, java.lang.Class<T> classOfT)
          This method deserializes the Json read from the specified reader into an object of the specified class.
<T> T
fromJson(java.io.Reader json, java.lang.reflect.Type typeOfT)
          This method deserializes the Json read from the specified reader into an object of the specified type.
<T> T
fromJson(java.lang.String json, java.lang.Class<T> classOfT)
          This method deserializes the specified Json into an object of the specified class.
<T> T
fromJson(java.lang.String json, java.lang.reflect.Type typeOfT)
          This method deserializes the specified Json into an object of the specified type.
 java.lang.String toJson(JsonElement jsonElement)
          Converts a tree of JsonElements into its equivalent JSON representation.
 void toJson(JsonElement jsonElement, java.lang.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.
 java.lang.String toJson(java.lang.Object src)
          This method serializes the specified object into its equivalent Json representation.
 void toJson(java.lang.Object src, java.lang.Appendable writer)
          This method serializes the specified object into its equivalent Json representation.
 java.lang.String toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc)
          This method serializes the specified object, including those of generic types, into its equivalent Json representation.
 void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, java.lang.Appendable writer)
          This method serializes the specified object, including those of generic types, into its equivalent Json representation.
 void toJson(java.lang.Object src, java.lang.reflect.Type typeOfSrc, JsonWriter writer)
          Writes the JSON representation of src of type typeOfSrc to writer.
 JsonElement toJsonTree(java.lang.Object src)
          This method serializes the specified object into its equivalent representation as a tree of JsonElements.
 JsonElement toJsonTree(java.lang.Object src, java.lang.reflect.Type typeOfSrc)
          This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Gson

public Gson()
Constructs a Gson object with default configuration. The default configuration has the following settings:

Method Detail

toJsonTree

public JsonElement toJsonTree(java.lang.Object src)
This method serializes the specified object into its equivalent representation as a tree of JsonElements. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJsonTree(Object, Type) instead.

Parameters:
src - the object for which Json representation is to be created setting for Gson
Returns:
Json representation of src.
Since:
1.4

toJsonTree

public JsonElement toJsonTree(java.lang.Object src,
                              java.lang.reflect.Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements. This method must be used if the specified object is a generic type. For non-generic objects, use toJsonTree(Object) instead.

Parameters:
src - the object for which JSON representation is to be created
typeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
 
Returns:
Json representation of src
Since:
1.4

toJson

public java.lang.String toJson(java.lang.Object src)
This method serializes the specified object into its equivalent Json representation. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJson(Object, Type) instead. If you want to write out the object to a Writer, use toJson(Object, Appendable) instead.

Parameters:
src - the object for which Json representation is to be created setting for Gson
Returns:
Json representation of src.

toJson

public java.lang.String toJson(java.lang.Object src,
                               java.lang.reflect.Type typeOfSrc)
This method serializes the specified object, including those of generic types, into its equivalent Json representation. This method must be used if the specified object is a generic type. For non-generic objects, use toJson(Object) instead. If you want to write out the object to a Appendable, use toJson(Object, Type, Appendable) instead.

Parameters:
src - the object for which JSON representation is to be created
typeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
 
Returns:
Json representation of src

toJson

public void toJson(java.lang.Object src,
                   java.lang.Appendable writer)
            throws JsonIOException
This method serializes the specified object into its equivalent Json representation. This method should be used when the specified object is not a generic type. This method uses Object.getClass() to get the type for the specified object, but the getClass() loses the generic type information because of the Type Erasure feature of Java. Note that this method works fine if the any of the object fields are of generic type, just the object itself should not be of a generic type. If the object is of generic type, use toJson(Object, Type, Appendable) instead.

Parameters:
src - the object for which Json representation is to be created setting for Gson
writer - Writer to which the Json representation needs to be written
Throws:
JsonIOException - if there was a problem writing to the writer
Since:
1.2

toJson

public void toJson(java.lang.Object src,
                   java.lang.reflect.Type typeOfSrc,
                   java.lang.Appendable writer)
            throws JsonIOException
This method serializes the specified object, including those of generic types, into its equivalent Json representation. This method must be used if the specified object is a generic type. For non-generic objects, use toJson(Object, Appendable) instead.

Parameters:
src - the object for which JSON representation is to be created
typeOfSrc - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
 
writer - Writer to which the Json representation of src needs to be written.
Throws:
JsonIOException - if there was a problem writing to the writer
Since:
1.2

toJson

public void toJson(java.lang.Object src,
                   java.lang.reflect.Type typeOfSrc,
                   JsonWriter writer)
            throws JsonIOException
Writes the JSON representation of src of type typeOfSrc to writer.

Throws:
JsonIOException - if there was a problem writing to the writer

toJson

public java.lang.String toJson(JsonElement jsonElement)
Converts a tree of JsonElements into its equivalent JSON representation.

Parameters:
jsonElement - root of a tree of JsonElements
Returns:
JSON String representation of the tree
Since:
1.4

toJson

public void toJson(JsonElement jsonElement,
                   java.lang.Appendable writer)
            throws JsonIOException
Writes out the equivalent JSON for a tree of JsonElements.

Parameters:
jsonElement - root of a tree of JsonElements
writer - Writer to which the Json representation needs to be written
Throws:
JsonIOException - if there was a problem writing to the writer
Since:
1.4

toJson

public void toJson(JsonElement jsonElement,
                   JsonWriter writer)
            throws JsonIOException
Writes the JSON for jsonElement to writer.

Throws:
JsonIOException - if there was a problem writing to the writer

fromJson

public <T> T fromJson(java.lang.String json,
                      java.lang.Class<T> classOfT)
           throws JsonSyntaxException
This method deserializes the specified Json into an object of the specified class. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(String, Type). If you have the Json in a Reader instead of a String, use fromJson(Reader, Class) instead.

Type Parameters:
T - the type of the desired object
Parameters:
json - the string from which the object is to be deserialized
classOfT - the class of T
Returns:
an object of type T from the string
Throws:
JsonSyntaxException - if json is not a valid representation for an object of type classOfT

fromJson

public <T> T fromJson(java.lang.String json,
                      java.lang.reflect.Type typeOfT)
           throws JsonSyntaxException
This method deserializes the specified Json into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(String, Class) instead. If you have the Json in a Reader instead of a String, use fromJson(Reader, Type) instead.

Type Parameters:
T - the type of the desired object
Parameters:
json - the string from which the object is to be deserialized
typeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
 
Returns:
an object of type T from the string
Throws:
JsonParseException - if json is not a valid representation for an object of type typeOfT
JsonSyntaxException - if json is not a valid representation for an object of type

fromJson

public <T> T fromJson(java.io.Reader json,
                      java.lang.Class<T> classOfT)
           throws JsonSyntaxException,
                  JsonIOException
This method deserializes the Json read from the specified reader into an object of the specified class. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(Reader, Type). If you have the Json in a String form instead of a Reader, use fromJson(String, Class) instead.

Type Parameters:
T - the type of the desired object
Parameters:
json - the reader producing the Json from which the object is to be deserialized.
classOfT - the class of T
Returns:
an object of type T from the string
Throws:
JsonIOException - if there was a problem reading from the Reader
JsonSyntaxException - if json is not a valid representation for an object of type
Since:
1.2

fromJson

public <T> T fromJson(java.io.Reader json,
                      java.lang.reflect.Type typeOfT)
           throws JsonIOException,
                  JsonSyntaxException
This method deserializes the Json read from the specified reader into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(Reader, Class) instead. If you have the Json in a String form instead of a Reader, use fromJson(String, Type) instead.

Type Parameters:
T - the type of the desired object
Parameters:
json - the reader producing Json from which the object is to be deserialized
typeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
 
Returns:
an object of type T from the json
Throws:
JsonIOException - if there was a problem reading from the Reader
JsonSyntaxException - if json is not a valid representation for an object of type
Since:
1.2

fromJson

public <T> T fromJson(JsonReader reader,
                      java.lang.reflect.Type typeOfT)
           throws JsonIOException,
                  JsonSyntaxException
Reads the next JSON value from reader and convert it to an object of type typeOfT. Since Type is not parameterized by T, this method is type unsafe and should be used carefully

Throws:
JsonIOException - if there was a problem writing to the Reader
JsonSyntaxException - if json is not a valid representation for an object of type

fromJson

public <T> T fromJson(JsonElement json,
                      java.lang.Class<T> classOfT)
           throws JsonSyntaxException
This method deserializes the Json read from the specified parse tree into an object of the specified type. It is not suitable to use if the specified class is a generic type since it will not have the generic type information because of the Type Erasure feature of Java. Therefore, this method should not be used if the desired type is a generic type. Note that this method works fine if the any of the fields of the specified object are generics, just the object itself should not be a generic type. For the cases when the object is of generic type, invoke fromJson(JsonElement, Type).

Type Parameters:
T - the type of the desired object
Parameters:
json - the root of the parse tree of JsonElements from which the object is to be deserialized
classOfT - The class of T
Returns:
an object of type T from the json
Throws:
JsonSyntaxException - if json is not a valid representation for an object of type typeOfT
Since:
1.3

fromJson

public <T> T fromJson(JsonElement json,
                      java.lang.reflect.Type typeOfT)
           throws JsonSyntaxException
This method deserializes the Json read from the specified parse tree into an object of the specified type. This method is useful if the specified object is a generic type. For non-generic objects, use fromJson(JsonElement, Class) instead.

Type Parameters:
T - the type of the desired object
Parameters:
json - the root of the parse tree of JsonElements from which the object is to be deserialized
typeOfT - The specific genericized type of src. You can obtain this type by using the TypeToken class. For example, to get the type for Collection<Foo>, you should use:
 Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
 
Returns:
an object of type T from the json
Throws:
JsonSyntaxException - if json is not a valid representation for an object of type typeOfT
Since:
1.3

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2008-2011 Google, Inc.. All Rights Reserved.