Skip to content

Commit

Permalink
Remove fromJson(JsonReader, Class) again
Browse files Browse the repository at this point in the history
As noticed during review adding this method is source incompatible.
  • Loading branch information
Marcono1234 committed Sep 19, 2022
1 parent 43ebe28 commit 7bf72a8
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions gson/src/main/java/com/google/gson/Gson.java
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
* 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. Use {@link #fromJson(JsonReader, Class)} if this behavior is not desired.
* 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
Expand Down Expand Up @@ -1039,7 +1039,7 @@ public <T> T fromJson(String json, TypeToken<T> typeOfT) throws JsonSyntaxExcept
* {@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. Use {@link #fromJson(JsonReader, Class)} if this behavior is not desired.
* 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.
Expand Down Expand Up @@ -1131,48 +1131,18 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
}
}

/**
* 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, TypeToken)}.
*
* <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 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, TypeToken)
*/
public <T> T fromJson(JsonReader reader, Class<T> classOfT) throws JsonIOException, JsonSyntaxException {
T object = fromJson(reader, TypeToken.get(classOfT));
return Primitives.wrap(classOfT).cast(object);
}
// 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/files#r973764414

/**
* 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.
*
* <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.
* 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.
Expand All @@ -1189,7 +1159,6 @@ public <T> T fromJson(JsonReader reader, Class<T> classOfT) throws JsonIOExcepti
* @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")
Expand All @@ -1201,7 +1170,8 @@ public <T> 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.
* {@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.
Expand All @@ -1223,7 +1193,7 @@ public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
* @throws JsonSyntaxException if json is not a valid representation for an object of the type typeOfT
*
* @see #fromJson(Reader, TypeToken)
* @see #fromJson(JsonReader, Class)
* @see #fromJson(JsonReader, Type)
*/
public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOException, JsonSyntaxException {
boolean isEmpty = true;
Expand Down

0 comments on commit 7bf72a8

Please sign in to comment.