Printing more debugging information to help track an invalid JsonAdapter. (#1068)

Now the thrown exception carries this information:
java.lang.IllegalArgumentException: Invalid attempt to bind an instance of java.lang.Integer as a @JsonAdapter for com.google.gson.functional.JsonAdapterAnnotationOnClassesTest$D. @JsonAdapter value must be a TypeAdapter, TypeAdapterFactory, JsonSerializer or JsonDeserializer.
This commit is contained in:
inder123 2017-04-19 17:08:21 -07:00 committed by GitHub
parent 9a2421997e
commit 5412f21431
3 changed files with 16 additions and 3 deletions

View File

@ -136,6 +136,7 @@ public final class JsonObject extends JsonElement {
* Returns a set of members key values.
*
* @return a set of member keys as Strings
* @since 2.8.1
*/
public Set<String> keySet() {
return members.keySet();

View File

@ -68,9 +68,10 @@ public final class JsonAdapterAnnotationTypeAdapterFactory implements TypeAdapte
: null;
typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
} else {
throw new IllegalArgumentException(
"@JsonAdapter value must be TypeAdapter, TypeAdapterFactory, "
+ "JsonSerializer or JsonDeserializer reference.");
throw new IllegalArgumentException("Invalid attempt to bind an instance of "
+ instance.getClass().getName() + " as a @JsonAdapter for " + type.toString()
+ ". @JsonAdapter value must be a TypeAdapter, TypeAdapterFactory,"
+ " JsonSerializer or JsonDeserializer.");
}
if (typeAdapter != null && annotation.nullSafe()) {

View File

@ -249,4 +249,15 @@ public final class JsonAdapterAnnotationOnClassesTest extends TestCase {
return Foo.valueOf(in.nextString().toUpperCase(Locale.US));
}
}
public void testIncorrectJsonAdapterType() {
try {
new Gson().toJson(new D());
fail();
} catch (IllegalArgumentException expected) {}
}
@JsonAdapter(Integer.class)
private static final class D {
@SuppressWarnings("unused") final String value = "a";
}
}