Skip to content

Commit

Permalink
#2436 - Updates to User guide & comments to indicate exception cases …
Browse files Browse the repository at this point in the history
…and fix for 7 test cases of Parameterized Type
  • Loading branch information
Sachin Patil committed Aug 25, 2023
1 parent 15a11a9 commit 0218548
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 3 additions & 1 deletion UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,9 @@ gson.registerTypeAdapter(MyType.class, new MyDeserializer());
gson.registerTypeAdapter(MyType.class, new MyInstanceCreator());
```

`registerTypeAdapter` call checks if the type adapter implements more than one of these interfaces and register it for all of them.
`registerTypeAdapter` call checks
1. if the type adapter implements more than one of these interfaces and register it for all of them.
2. if the type adapter is for Object class or (JsonElements or any of its subclasses), it throws IllegalArgumentException.

#### Writing a Serializer

Expand Down
11 changes: 8 additions & 3 deletions gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -337,7 +339,7 @@ public GsonBuilder enableComplexMapKeySerialization() {
*
* <p>In general using inner classes with Gson should be avoided; they should be converted to {@code static}
* nested classes if possible.
*
*Ø
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @since 1.3
*/
Expand Down Expand Up @@ -664,6 +666,7 @@ public GsonBuilder setDateFormat(int dateStyle, int timeStyle) {
* @param typeAdapter This object must implement at least one of the {@link TypeAdapter},
* {@link InstanceCreator}, {@link JsonSerializer}, and a {@link JsonDeserializer} interfaces.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @throws IllegalArgumentException if the Type adapter being registered is for Object class or (JsonElements or any of its subclasses)
*/
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Expand All @@ -689,8 +692,9 @@ public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
}

private boolean isTypeObjectOrJsonElements(Type type) {
return (type == Object.class
|| JsonElement.class.isAssignableFrom((Class<?>) type));
return (!(type instanceof ParameterizedType) &&
(type == Object.class
|| JsonElement.class.isAssignableFrom((Class<?>) type)));
}

/**
Expand Down Expand Up @@ -724,6 +728,7 @@ public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) {
* @param typeAdapter This object must implement at least one of {@link TypeAdapter},
* {@link JsonSerializer} or {@link JsonDeserializer} interfaces.
* @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern
* @throws IllegalArgumentException if the Type adapter being registered is for a JsonElements or any of its subclasses
* @since 1.7
*/
@CanIgnoreReturnValue
Expand Down
5 changes: 2 additions & 3 deletions gson/src/test/java/com/google/gson/GsonBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public void testRegisterTypeAdapterForCoreType() {
String.class,
};
for (Type type : types) {
GsonBuilder gsonBuilder = new GsonBuilder();
assertThat(gsonBuilder.registerTypeAdapter(type, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder);
new GsonBuilder().registerTypeAdapter(type, NULL_TYPE_ADAPTER);
}
}

Expand Down Expand Up @@ -282,6 +281,6 @@ public void testRegisterTypeHierarchyAdapterJsonElements() {
assertThrows(IllegalArgumentException.class,
() -> gsonBuilder.registerTypeHierarchyAdapter((Class<?>) type, NULL_TYPE_ADAPTER));
}
assertThat(gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER)).isEqualTo(gsonBuilder);
gsonBuilder.registerTypeHierarchyAdapter(Object.class, NULL_TYPE_ADAPTER);
}
}

0 comments on commit 0218548

Please sign in to comment.