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.
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:
getAdapter(Class) to obtain the adapter for the type to be serialized
JsonWriter, manually apply the writer settings of this
Gson instance listed by newJsonWriter(Writer).JsonWriter, use newJsonWriter(Writer) to construct one.
TypeAdapter#write(JsonWriter, Object)
getAdapter(Class) to obtain the adapter for the type to be deserialized
JsonReader, manually apply the reader settings of this
Gson instance listed by newJsonReader(Reader).JsonReader, use newJsonReader(Reader) to construct one.
TypeAdapter#read(JsonReader)
JsonReader#peek() and verify that the result is JsonToken#END_DOCUMENT
to make sure there is no trailing data
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.
TypeToken
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final List | |
| pack-priv final List | |
| pack-priv final boolean | |
| private final ConstructorConstructor | |
| pack-priv final String | |
| pack-priv final int | |
| pack-priv final Excluder | |
| pack-priv final List | |
| pack-priv final FieldNamingStrategy | |
| pack-priv final FormattingStyle | |
| pack-priv final boolean | |
| pack-priv final boolean | |
| pack-priv final Map | |
| private static final String | |
| private final JsonAdapterAnnotationTypeAdapterFactory | |
| pack-priv final LongSerializationPolicy | |
| pack-priv final ToNumberStrategy | |
| pack-priv final ToNumberStrategy | |
| pack-priv final List | |
| pack-priv final boolean | |
| pack-priv final boolean | |
| pack-priv final Strictness | |
| private final ThreadLocal | threadLocalAdapterResults
This thread local guards against reentrant calls to |
| pack-priv final int | |
| private final ConcurrentMap | |
| pack-priv final boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| pack-priv |
| Modifier and Type | Method 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 | |
| public < the type of the desired object T> T | |
| public < the type of the desired object T> T | Returns: an object of type T from the string. Returnsnull if json is null or if json is empty.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. Returnsnull if json is null or if json is empty.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
typeOfT)TypeToken with the specific generic type arguments. For example, to get the type
for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
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. Returnsnull if json is at EOF.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. Returnsnull if json is at EOF.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. Returnsnull if json is at EOF.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
typeOfT)TypeToken with the specific generic type arguments. For example, to get the type
for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
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. Returnsnull if reader is at
EOF.the reader whose next JSON value should be deserialized reader, Type The specific genericized type of src typeOfT)Reads the next JSON value from |
| public < the type of the desired object T> T | Returns: an object of type T from the JsonReader. Returnsnull if reader is at
EOF.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
typeOfT)TypeToken with the specific generic type arguments. For example, to get the type
for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
Reads the next JSON value from |
| public < the type of the desired object T> T | Returns: an object of type T from the JSON. Returnsnull if json is null
or if json is empty.the root of the parse tree of json, Class<T> JsonElements from which the object is to be
deserializedThe 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. Returnsnull if json is null
or if json is empty.the root of the parse tree of json, Type JsonElements from which the object is to be
deserializedThe 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. Returnsnull if json is null
or if json is empty.the root of the parse tree of json, TypeToken<T> JsonElements from which the object is to be
deserializedThe specific genericized type of src. You should create an anonymous subclass of
typeOfT)TypeToken with the specific generic type arguments. For example, to get the type
for Collection<Foo>, you should use:
new TypeToken<Collection<Foo>>(){}
This method deserializes the JSON read from the specified parse tree into an object of the specified type. |
| public <T> TypeAdapter | |
| public <T> TypeAdapter | |
| public <T> TypeAdapter | 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 skipPast, TypeToken<T> getDelegateAdapter method is being invoked).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.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 |
| public String | |
| public String | Returns: JSON representation ofsrc.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
typeOfSrc)com. class. For example, to get the type for Collection<Foo>, you should use:
Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
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
typeOfSrc, Appendable com. class. For example, to get the type for Collection<Foo>, you should use:
Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
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 |
| public String | Returns: JSON String representation of the tree.root of a tree of jsonElement)JsonElementsConverts a tree of |
| public void | toJson(JsonElement
root of a tree of jsonElement, Appendable JsonElementsWriter to which the JSON representation needs to be written writer)Writes out the equivalent JSON for a tree of |
| 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 |
| public JsonElement | Returns: JSON representation ofsrc.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
|
| public JsonElement | Returns: JSON representation ofsrc.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
typeOfSrc)com. class. For example, to get the type for Collection<Foo>, you should use:
Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
This method serializes the specified object, including those of generic types, into its
equivalent representation as a tree of |
| public String |