classOfT) throws JsonSyntaxException
}
/**
- * 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.
*
+ * 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.
+ *
* @param 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
- * {@code Collection}, you should use:
- *
- * Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
- *
+ * @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 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 type typeOfT
+ *
+ * @see #fromJson(Reader, Type)
+ * @see #fromJson(String, Class)
+ * @see #fromJson(String, TypeToken)
*/
- @SuppressWarnings("unchecked")
public T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
if (json == null) {
return null;
}
StringReader reader = new StringReader(json);
+ @SuppressWarnings("unchecked")
T target = (T) fromJson(reader, typeOfT);
return target;
}
/**
- * This method deserializes the Json read from the specified reader into an object of the
+ * 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.
+ *
+ * @param 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}, you should use:
+ *
+ * new TypeToken<Collection<Foo>>(){}
+ *
+ * @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 the erasure type
+ * of typeOfT
+ *
+ * @see #fromJson(Reader, TypeToken)
+ * @see #fromJson(String, Class)
+ */
+ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxException {
+ return fromJson(json, typeOfT.getType());
+ }
+
+ /**
+ * 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, Type)}. If you have the JSON in a String form instead of a
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
*
* @param 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, Type)
*/
public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException, JsonIOException {
JsonReader jsonReader = newJsonReader(json);
@@ -873,32 +934,64 @@ public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException
}
/**
- * 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.
*
+ * 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.
+ *
* @param 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
- * {@code Collection}, you should use:
- *
- * Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
- *
- * @return an object of type T from the json. Returns {@code null} if {@code json} is at EOF.
+ * @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
+ * @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 fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
JsonReader jsonReader = newJsonReader(json);
+ @SuppressWarnings("unchecked")
T object = (T) fromJson(jsonReader, typeOfT);
assertFullConsumption(object, jsonReader);
return object;
}
+ /**
+ * 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.
+ *
+ * @param 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}, you should use:
+ *
+ * new TypeToken<Collection<Foo>>(){}
+ *
+ * @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 the erasure type
+ * of typeOfT
+ *
+ * @see #fromJson(String, TypeToken)
+ * @see #fromJson(Reader, Class)
+ */
+ public T fromJson(Reader json, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
+ return fromJson(json, typeOfT.getType());
+ }
+
private static void assertFullConsumption(Object obj, JsonReader reader) {
try {
if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) {
@@ -912,14 +1005,53 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
}
/**
- * 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 classOfT}. Returns {@code null}, if the {@code reader} is at EOF.
+ * 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 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(JsonReader, Type)}.
+ *
+ * @param the type of the desired object
+ * @param reader the reader whose next JSON value should be deserialized
+ * @param classOfT the class of T
+ * @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 classOfT
+ *
+ * @see #fromJson(Reader, Class)
+ * @see #fromJson(JsonReader, Type)
+ */
+ public T fromJson(JsonReader reader, Class classOfT) throws JsonIOException, JsonSyntaxException {
+ return fromJson(reader, (Type) classOfT);
+ }
+
+ /**
+ * 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
+ * This method is useful if the specified object is a generic type. For non-generic objects,
+ * use {@link #fromJson(JsonReader, Class)} instead.
*
- * @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
+ * 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.
+ *
+ * @param 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, Class)
+ * @see #fromJson(JsonReader, TypeToken)
*/
- @SuppressWarnings("unchecked")
public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
boolean isEmpty = true;
boolean oldLenient = reader.isLenient();
@@ -927,6 +1059,7 @@ public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
try {
reader.peek();
isEmpty = false;
+ @SuppressWarnings("unchecked")
TypeToken typeToken = (TypeToken) TypeToken.get(typeOfT);
TypeAdapter typeAdapter = getAdapter(typeToken);
T object = typeAdapter.read(reader);
@@ -955,21 +1088,51 @@ public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
}
/**
- * This method deserializes the Json read from the specified parse tree into an object of the
+ * 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,
+ * use {@link #fromJson(JsonReader, Class)} instead.
+ *
+ * @param 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}, you should use:
+ *
+ * new TypeToken<Collection<Foo>>(){}
+ *
+ * @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 erasure type
+ * of typeOfT
+ *
+ * @see #fromJson(Reader, Type)
+ * @see #fromJson(JsonReader, Class)
+ */
+ public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
+ return fromJson(reader, typeOfT.getType());
+ }
+
+ /**
+ * 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)}.
+ *
* @param 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, Type)
*/
public T fromJson(JsonElement json, Class classOfT) throws JsonSyntaxException {
Object object = fromJson(json, (Type) classOfT);
@@ -977,23 +1140,29 @@ public T fromJson(JsonElement json, Class classOfT) throws JsonSyntaxExce
}
/**
- * 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.
*
+ * 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 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
- * {@code Collection}, you should use:
- *
- * Type typeOfT = new TypeToken<Collection<Foo>>(){}.getType();
- *
- * @return an object of type T from the json. Returns {@code null} if {@code json} is {@code null}
+ * @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 fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
@@ -1003,6 +1172,34 @@ public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException
return (T) fromJson(new JsonTreeReader(json), 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 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 should create an anonymous subclass of
+ * {@code TypeToken} with the specific generic type arguments. For example, to get the type for
+ * {@code Collection}, you should use:
+ *
+ * new TypeToken<Collection<Foo>>(){}
+ *
+ * @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 the erasure type
+ * of typeOfT
+ * @since 1.3
+ *
+ * @see #fromJson(Reader, Type)
+ * @see #fromJson(JsonElement, Class)
+ * @see #fromJson(JsonElement, TypeToken)
+ */
+ public T fromJson(JsonElement json, TypeToken typeOfT) throws JsonSyntaxException {
+ return fromJson(json, typeOfT.getType());
+ }
+
static class FutureTypeAdapter extends TypeAdapter {
private TypeAdapter delegate;
diff --git a/gson/src/test/java/com/google/gson/MixedStreamTest.java b/gson/src/test/java/com/google/gson/MixedStreamTest.java
index 00eb4bc8a0..fa16659f0d 100644
--- a/gson/src/test/java/com/google/gson/MixedStreamTest.java
+++ b/gson/src/test/java/com/google/gson/MixedStreamTest.java
@@ -174,7 +174,7 @@ public void testReadNulls() {
} catch (NullPointerException expected) {
}
try {
- gson.fromJson(new JsonReader(new StringReader("true")), null);
+ gson.fromJson(new JsonReader(new StringReader("true")), (Type) null);
fail();
} catch (NullPointerException expected) {
}
From 789bb164893dff05803171c797e1e4179c00adf0 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Wed, 10 Jun 2020 15:49:32 +0200
Subject: [PATCH 02/11] Add to Gson.fromJson javadoc that JSON is fully
consumed
The newly added documentation deliberately does not state which exception
is thrown because Gson.assertFullConsumption could throw either a
JsonIOException or a JsonSyntaxException.
---
gson/src/main/java/com/google/gson/Gson.java | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 4eddcfd7d6..0121b0b2c3 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -829,6 +829,9 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
* {@link #fromJson(String, Type)}. If you have the JSON in a {@link Reader} instead of
* a String, use {@link #fromJson(Reader, Class)} instead.
*
+ * This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, Class)} if this behavior is not desired.
+ *
* @param the type of the desired object
* @param json the string from which the object is to be deserialized
* @param classOfT the class of T
@@ -857,6 +860,9 @@ public T fromJson(String json, Class classOfT) throws JsonSyntaxException
* 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.
+ *
+ *
This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
*
* @param the type of the desired object
* @param json the string from which the object is to be deserialized
@@ -885,6 +891,9 @@ public T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
* {@link #fromJson(String, Class)} instead. If you have the JSON in a {@link Reader} instead of
* a String, use {@link #fromJson(Reader, TypeToken)} instead.
*
+ * This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, TypeToken)} if this behavior is not desired.
+ *
* @param 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
@@ -914,6 +923,9 @@ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxExcept
* 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
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
+ *
+ * This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, Class)} if this behavior is not desired.
*
* @param the type of the desired object
* @param json the reader producing the JSON from which the object is to be deserialized.
@@ -945,6 +957,9 @@ public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException
* 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.
+ *
+ *
This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
*
* @param the type of the desired object
* @param json the reader producing JSON from which the object is to be deserialized
@@ -971,6 +986,9 @@ public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyn
* 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.
+ *
+ * This method tries to fully consume the JSON and throws an exception in case that is not
+ * possible. Use {@link #fromJson(JsonReader, TypeToken)} if this behavior is not desired.
*
* @param the type of the desired object
* @param json the reader producing JSON from which the object is to be deserialized
From 3b64053b11f36d1a614e5e6ba5f44840cec6526a Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Wed, 2 Sep 2020 18:10:51 +0200
Subject: [PATCH 03/11] Remove unnecessary wrapping and unwrapping as TypeToken
in Gson.fromJson
Since the actual implementation of Gson.fromJson is TypeToken based, the
TypeToken variant overloads are now the "main" implementation and the other
overloads delegate to them.
Previously the Type variant overloads were the "main" implementation which
caused `TypeToken.getType()` followed by `TypeToken.get(...)` when the
TypeToken variant overloads were used.
---
gson/src/main/java/com/google/gson/Gson.java | 99 ++++++++++----------
1 file changed, 48 insertions(+), 51 deletions(-)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 0121b0b2c3..19a6dcd0c0 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -875,14 +875,9 @@ public T fromJson(String json, Class classOfT) throws JsonSyntaxException
* @see #fromJson(String, Class)
* @see #fromJson(String, TypeToken)
*/
+ @SuppressWarnings("unchecked")
public T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
- if (json == null) {
- return null;
- }
- StringReader reader = new StringReader(json);
- @SuppressWarnings("unchecked")
- T target = (T) fromJson(reader, typeOfT);
- return target;
+ return (T) fromJson(json, TypeToken.get(typeOfT));
}
/**
@@ -911,7 +906,12 @@ public T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
* @see #fromJson(String, Class)
*/
public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxException {
- return fromJson(json, typeOfT.getType());
+ if (json == null) {
+ return null;
+ }
+ StringReader reader = new StringReader(json);
+ T target = fromJson(reader, typeOfT);
+ return target;
}
/**
@@ -939,9 +939,7 @@ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxExcept
* @see #fromJson(Reader, Type)
*/
public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException, JsonIOException {
- JsonReader jsonReader = newJsonReader(json);
- Object object = fromJson(jsonReader, classOfT);
- assertFullConsumption(object, jsonReader);
+ Object object = fromJson(json, (Type) classOfT);
return Primitives.wrap(classOfT).cast(object);
}
@@ -973,12 +971,9 @@ public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException
* @see #fromJson(Reader, Class)
* @see #fromJson(Reader, TypeToken)
*/
+ @SuppressWarnings("unchecked")
public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
- JsonReader jsonReader = newJsonReader(json);
- @SuppressWarnings("unchecked")
- T object = (T) fromJson(jsonReader, typeOfT);
- assertFullConsumption(object, jsonReader);
- return object;
+ return (T) fromJson(json, TypeToken.get(typeOfT));
}
/**
@@ -1007,7 +1002,10 @@ public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyn
* @see #fromJson(Reader, Class)
*/
public T fromJson(Reader json, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
- return fromJson(json, typeOfT.getType());
+ JsonReader jsonReader = newJsonReader(json);
+ T object = fromJson(jsonReader, typeOfT);
+ assertFullConsumption(object, jsonReader);
+ return object;
}
private static void assertFullConsumption(Object obj, JsonReader reader) {
@@ -1070,16 +1068,41 @@ public T fromJson(JsonReader reader, Class classOfT) throws JsonIOExcepti
* @see #fromJson(JsonReader, Class)
* @see #fromJson(JsonReader, TypeToken)
*/
+ @SuppressWarnings("unchecked")
public 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,
+ * use {@link #fromJson(JsonReader, Class)} instead.
+ *
+ * @param 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}, you should use:
+ *
+ * new TypeToken<Collection<Foo>>(){}
+ *
+ * @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 erasure type
+ * of typeOfT
+ *
+ * @see #fromJson(Reader, Type)
+ * @see #fromJson(JsonReader, Class)
+ */
+ public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
boolean isEmpty = true;
boolean oldLenient = reader.isLenient();
reader.setLenient(true);
try {
reader.peek();
isEmpty = false;
- @SuppressWarnings("unchecked")
- TypeToken typeToken = (TypeToken) TypeToken.get(typeOfT);
- TypeAdapter typeAdapter = getAdapter(typeToken);
+ TypeAdapter typeAdapter = getAdapter(typeOfT);
T object = typeAdapter.read(reader);
return object;
} catch (EOFException e) {
@@ -1105,32 +1128,6 @@ public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
}
}
- /**
- * 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,
- * use {@link #fromJson(JsonReader, Class)} instead.
- *
- * @param 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}, you should use:
- *
- * new TypeToken<Collection<Foo>>(){}
- *
- * @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 erasure type
- * of typeOfT
- *
- * @see #fromJson(Reader, Type)
- * @see #fromJson(JsonReader, Class)
- */
- public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
- return fromJson(reader, typeOfT.getType());
- }
-
/**
* 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
@@ -1184,10 +1181,7 @@ public T fromJson(JsonElement json, Class classOfT) throws JsonSyntaxExce
*/
@SuppressWarnings("unchecked")
public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException {
- if (json == null) {
- return null;
- }
- return (T) fromJson(new JsonTreeReader(json), typeOfT);
+ return (T) fromJson(json, TypeToken.get(typeOfT));
}
/**
@@ -1215,7 +1209,10 @@ public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException
* @see #fromJson(JsonElement, TypeToken)
*/
public T fromJson(JsonElement json, TypeToken typeOfT) throws JsonSyntaxException {
- return fromJson(json, typeOfT.getType());
+ if (json == null) {
+ return null;
+ }
+ return fromJson(new JsonTreeReader(json), typeOfT);
}
static class FutureTypeAdapter extends TypeAdapter {
From 04fbb6db246bafef2fe9d1d0b7f38d02bbd3969e Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Wed, 2 Sep 2020 18:11:50 +0200
Subject: [PATCH 04/11] Trim source code whitespaces
---
gson/src/main/java/com/google/gson/Gson.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 19a6dcd0c0..60237e9dcc 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -860,7 +860,7 @@ public T fromJson(String json, Class classOfT) throws JsonSyntaxException
* 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.
- *
+ *
*
This method tries to fully consume the JSON and throws an exception in case that is not
* possible. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
*
@@ -923,7 +923,7 @@ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxExcept
* 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
* {@link Reader}, use {@link #fromJson(String, Class)} instead.
- *
+ *
* This method tries to fully consume the JSON and throws an exception in case that is not
* possible. Use {@link #fromJson(JsonReader, Class)} if this behavior is not desired.
*
@@ -955,7 +955,7 @@ public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException
* 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.
- *
+ *
*
This method tries to fully consume the JSON and throws an exception in case that is not
* possible. Use {@link #fromJson(JsonReader, Type)} if this behavior is not desired.
*
@@ -981,7 +981,7 @@ public T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyn
* 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.
- *
+ *
* This method tries to fully consume the JSON and throws an exception in case that is not
* possible. Use {@link #fromJson(JsonReader, TypeToken)} if this behavior is not desired.
*
From 224db116e31e2ca95023c3ed94835b7b13de9df7 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Wed, 2 Sep 2020 18:14:19 +0200
Subject: [PATCH 05/11] Fix Gson.fromJson(JsonReader, Class) not casting read
Object
To be consistent with the other Gson.fromJson(..., Class) overloads the
method should cast the result.
---
gson/src/main/java/com/google/gson/Gson.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 60237e9dcc..6542e1bdea 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -1041,7 +1041,8 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
* @see #fromJson(JsonReader, Type)
*/
public T fromJson(JsonReader reader, Class classOfT) throws JsonIOException, JsonSyntaxException {
- return fromJson(reader, (Type) classOfT);
+ Object object = fromJson(reader, (Type) classOfT);
+ return Primitives.wrap(classOfT).cast(object);
}
/**
From b38ef2cf9fdc3177939c84e7db000e54fdd05398 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Fri, 16 Sep 2022 20:06:10 +0200
Subject: [PATCH 06/11] Replace User Guide link in Gson documentation
---
gson/src/main/java/com/google/gson/Gson.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 8cd2a3b1ea..fac0bb31c0 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -99,7 +99,7 @@
* List<MyType> target2 = gson.fromJson(json, listType);
*
*
- * See the Gson User Guide
+ *
See the Gson User Guide
* for a more complete set of examples.
*
* Lenient JSON handling
From 17b40f8effab21fb3b70fe74f272df8f2480d6c3 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Fri, 16 Sep 2022 20:21:46 +0200
Subject: [PATCH 07/11] Remove more references to fromJson(..., Type)
---
gson/src/main/java/com/google/gson/Gson.java | 61 +++++++++-----------
1 file changed, 26 insertions(+), 35 deletions(-)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index fac0bb31c0..83cb6aa9ea 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -941,7 +941,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
* 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.
*
* An exception is thrown if the JSON string has multiple top-level JSON elements, or if there
@@ -956,10 +956,10 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
* classOfT
*
* @see #fromJson(Reader, Class)
- * @see #fromJson(String, Type)
+ * @see #fromJson(String, TypeToken)
*/
public T fromJson(String json, Class classOfT) throws JsonSyntaxException {
- Object object = fromJson(json, (Type) classOfT);
+ T object = fromJson(json, TypeToken.get(classOfT));
return Primitives.wrap(classOfT).cast(object);
}
@@ -970,11 +970,9 @@ public T fromJson(String json, Class classOfT) throws JsonSyntaxException
* a String, use {@link #fromJson(Reader, Type)} instead.
*
* 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.
+ * 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.
*
*
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
@@ -1035,7 +1033,7 @@ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxExcept
* Therefore, this method should not be used if the desired type is a generic type. Note that
* 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.
*
* An exception is thrown if the JSON data has multiple top-level JSON elements, or if there
@@ -1050,10 +1048,10 @@ public T fromJson(String json, TypeToken typeOfT) throws JsonSyntaxExcept
* @since 1.2
*
* @see #fromJson(String, Class)
- * @see #fromJson(Reader, Type)
+ * @see #fromJson(Reader, TypeToken)
*/
public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException, JsonIOException {
- Object object = fromJson(json, (Type) classOfT);
+ T object = fromJson(json, TypeToken.get(classOfT));
return Primitives.wrap(classOfT).cast(object);
}
@@ -1064,11 +1062,9 @@ public T fromJson(Reader json, Class classOfT) throws JsonSyntaxException
* String form instead of a {@link Reader}, use {@link #fromJson(String, Type)} instead.
*
* 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.
+ * 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.
*
*
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.
@@ -1141,7 +1137,7 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
* Therefore, this method should not be used if the desired type is a generic type. Note that
* 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(JsonReader, Type)}.
+ * invoke {@link #fromJson(JsonReader, TypeToken)}.
*
* @param the type of the desired object
* @param reader the reader whose next JSON value should be deserialized
@@ -1151,10 +1147,10 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
* @throws JsonSyntaxException if json is not a valid representation for an object of type classOfT
*
* @see #fromJson(Reader, Class)
- * @see #fromJson(JsonReader, Type)
+ * @see #fromJson(JsonReader, TypeToken)
*/
public T fromJson(JsonReader reader, Class classOfT) throws JsonIOException, JsonSyntaxException {
- Object object = fromJson(reader, (Type) classOfT);
+ T object = fromJson(reader, TypeToken.get(classOfT));
return Primitives.wrap(classOfT).cast(object);
}
@@ -1165,11 +1161,9 @@ public T fromJson(JsonReader reader, Class classOfT) throws JsonIOExcepti
* use {@link #fromJson(JsonReader, Class)} instead.
*
* 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.
+ * 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.
*
*
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.
@@ -1212,7 +1206,7 @@ public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
* @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, Type)
+ * @see #fromJson(Reader, TypeToken)
* @see #fromJson(JsonReader, Class)
*/
public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOException, JsonSyntaxException {
@@ -1255,7 +1249,7 @@ public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOExce
* Therefore, this method should not be used if the desired type is a generic type. Note that
* 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 the type of the desired object
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
@@ -1267,10 +1261,10 @@ public T fromJson(JsonReader reader, TypeToken typeOfT) throws JsonIOExce
* @since 1.3
*
* @see #fromJson(Reader, Class)
- * @see #fromJson(JsonElement, Type)
+ * @see #fromJson(JsonElement, TypeToken)
*/
public T fromJson(JsonElement json, Class classOfT) throws JsonSyntaxException {
- Object object = fromJson(json, (Type) classOfT);
+ T object = fromJson(json, TypeToken.get(classOfT));
return Primitives.wrap(classOfT).cast(object);
}
@@ -1280,11 +1274,9 @@ public T fromJson(JsonElement json, Class classOfT) throws JsonSyntaxExce
* non-generic objects, use {@link #fromJson(JsonElement, Class)} instead.
*
* 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.
+ * 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 the type of the desired object
* @param json the root of the parse tree of {@link JsonElement}s from which the object is to
@@ -1323,9 +1315,8 @@ public T fromJson(JsonElement json, Type typeOfT) throws JsonSyntaxException
* @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT
* @since 1.3
*
- * @see #fromJson(Reader, Type)
+ * @see #fromJson(Reader, TypeToken)
* @see #fromJson(JsonElement, Class)
- * @see #fromJson(JsonElement, TypeToken)
*/
public T fromJson(JsonElement json, TypeToken typeOfT) throws JsonSyntaxException {
if (json == null) {
From 853d9f521f6999c3f0746a281f3433a4a8ed81a2 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Fri, 16 Sep 2022 20:27:49 +0200
Subject: [PATCH 08/11] Extend documentation for fromJson(JsonReader, ...)
---
gson/src/main/java/com/google/gson/Gson.java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java
index 83cb6aa9ea..a0c555c88d 100644
--- a/gson/src/main/java/com/google/gson/Gson.java
+++ b/gson/src/main/java/com/google/gson/Gson.java
@@ -1139,6 +1139,13 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
* object itself should not be a generic type. For the cases when the object is of generic type,
* invoke {@link #fromJson(JsonReader, TypeToken)}.
*
+ * 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.
+ *
+ *
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 the type of the desired object
* @param reader the reader whose next JSON value should be deserialized
* @param classOfT the class of T
@@ -1194,6 +1201,13 @@ public T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
* This method is useful if the specified object is a generic type. For non-generic objects,
* use {@link #fromJson(JsonReader, Class)} instead.
*
+ * 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.
+ *
+ *
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 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
From c34ba6c96c7fb4dffb88005c493f5d54f4be91f2 Mon Sep 17 00:00:00 2001
From: Marcono1234
Date: Sun, 18 Sep 2022 18:24:27 +0200
Subject: [PATCH 09/11] Replace some TypeToken.getType() usages
---
UserGuide.md | 4 ++--
.../metrics/CollectionsDeserializationBenchmark.java | 9 +++++----
.../java/com/google/gson/metrics/ParseBenchmark.java | 4 ++--
3 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/UserGuide.md b/UserGuide.md
index 12b53351bd..2aafb067e8 100644
--- a/UserGuide.md
+++ b/UserGuide.md
@@ -225,7 +225,7 @@ Collection ints = Arrays.asList(1,2,3,4,5);
String json = gson.toJson(ints); // ==> json is [1,2,3,4,5]
// Deserialization
-Type collectionType = new TypeToken>(){}.getType();
+TypeToken> collectionType = new TypeToken>(){};
Collection ints2 = gson.fromJson(json, collectionType);
// ==> ints2 is same as ints
```
@@ -263,7 +263,7 @@ For deserialization Gson uses the `read` method of the `TypeAdapter` registered
```java
Gson gson = new Gson();
-Type mapType = new TypeToken