Top Description Inners Fields Constructors Methods
com.google.gson

public final Class Gson

extends Object
Class Inheritance
Static Imports
com.google.gson.GsonBuilder.newImmutableList

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. Gson instances are Thread-safe so you can reuse them freely across multiple threads.

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 newline, custom indent, 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 type of the object that you are converting is a ParameterizedType (i.e. has at least one type argument, for example List<MyType>) then for deserialization you must use a fromJson method with Type or TypeToken parameter to specify the parameterized type. For serialization specifying a Type or TypeToken is optional, otherwise Gson will use the runtime type of the object. TypeToken is a class provided by Gson which helps creating parameterized types. Here is an example showing how this can be done:

TypeToken<List<MyType>> listType = new TypeToken<List<MyType>>() {};
List<MyType> target = new LinkedList<MyType>();
target.add(new MyType(1, "abc"));

Gson gson = new Gson();
// For serialization you normally do not have to specify the type, Gson will use
// the runtime type of the objects, however you can also specify it explicitly
String json = gson.toJson(target, listType.getType());

// But for deserialization you have to specify the type
List<MyType> target2 = gson.fromJson(json, listType);

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

JSON Strictness handling

For legacy reasons most of the Gson methods allow JSON data which does not comply with the JSON specification when no explicit strictness is set (the default). To specify the strictness of a Gson instance, you should set it through GsonBuilder#setStrictness(Strictness).

For older Gson versions, which don't have the strictness mode API, the following workarounds can be used:

Serialization

  1. Use getAdapter(Class) to obtain the adapter for the type to be serialized
  2. When using an existing JsonWriter, manually apply the writer settings of this Gson instance listed by newJsonWriter(Writer).
    Otherwise, when not using an existing JsonWriter, use newJsonWriter(Writer) to construct one.
  3. Call TypeAdapter#write(JsonWriter, Object)

Deserialization

  1. Use getAdapter(Class) to obtain the adapter for the type to be deserialized
  2. When using an existing JsonReader, manually apply the reader settings of this Gson instance listed by newJsonReader(Reader).
    Otherwise, when not using an existing JsonReader, use newJsonReader(Reader) to construct one.
  3. Call TypeAdapter#read(JsonReader)
  4. Call JsonReader#peek() and verify that the result is JsonToken#END_DOCUMENT to make sure there is no trailing data
Note that the JsonReader created this way is only 'legacy strict', it mostly adheres to the JSON specification but allows small deviations. See JsonReader#setStrictness(Strictness) for details.
Authors
Inderjeet Singh, Joel Leitch, Jesse Wilson
See Also
TypeToken

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
Gson.FutureTypeAdapter<T>

Proxy type adapter for cyclic type graphs.

Field Summary

Modifier and TypeField and Description
pack-priv final List<TypeAdapterFactory>
pack-priv final List<TypeAdapterFactory>
pack-priv final boolean
private final ConstructorConstructor
pack-priv final String
pack-priv final int
pack-priv final Excluder
pack-priv final List<TypeAdapterFactory>
pack-priv final FieldNamingStrategy
pack-priv final FormattingStyle
pack-priv final boolean
pack-priv final boolean
pack-priv final Map<Type, InstanceCreator<?>>
private static final String
private final JsonAdapterAnnotationTypeAdapterFactory
pack-priv final LongSerializationPolicy
pack-priv final ToNumberStrategy
pack-priv final ToNumberStrategy
pack-priv final List<ReflectionAccessFilter>
pack-priv final boolean
pack-priv final boolean
pack-priv final Strictness
private final ThreadLocal<Map<TypeToken<?>, TypeAdapter<?>>>
threadLocalAdapterResults

This thread local guards against reentrant calls to getAdapter(TypeToken).

pack-priv final int
private final ConcurrentMap<TypeToken<?>, TypeAdapter<?>>
pack-priv final boolean

Constructor Summary

AccessConstructor and Description
public
Gson()

Constructs a Gson object with default configuration.

pack-priv
Gson(GsonBuilder builder)

Method Summary

Modifier and TypeMethod and Description
private static void
public Excluder
excluder()
Deprecated This method by accident exposes an internal Gson class; it might be removed in a future version.

public FieldNamingStrategy
fieldNamingStrategy()

Returns the field naming strategy used by this Gson instance.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the string. Returns null if json is null or if json is empty.
fromJson
(String
the string from which the object is to be deserialized
json
,
Class<T>
the class of T
classOfT
)

This method deserializes the specified JSON into an object of the specified class.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the string. Returns null if json is null or if json is empty.
fromJson
(String
the string from which the object is to be deserialized
json
,
Type
The specific genericized type of src
typeOfT
)

This method deserializes the specified JSON into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the string. Returns null if json is null or if json is empty.
fromJson
(String
the string from which the object is to be deserialized
json
,
TypeToken<T>
The specific genericized type of src. You should create an anonymous subclass of TypeToken with the specific generic type arguments. For example, to get the type for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
typeOfT
)

This method deserializes the specified JSON into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the Reader. Returns null if json is at EOF.
fromJson
(Reader
the reader producing the JSON from which the object is to be deserialized.
json
,
Class<T>
the class of T
classOfT
)

This method deserializes the JSON read from the specified reader into an object of the specified class.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the Reader. Returns null if json is at EOF.
fromJson
(Reader
the reader producing JSON from which the object is to be deserialized
json
,
Type
The specific genericized type of src
typeOfT
)

This method deserializes the JSON read from the specified reader into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the Reader. Returns null if json is at EOF.
fromJson
(Reader
the reader producing JSON from which the object is to be deserialized
json
,
TypeToken<T>
The specific genericized type of src. You should create an anonymous subclass of TypeToken with the specific generic type arguments. For example, to get the type for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
typeOfT
)

This method deserializes the JSON read from the specified reader into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the JsonReader. Returns null if reader is at EOF.
fromJson
(JsonReader
the reader whose next JSON value should be deserialized
reader
,
Type
The specific genericized type of src
typeOfT
)

Reads the next JSON value from reader and converts it to an object of type typeOfT.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the JsonReader. Returns null if reader is at EOF.
fromJson
(JsonReader
the reader whose next JSON value should be deserialized
reader
,
TypeToken<T>
The specific genericized type of src. You should create an anonymous subclass of TypeToken with the specific generic type arguments. For example, to get the type for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
typeOfT
)

Reads the next JSON value from reader and converts it to an object of type typeOfT.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the JSON. Returns null if json is null or if json is empty.
fromJson
(JsonElement
the root of the parse tree of JsonElements from which the object is to be deserialized
json
,
Class<T>
The class of T
classOfT
)

This method deserializes the JSON read from the specified parse tree into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the JSON. Returns null if json is null or if json is empty.
fromJson
(JsonElement
the root of the parse tree of JsonElements from which the object is to be deserialized
json
,
Type
The specific genericized type of src
typeOfT
)

This method deserializes the JSON read from the specified parse tree into an object of the specified type.

public <
the type of the desired object
T
>
T

Returns:

an object of type T from the JSON. Returns null if json is null or if json is empty.
fromJson
(JsonElement
the root of the parse tree of JsonElements from which the object is to be deserialized
json
,
TypeToken<T>
The specific genericized type of src. You should create an anonymous subclass of TypeToken with the specific generic type arguments. For example, to get the type for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
typeOfT
)

This method deserializes the JSON read from the specified parse tree into an object of the specified type.

public <T> TypeAdapter<T>
getAdapter(TypeToken<T> type)

Returns the type adapter for type.

public <T> TypeAdapter<T>
getAdapter(Class<T> type)

Returns the type adapter for type.

public <T> TypeAdapter<T>
getDelegateAdapter(TypeAdapterFactory
The type adapter factory that needs to be skipped while searching for a matching type adapter. In most cases, you should just pass this (the type adapter factory from where getDelegateAdapter method is being invoked).
skipPast
,
TypeToken<T>
Type for which the delegate adapter is being searched for.
type
)

This method is used to get an alternate type adapter for the specified type.

public boolean
htmlSafe()

Returns whether this Gson instance produces JSON output which is HTML-safe, that means all HTML characters are escaped.

public GsonBuilder

Returns:

a GsonBuilder instance.
newBuilder
()

Returns a new GsonBuilder containing all custom factories and configuration used by the current instance.

public JsonReader
newJsonReader(Reader reader)

Returns a new JSON reader configured for the settings on this Gson instance.

public JsonWriter
newJsonWriter(Writer writer)

Returns a new JSON writer configured for the settings on this Gson instance.

public boolean
serializeNulls()

Returns whether this Gson instance is serializing JSON object properties with null values, or just omits them.

public String

Returns:

JSON representation of src.
toJson
(Object
the object for which JSON representation is to be created
src
)

This method serializes the specified object into its equivalent JSON representation.

public String

Returns:

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

This method serializes the specified object, including those of generic types, into its equivalent JSON representation.

public void
toJson(Object
the object for which JSON representation is to be created
src
,
Appendable
Writer to which the JSON representation needs to be written
writer
)

This method serializes the specified object into its equivalent JSON representation and writes it to the writer.

public void
toJson(Object
the object for which JSON representation is to be created
src
,
Type
The specific genericized type of src. You can obtain this type by using the com.google.gson.reflect.TypeToken class. For example, to get the type for Collection<Foo>, you should use:
Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
typeOfSrc
,
Appendable
Writer to which the JSON representation of src needs to be written
writer
)

This method serializes the specified object, including those of generic types, into its equivalent JSON representation and writes it to the writer.

public void
toJson(Object
the object for which JSON representation is to be created
src
,
Type
the type of the object to be written
typeOfSrc
,
JsonWriter
Writer to which the JSON representation of src needs to be written
writer
)

Writes the JSON representation of src of type typeOfSrc to writer.

public String

Returns:

JSON String representation of the tree.
toJson
(JsonElement
root of a tree of JsonElements
jsonElement
)

Converts a tree of JsonElements into its equivalent JSON representation.

public void
toJson(JsonElement
root of a tree of JsonElements
jsonElement
,
Appendable
Writer to which the JSON representation needs to be written
writer
)

Writes out the equivalent JSON for a tree of JsonElements.

public void
toJson(JsonElement
the JSON element to be written
jsonElement
,
JsonWriter
the JSON writer to which the provided element will be written
writer
)

Writes the JSON for jsonElement to writer.

public JsonElement

Returns:

JSON representation of src.
toJsonTree
(Object
the object for which JSON representation is to be created
src
)

This method serializes the specified object into its equivalent representation as a tree of JsonElements.

public JsonElement

Returns:

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

This method serializes the specified object, including those of generic types, into its equivalent representation as a tree of JsonElements.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait