|
|
|
@ -76,26 +76,32 @@ import java.util.concurrent.atomic.AtomicLongArray;
|
|
|
|
|
* <pre>
|
|
|
|
|
* Gson gson = new Gson(); // Or use new GsonBuilder().create();
|
|
|
|
|
* MyType target = new MyType();
|
|
|
|
|
* String json = gson.toJson(target); // serializes target to Json
|
|
|
|
|
* String json = gson.toJson(target); // serializes target to JSON
|
|
|
|
|
* MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* <p>If the object that your are serializing/deserializing is a {@code ParameterizedType}
|
|
|
|
|
* (i.e. contains at least one type parameter and may be an array) then you must use the
|
|
|
|
|
* {@link #toJson(Object, Type)} or {@link #fromJson(String, Type)} method. Here is an
|
|
|
|
|
* example for serializing and deserializing a {@code ParameterizedType}:
|
|
|
|
|
*
|
|
|
|
|
* <p>If the type of the object that you are converting is a {@code ParameterizedType}
|
|
|
|
|
* (i.e. has at least one type argument, for example {@code List<MyType>}) then for
|
|
|
|
|
* deserialization you must use a {@code fromJson} method with {@link Type} or {@link TypeToken}
|
|
|
|
|
* parameter to specify the parameterized type. For serialization specifying a {@code Type}
|
|
|
|
|
* or {@code TypeToken} is optional, otherwise Gson will use the runtime type of the object.
|
|
|
|
|
* {@link TypeToken} is a class provided by Gson which helps creating parameterized types.
|
|
|
|
|
* Here is an example showing how this can be done:
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type listType = new TypeToken<List<String>>() {}.getType();
|
|
|
|
|
* List<String> target = new LinkedList<String>();
|
|
|
|
|
* target.add("blah");
|
|
|
|
|
* TypeToken<List<MyType>> listType = new TypeToken<List<MyType>>() {};
|
|
|
|
|
* List<MyType> target = new LinkedList<MyType>();
|
|
|
|
|
* target.add(new MyType(1, "abc"));
|
|
|
|
|
*
|
|
|
|
|
* Gson gson = new Gson();
|
|
|
|
|
* String json = gson.toJson(target, listType);
|
|
|
|
|
* List<String> target2 = gson.fromJson(json, listType);
|
|
|
|
|
* // 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);
|
|
|
|
|
* </pre>
|
|
|
|
|
*
|
|
|
|
|
* <p>See the <a href="https://sites.google.com/site/gson/gson-user-guide">Gson User Guide</a>
|
|
|
|
|
* <p>See the <a href="https://github.com/google/gson/blob/master/UserGuide.md">Gson User Guide</a>
|
|
|
|
|
* for a more complete set of examples.</p>
|
|
|
|
|
*
|
|
|
|
|
* <h2>Lenient JSON handling</h2>
|
|
|
|
@ -125,7 +131,7 @@ import java.util.concurrent.atomic.AtomicLongArray;
|
|
|
|
|
* to make sure there is no trailing data
|
|
|
|
|
* </ol>
|
|
|
|
|
*
|
|
|
|
|
* @see com.google.gson.reflect.TypeToken
|
|
|
|
|
* @see TypeToken
|
|
|
|
|
*
|
|
|
|
|
* @author Inderjeet Singh
|
|
|
|
|
* @author Joel Leitch
|
|
|
|
@ -210,9 +216,9 @@ public final class Gson {
|
|
|
|
|
* through {@link GsonBuilder#excludeFieldsWithoutExposeAnnotation()}. </li>
|
|
|
|
|
* <li>By default, Gson ignores the {@link com.google.gson.annotations.Since} annotation. You
|
|
|
|
|
* can enable Gson to use this annotation through {@link GsonBuilder#setVersion(double)}.</li>
|
|
|
|
|
* <li>The default field naming policy for the output Json is same as in Java. So, a Java class
|
|
|
|
|
* <li>The default field naming policy for the output JSON is same as in Java. So, a Java class
|
|
|
|
|
* field <code>versionNumber</code> will be output as <code>"versionNumber"</code> in
|
|
|
|
|
* Json. The same rules are applied for mapping incoming Json to the Java classes. You can
|
|
|
|
|
* JSON. The same rules are applied for mapping incoming JSON to the Java classes. You can
|
|
|
|
|
* change this policy through {@link GsonBuilder#setFieldNamingPolicy(FieldNamingPolicy)}.</li>
|
|
|
|
|
* <li>By default, Gson excludes <code>transient</code> or <code>static</code> fields from
|
|
|
|
|
* consideration for serialization and deserialization. You can change this behavior through
|
|
|
|
@ -644,13 +650,15 @@ public final class Gson {
|
|
|
|
|
* {@link JsonElement}s. This method should be used when the specified object is not a generic
|
|
|
|
|
* type. This method uses {@link Class#getClass()} to get the type for the specified object, but
|
|
|
|
|
* the {@code 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,
|
|
|
|
|
* of Java. Note that this method works fine if 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
|
|
|
|
|
* {@link #toJsonTree(Object, Type)} instead.
|
|
|
|
|
*
|
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
|
* @return Json representation of {@code src}.
|
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
|
* @return JSON representation of {@code src}.
|
|
|
|
|
* @since 1.4
|
|
|
|
|
*
|
|
|
|
|
* @see #toJsonTree(Object, Type)
|
|
|
|
|
*/
|
|
|
|
|
public JsonElement toJsonTree(Object src) {
|
|
|
|
|
if (src == null) {
|
|
|
|
@ -674,6 +682,8 @@ public final class Gson {
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return Json representation of {@code src}
|
|
|
|
|
* @since 1.4
|
|
|
|
|
*
|
|
|
|
|
* @see #toJsonTree(Object)
|
|
|
|
|
*/
|
|
|
|
|
public JsonElement toJsonTree(Object src, Type typeOfSrc) {
|
|
|
|
|
JsonTreeWriter writer = new JsonTreeWriter();
|
|
|
|
@ -682,17 +692,20 @@ public final class Gson {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method serializes the specified object into its equivalent Json representation.
|
|
|
|
|
* 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
|
|
|
|
|
* {@link Class#getClass()} to get the type for the specified object, but the
|
|
|
|
|
* {@code 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,
|
|
|
|
|
* of Java. Note that this method works fine if 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
|
|
|
|
|
* {@link #toJson(Object, Type)} instead. If you want to write out the object to a
|
|
|
|
|
* {@link Writer}, use {@link #toJson(Object, Appendable)} instead.
|
|
|
|
|
*
|
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
|
* @return Json representation of {@code src}.
|
|
|
|
|
*
|
|
|
|
|
* @see #toJson(Object, Appendable)
|
|
|
|
|
* @see #toJson(Object, Type)
|
|
|
|
|
*/
|
|
|
|
|
public String toJson(Object src) {
|
|
|
|
|
if (src == null) {
|
|
|
|
@ -703,7 +716,7 @@ public final class Gson {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* equivalent JSON representation. This method must be used if the specified object is a generic
|
|
|
|
|
* type. For non-generic objects, use {@link #toJson(Object)} instead. If you want to write out
|
|
|
|
|
* the object to a {@link Appendable}, use {@link #toJson(Object, Type, Appendable)} instead.
|
|
|
|
|
*
|
|
|
|
@ -714,7 +727,10 @@ public final class Gson {
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return Json representation of {@code src}
|
|
|
|
|
* @return JSON representation of {@code src}
|
|
|
|
|
*
|
|
|
|
|
* @see #toJson(Object, Type, Appendable)
|
|
|
|
|
* @see #toJson(Object)
|
|
|
|
|
*/
|
|
|
|
|
public String toJson(Object src, Type typeOfSrc) {
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
@ -723,18 +739,22 @@ public final class Gson {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method serializes the specified object into its equivalent Json representation.
|
|
|
|
|
* This method serializes the specified object into its equivalent JSON representation and
|
|
|
|
|
* writes it to the writer.
|
|
|
|
|
* This method should be used when the specified object is not a generic type. This method uses
|
|
|
|
|
* {@link Class#getClass()} to get the type for the specified object, but the
|
|
|
|
|
* {@code 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,
|
|
|
|
|
* of Java. Note that this method works fine if 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
|
|
|
|
|
* {@link #toJson(Object, Type, Appendable)} instead.
|
|
|
|
|
*
|
|
|
|
|
* @param src the object for which Json representation is to be created setting for Gson
|
|
|
|
|
* @param writer Writer to which the Json representation needs to be written
|
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
|
* @param writer Writer to which the JSON representation needs to be written
|
|
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
|
|
|
|
* @since 1.2
|
|
|
|
|
*
|
|
|
|
|
* @see #toJson(Object)
|
|
|
|
|
* @see #toJson(Object, Type, Appendable)
|
|
|
|
|
*/
|
|
|
|
|
public void toJson(Object src, Appendable writer) throws JsonIOException {
|
|
|
|
|
if (src != null) {
|
|
|
|
@ -746,8 +766,9 @@ public final class Gson {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 {@link #toJson(Object, Appendable)} instead.
|
|
|
|
|
* equivalent JSON representation and writes it to the writer.
|
|
|
|
|
* This method must be used if the specified object is a generic type. For non-generic objects,
|
|
|
|
|
* use {@link #toJson(Object, Appendable)} instead.
|
|
|
|
|
*
|
|
|
|
|
* @param src the object for which JSON representation is to be created
|
|
|
|
|
* @param typeOfSrc The specific genericized type of src. You can obtain
|
|
|
|
@ -756,9 +777,12 @@ public final class Gson {
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
|
* </pre>
|
|
|
|
|
* @param writer Writer to which the Json representation of src needs to be written.
|
|
|
|
|
* @param writer Writer to which the JSON representation of src needs to be written.
|
|
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
|
|
|
|
* @since 1.2
|
|
|
|
|
*
|
|
|
|
|
* @see #toJson(Object, Type)
|
|
|
|
|
* @see #toJson(Object, Appendable)
|
|
|
|
|
*/
|
|
|
|
|
public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOException {
|
|
|
|
|
try {
|
|
|
|
@ -824,7 +848,7 @@ public final class Gson {
|
|
|
|
|
* Writes out the equivalent JSON for a tree of {@link JsonElement}s.
|
|
|
|
|
*
|
|
|
|
|
* @param jsonElement root of a tree of {@link JsonElement}s
|
|
|
|
|
* @param writer Writer to which the Json representation needs to be written
|
|
|
|
|
* @param writer Writer to which the JSON representation needs to be written
|
|
|
|
|
* @throws JsonIOException if there was a problem writing to the writer
|
|
|
|
|
* @since 1.4
|
|
|
|
|
*/
|
|
|
|
@ -913,17 +937,17 @@ public final class Gson {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the specified Json into an object of the specified class. It is not
|
|
|
|
|
* 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
|
|
|
|
|
* {@link #fromJson(String, Type)}. If you have the Json in a {@link Reader} instead of
|
|
|
|
|
* {@link #fromJson(String, TypeToken)}. If you have the JSON in a {@link Reader} instead of
|
|
|
|
|
* a String, use {@link #fromJson(Reader, Class)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON string has multiple top-level JSON elements,
|
|
|
|
|
* or if there is trailing data.
|
|
|
|
|
* <p>An exception is thrown if the JSON string has multiple top-level JSON elements, or if there
|
|
|
|
|
* is trailing data. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the string from which the object is to be deserialized
|
|
|
|
@ -932,98 +956,165 @@ public final class Gson {
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
|
|
|
|
* classOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, Class)
|
|
|
|
|
* @see #fromJson(String, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
|
|
|
|
|
Object object = fromJson(json, (Type) classOfT);
|
|
|
|
|
T object = fromJson(json, TypeToken.get(classOfT));
|
|
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the specified Json into an object of the specified type. This method
|
|
|
|
|
* 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
|
|
|
|
|
* {@link #fromJson(String, Class)} instead. If you have the Json in a {@link Reader} instead of
|
|
|
|
|
* {@link #fromJson(String, Class)} instead. If you have the JSON in a {@link Reader} instead of
|
|
|
|
|
* a String, use {@link #fromJson(Reader, Type)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>Since {@code Type} is not parameterized by T, this method is not type-safe and
|
|
|
|
|
* should be used carefully. If you are creating the {@code Type} from a {@link TypeToken},
|
|
|
|
|
* prefer using {@link #fromJson(String, TypeToken)} instead since its return type is based
|
|
|
|
|
* on the {@code TypeToken} and is therefore more type-safe.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON string has multiple top-level JSON elements,
|
|
|
|
|
* or if there is trailing data.
|
|
|
|
|
* or if there is trailing data. Use {@link #fromJson(JsonReader, Type)} if this behavior is
|
|
|
|
|
* not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the string from which the object is to be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
|
* @param typeOfT The specific genericized type of src
|
|
|
|
|
* @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, Type)
|
|
|
|
|
* @see #fromJson(String, Class)
|
|
|
|
|
* @see #fromJson(String, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
|
|
|
|
|
return (T) fromJson(json, TypeToken.get(typeOfT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* {@link #fromJson(String, Class)} instead. If you have the JSON in a {@link Reader} instead of
|
|
|
|
|
* a String, use {@link #fromJson(Reader, TypeToken)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON string has multiple top-level JSON elements, or if there
|
|
|
|
|
* is trailing data. Use {@link #fromJson(JsonReader, TypeToken)} if this behavior is not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the string from which the object is to be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You should create an anonymous subclass of
|
|
|
|
|
* {@code TypeToken} with the specific generic type arguments. For example, to get the type for
|
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
|
* new TypeToken<Collection<Foo>>(){}
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return an object of type T from the string. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonParseException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of the type typeOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, TypeToken)
|
|
|
|
|
* @see #fromJson(String, Class)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
|
|
|
|
|
public <T> T fromJson(String json, TypeToken<T> typeOfT) throws JsonSyntaxException {
|
|
|
|
|
if (json == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
StringReader reader = new StringReader(json);
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
T target = (T) fromJson(reader, typeOfT);
|
|
|
|
|
return target;
|
|
|
|
|
return fromJson(reader, typeOfT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the Json read from the specified reader into an object of the
|
|
|
|
|
* This method deserializes the JSON read from the specified reader 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
|
|
|
|
|
* this method works fine if 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 {@link #fromJson(Reader, Type)}. If you have the Json in a String form instead of a
|
|
|
|
|
* invoke {@link #fromJson(Reader, TypeToken)}. If you have the JSON in a String form instead of a
|
|
|
|
|
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON data has multiple top-level JSON elements,
|
|
|
|
|
* or if there is trailing data.
|
|
|
|
|
* <p>An exception is thrown if the JSON data has multiple top-level JSON elements, or if there
|
|
|
|
|
* is trailing data. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the reader producing the Json from which the object is to be deserialized.
|
|
|
|
|
* @param json the reader producing the JSON from which the object is to be deserialized.
|
|
|
|
|
* @param classOfT the class of T
|
|
|
|
|
* @return an object of type T from the string. Returns {@code null} if {@code json} is at EOF.
|
|
|
|
|
* @return an object of type T from the Reader. Returns {@code null} if {@code json} is at EOF.
|
|
|
|
|
* @throws JsonIOException if there was a problem reading from the Reader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @since 1.2
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(String, Class)
|
|
|
|
|
* @see #fromJson(Reader, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(Reader json, Class<T> classOfT) throws JsonSyntaxException, JsonIOException {
|
|
|
|
|
JsonReader jsonReader = newJsonReader(json);
|
|
|
|
|
Object object = fromJson(jsonReader, classOfT);
|
|
|
|
|
assertFullConsumption(object, jsonReader);
|
|
|
|
|
T object = fromJson(json, TypeToken.get(classOfT));
|
|
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the Json read from the specified reader into an object of the
|
|
|
|
|
* This method deserializes the JSON read from the specified reader into an object of the
|
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
|
|
|
|
* non-generic objects, use {@link #fromJson(Reader, Class)} instead. If you have the Json in a
|
|
|
|
|
* non-generic objects, use {@link #fromJson(Reader, Class)} instead. If you have the JSON in a
|
|
|
|
|
* String form instead of a {@link Reader}, use {@link #fromJson(String, Type)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON data has multiple top-level JSON elements,
|
|
|
|
|
* or if there is trailing data.
|
|
|
|
|
* <p>Since {@code Type} is not parameterized by T, this method is not type-safe and
|
|
|
|
|
* should be used carefully. If you are creating the {@code Type} from a {@link TypeToken},
|
|
|
|
|
* prefer using {@link #fromJson(Reader, TypeToken)} instead since its return type is based
|
|
|
|
|
* on the {@code TypeToken} and is therefore more type-safe.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON data has multiple top-level JSON elements, or if there
|
|
|
|
|
* is trailing data. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the reader producing Json from which the object is to be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
|
* @param json the reader producing JSON from which the object is to be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src
|
|
|
|
|
* @return an object of type T from the Reader. Returns {@code null} if {@code json} is at EOF.
|
|
|
|
|
* @throws JsonIOException if there was a problem reading from the Reader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @since 1.2
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(String, Type)
|
|
|
|
|
* @see #fromJson(Reader, Class)
|
|
|
|
|
* @see #fromJson(Reader, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
|
|
|
|
|
return (T) fromJson(json, TypeToken.get(typeOfT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the JSON read from the specified reader into an object of the
|
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
|
|
|
|
* non-generic objects, use {@link #fromJson(Reader, Class)} instead. If you have the JSON in a
|
|
|
|
|
* String form instead of a {@link Reader}, use {@link #fromJson(String, TypeToken)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>An exception is thrown if the JSON data has multiple top-level JSON elements, or if there
|
|
|
|
|
* is trailing data. Use {@link #fromJson(JsonReader, TypeToken)} if this behavior is not desired.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the reader producing JSON from which the object is to be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You should create an anonymous subclass of
|
|
|
|
|
* {@code TypeToken} with the specific generic type arguments. For example, to get the type for
|
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
|
* new TypeToken<Collection<Foo>>(){}
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return an object of type T from the json. Returns {@code null} if {@code json} is at EOF.
|
|
|
|
|
* @return an object of type T from the Reader. Returns {@code null} if {@code json} is at EOF.
|
|
|
|
|
* @throws JsonIOException if there was a problem reading from the Reader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
|
|
|
|
* @since 1.2
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type of typeOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(String, TypeToken)
|
|
|
|
|
* @see #fromJson(Reader, Class)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
|
|
|
|
|
public <T> T fromJson(Reader json, TypeToken<T> typeOfT) throws JsonIOException, JsonSyntaxException {
|
|
|
|
|
JsonReader jsonReader = newJsonReader(json);
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
T object = (T) fromJson(jsonReader, typeOfT);
|
|
|
|
|
T object = fromJson(jsonReader, typeOfT);
|
|
|
|
|
assertFullConsumption(object, jsonReader);
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
@ -1040,10 +1131,18 @@ public final class Gson {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fromJson(JsonReader, Class) is unfortunately missing and cannot be added now without breaking
|
|
|
|
|
// source compatibility in certain cases, see https://github.com/google/gson/pull/1700#discussion_r973764414
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads the next JSON value from {@code reader} and convert it to an object
|
|
|
|
|
* Reads the next JSON value from {@code reader} and converts it to an object
|
|
|
|
|
* of type {@code typeOfT}. Returns {@code null}, if the {@code reader} is at EOF.
|
|
|
|
|
* Since Type is not parameterized by T, this method is type unsafe and should be used carefully.
|
|
|
|
|
*
|
|
|
|
|
* <p>Since {@code Type} is not parameterized by T, this method is not type-safe and
|
|
|
|
|
* should be used carefully. If you are creating the {@code Type} from a {@link TypeToken},
|
|
|
|
|
* prefer using {@link #fromJson(JsonReader, TypeToken)} instead since its return type is based
|
|
|
|
|
* on the {@code TypeToken} and is therefore more type-safe. If the provided type is a
|
|
|
|
|
* {@code Class} the {@code TypeToken} can be created with {@link TypeToken#get(Class)}.
|
|
|
|
|
*
|
|
|
|
|
* <p>Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has
|
|
|
|
|
* multiple top-level JSON elements, or if there is trailing data.
|
|
|
|
@ -1052,19 +1151,58 @@ public final class Gson {
|
|
|
|
|
* regardless of the lenient mode setting of the provided reader. The lenient mode setting
|
|
|
|
|
* of the reader is restored once this method returns.
|
|
|
|
|
*
|
|
|
|
|
* @throws JsonIOException if there was a problem writing to the Reader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param reader the reader whose next JSON value should be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src
|
|
|
|
|
* @return an object of type T from the JsonReader. Returns {@code null} if {@code reader} is at EOF.
|
|
|
|
|
* @throws JsonIOException if there was a problem reading from the JsonReader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, Type)
|
|
|
|
|
* @see #fromJson(JsonReader, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
|
|
|
|
|
return (T) fromJson(reader, TypeToken.get(typeOfT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads the next JSON value from {@code reader} and converts it to an object
|
|
|
|
|
* of type {@code typeOfT}. Returns {@code null}, if the {@code reader} is at EOF.
|
|
|
|
|
* This method is useful if the specified object is a generic type. For non-generic objects,
|
|
|
|
|
* {@link #fromJson(JsonReader, Type)} can be called, or {@link TypeToken#get(Class)} can
|
|
|
|
|
* be used to create the type token.
|
|
|
|
|
*
|
|
|
|
|
* <p>Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has
|
|
|
|
|
* multiple top-level JSON elements, or if there is trailing data.
|
|
|
|
|
*
|
|
|
|
|
* <p>The JSON data is parsed in {@linkplain JsonReader#setLenient(boolean) lenient mode},
|
|
|
|
|
* regardless of the lenient mode setting of the provided reader. The lenient mode setting
|
|
|
|
|
* of the reader is restored once this method returns.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param reader the reader whose next JSON value should be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You should create an anonymous subclass of
|
|
|
|
|
* {@code TypeToken} with the specific generic type arguments. For example, to get the type for
|
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
|
* <pre>
|
|
|
|
|
* new TypeToken<Collection<Foo>>(){}
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return an object of type T from the JsonReader. Returns {@code null} if {@code reader} is at EOF.
|
|
|
|
|
* @throws JsonIOException if there was a problem reading from the JsonReader
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of the type typeOfT
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, TypeToken)
|
|
|
|
|
* @see #fromJson(JsonReader, Type)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOException, JsonSyntaxException {
|
|
|
|
|
boolean isEmpty = true;
|
|
|
|
|
boolean oldLenient = reader.isLenient();
|
|
|
|
|
reader.setLenient(true);
|
|
|
|
|
try {
|
|
|
|
|
reader.peek();
|
|
|
|
|
isEmpty = false;
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT);
|
|
|
|
|
TypeAdapter<T> typeAdapter = getAdapter(typeToken);
|
|
|
|
|
TypeAdapter<T> typeAdapter = getAdapter(typeOfT);
|
|
|
|
|
T object = typeAdapter.read(reader);
|
|
|
|
|
return object;
|
|
|
|
|
} catch (EOFException e) {
|
|
|
|
@ -1091,52 +1229,86 @@ public final class Gson {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the Json read from the specified parse tree into an object of the
|
|
|
|
|
* This method deserializes the JSON read from the specified parse tree into an object of the
|
|
|
|
|
* specified type. 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
|
|
|
|
|
* this method works fine if 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 {@link #fromJson(JsonElement, Type)}.
|
|
|
|
|
* invoke {@link #fromJson(JsonElement, TypeToken)}.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
|
|
|
|
|
* be deserialized
|
|
|
|
|
* @param classOfT The class of T
|
|
|
|
|
* @return an object of type T from the json. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* @return an object of type T from the JSON. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type classOfT
|
|
|
|
|
* @since 1.3
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, Class)
|
|
|
|
|
* @see #fromJson(JsonElement, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
|
|
|
|
|
Object object = fromJson(json, (Type) classOfT);
|
|
|
|
|
T object = fromJson(json, TypeToken.get(classOfT));
|
|
|
|
|
return Primitives.wrap(classOfT).cast(object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the Json read from the specified parse tree into an object of the
|
|
|
|
|
* This method deserializes the JSON read from the specified parse tree into an object of the
|
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
|
|
|
|
* non-generic objects, use {@link #fromJson(JsonElement, Class)} instead.
|
|
|
|
|
*
|
|
|
|
|
* <p>Since {@code Type} is not parameterized by T, this method is not type-safe and
|
|
|
|
|
* should be used carefully. If you are creating the {@code Type} from a {@link TypeToken},
|
|
|
|
|
* prefer using {@link #fromJson(JsonElement, TypeToken)} instead since its return type is based
|
|
|
|
|
* on the {@code TypeToken} and is therefore more type-safe.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
|
|
|
|
|
* be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src
|
|
|
|
|
* @return an object of type T from the JSON. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @since 1.3
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, Type)
|
|
|
|
|
* @see #fromJson(JsonElement, Class)
|
|
|
|
|
* @see #fromJson(JsonElement, TypeToken)
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
|
|
|
|
|
return (T) fromJson(json, TypeToken.get(typeOfT));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method deserializes the JSON read from the specified parse tree into an object of the
|
|
|
|
|
* specified type. This method is useful if the specified object is a generic type. For
|
|
|
|
|
* non-generic objects, use {@link #fromJson(JsonElement, Class)} instead.
|
|
|
|
|
*
|
|
|
|
|
* @param <T> the type of the desired object
|
|
|
|
|
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
|
|
|
|
|
* be deserialized
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You can obtain this type by using the
|
|
|
|
|
* {@link com.google.gson.reflect.TypeToken} class. For example, to get the type for
|
|
|
|
|
* @param typeOfT The specific genericized type of src. You should create an anonymous subclass of
|
|
|
|
|
* {@code TypeToken} with the specific generic type arguments. For example, to get the type for
|
|
|
|
|
* {@code Collection<Foo>}, you should use:
|
|
|
|
|
* <pre>
|
|
|
|
|
* Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
|
|
|
|
|
* new TypeToken<Collection<Foo>>(){}
|
|
|
|
|
* </pre>
|
|
|
|
|
* @return an object of type T from the json. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* @return an object of type T from the JSON. Returns {@code null} if {@code json} is {@code null}
|
|
|
|
|
* or if {@code json} is empty.
|
|
|
|
|
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
|
|
|
|
|
* @since 1.3
|
|
|
|
|
*
|
|
|
|
|
* @see #fromJson(Reader, TypeToken)
|
|
|
|
|
* @see #fromJson(JsonElement, Class)
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public <T> T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
|
|
|
|
|
public <T> T fromJson(JsonElement json, TypeToken<T> typeOfT) throws JsonSyntaxException {
|
|
|
|
|
if (json == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (T) fromJson(new JsonTreeReader(json), typeOfT);
|
|
|
|
|
return fromJson(new JsonTreeReader(json), typeOfT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class FutureTypeAdapter<T> extends TypeAdapter<T> {
|
|
|
|
|