|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.gson.Gson
public final class Gson
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
JsonSerializer
s, JsonDeserializer
s, and InstanceCreator
s.
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>() {}.getType(); List
target = new LinkedList (); target.add("blah"); Gson gson = new Gson(); String json = gson.toJson(target, listType); List target2 = gson.fromJson(json, listType);
See the Gson User Guide for a more complete set of examples.
TypeToken
Constructor Summary | |
---|---|
Gson()
Constructs a Gson object with default configuration. |
Method Summary | ||
---|---|---|
|
fromJson(JsonElement json,
Class<T> classOfT)
This method deserializes the Json read from the specified parse tree into an object of the specified type. |
|
|
fromJson(JsonElement json,
Type typeOfT)
This method deserializes the Json read from the specified parse tree into an object of the specified type. |
|
|
fromJson(Reader json,
Class<T> classOfT)
This method deserializes the Json read from the specified reader into an object of the specified class. |
|
|
fromJson(Reader json,
Type typeOfT)
This method deserializes the Json read from the specified reader into an object of the specified type. |
|
|
fromJson(String json,
Class<T> classOfT)
This method deserializes the specified Json into an object of the specified class. |
|
|
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. |
|
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. |
|
String |
toString()
|
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Gson()
toJson
methods is in compact representation. This
means that all the unneeded white-space is removed. You can change this behavior with
GsonBuilder.setPrettyPrinting()
. GsonBuilder.serializeNulls()
.Map
,
URL
, URI
, Locale
, Date
,
BigDecimal
, and BigInteger
classes. If you would prefer
to change the default representation, you can do so by registering a type adapter through
GsonBuilder.registerTypeAdapter(Type, Object)
. DateFormat.DEFAULT
. This format
ignores the millisecond portion of the date during serialization. You can change
this by invoking GsonBuilder.setDateFormat(int)
or
GsonBuilder.setDateFormat(String)
. Expose
annotation.
You can enable Gson to serialize/deserialize only those fields marked with this annotation
through GsonBuilder.excludeFieldsWithoutExposeAnnotation()
. Since
annotation. You
can enable Gson to use this annotation through GsonBuilder.setVersion(double)
.versionNumber
will be output as "versionNumber@quot;
in
Json. The same rules are applied for mapping incoming Json to the Java classes. You can
change this policy through GsonBuilder.setFieldNamingPolicy(FieldNamingPolicy)
.transient
or static
fields from
consideration for serialization and deserialization. You can change this behavior through
GsonBuilder.excludeFieldsWithModifiers(int...)
.
Method Detail |
---|
public String toJson(Object src)
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.
src
- the object for which Json representation is to be created setting for Gson
src
.public String toJson(Object src, Type typeOfSrc)
toJson(Object)
instead. If you want to write out
the object to a Appendable
, use toJson(Object, Type, Appendable)
instead.
src
- the object for which JSON representation is to be createdtypeOfSrc
- 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();
src
public void toJson(Object src, Appendable writer)
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.
src
- the object for which Json representation is to be created setting for Gsonwriter
- Writer to which the Json representation needs to be writtenpublic void toJson(Object src, Type typeOfSrc, Appendable writer)
toJson(Object, Appendable)
instead.
src
- the object for which JSON representation is to be createdtypeOfSrc
- 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.public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException
fromJson(String, Type)
. If you have the Json in a Reader
instead of
a String, use fromJson(Reader, Class)
instead.
T
- the type of the desired objectjson
- the string from which the object is to be deserializedclassOfT
- the class of T
JsonParseException
- if json is not a valid representation for an object of type
classOfTpublic <T> T fromJson(String json, Type typeOfT) throws JsonParseException
fromJson(String, Class)
instead. If you have the Json in a Reader
instead of
a String, use fromJson(Reader, Type)
instead.
T
- the type of the desired objectjson
- the string from which the object is to be deserializedtypeOfT
- 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();
JsonParseException
- if json is not a valid representation for an object of type typeOfTpublic <T> T fromJson(Reader json, Class<T> classOfT) throws JsonParseException
fromJson(Reader, Type)
. If you have the Json in a String form instead of a
Reader
, use fromJson(String, Class)
instead.
T
- the type of the desired objectjson
- the reader producing the Json from which the object is to be deserialized.classOfT
- the class of T
JsonParseException
- if json is not a valid representation for an object of type
classOfTpublic <T> T fromJson(Reader json, Type typeOfT) throws JsonParseException
fromJson(Reader, Class)
instead. If you have the Json in a
String form instead of a Reader
, use fromJson(String, Type)
instead.
T
- the type of the desired objectjson
- the reader producing Json from which the object is to be deserializedtypeOfT
- 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();
JsonParseException
- if json is not a valid representation for an object of type typeOfTpublic <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonParseException
fromJson(JsonElement, Type)
.
T
- the type of the desired objectjson
- the root of the parse tree of JsonElement
s from which the object is to
be deserializedclassOfT
- The class of T
JsonParseException
- if json is not a valid representation for an object of type typeOfTpublic <T> T fromJson(JsonElement json, Type typeOfT) throws JsonParseException
fromJson(JsonElement, Class)
instead.
T
- the type of the desired objectjson
- the root of the parse tree of JsonElement
s from which the object is to
be deserializedtypeOfT
- 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();
JsonParseException
- if json is not a valid representation for an object of type typeOfTpublic String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |