|
||||||||||
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
.
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
Constructor Summary | |
---|---|
Gson()
Constructs a Gson object with default configuration. |
Method Summary | ||
---|---|---|
|
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. |
|
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 |
---|
public Gson()
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.
src
- the object for which Json representation is to be created setting for Gson.
public String toJson(Object src, Type typeOfSrc)
toJson(Object)
instead.
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();
public <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException
fromJson(String, Type)
.
T
- the type of the desired object.json
- the string 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
classOfT.public <T> T fromJson(String json, Type typeOfT) throws JsonParseException
fromJson(String, Class)
instead.
T
- the type of the desired object.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();
JsonParseException
- if json is not a valid representation for an object of type typeOfT.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |