com.google.gson
Class Gson

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

public final class Gson
extends 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 InstanceCreator.

Here is an example of how Gson is used:

 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(MyType.class, json); // deserializes json into target2
 

Author:
Inderjeet Singh, Joel Leitch

Constructor Summary
Gson()
          Constructs a Gson object with default configuration.
 
Method Summary
<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.
 String toJson(Object src)
          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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Gson

public Gson()
Constructs a Gson object with default configuration.

Method Detail

toJson

public String toJson(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.

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

toJson

public String toJson(Object src,
                     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.

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.

fromJson

public <T> T fromJson(String json,
                      Class<T> classOfT)
           throws JsonParseException
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).

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:
JsonParseException - if json is not a valid representation for an object of type classOfT.

fromJson

public <T> T fromJson(String json,
                      Type typeOfT)
           throws JsonParseException
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.

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.


Copyright © 2008. All Rights Reserved.