google/gson · error · IllegalArgumentException
Class {typeAdapter.getClass().getName()} does not implement
Error message
Class {typeAdapter.getClass().getName()} does not implement any supported type adapter class or interface What it means
Thrown by registerTypeAdapter(Type, Object) when the supplied object implements none of TypeAdapter, JsonSerializer, JsonDeserializer, or InstanceCreator. Gson inspects the runtime type with instanceof and refuses to register an object it cannot dispatch on.
Source
Thrown at gson/src/main/java/com/google/gson/GsonBuilder.java:747
* TypeAdapter} should be used instead.
*
* @param type the type definition for the type adapter being registered
* @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 {@code Object}
* class or {@link JsonElement} or any of its subclasses
* @see #registerTypeHierarchyAdapter(Class, Object)
*/
@CanIgnoreReturnValue
public GsonBuilder registerTypeAdapter(Type type, Object typeAdapter) {
Objects.requireNonNull(type);
Objects.requireNonNull(typeAdapter);
if (!(typeAdapter instanceof JsonSerializer<?>
|| typeAdapter instanceof JsonDeserializer<?>
|| typeAdapter instanceof InstanceCreator<?>
|| typeAdapter instanceof TypeAdapter<?>)) {
throw new IllegalArgumentException(
"Class "
+ typeAdapter.getClass().getName()
+ " does not implement any supported type adapter class or interface");
}
if (hasNonOverridableAdapter(type)) {
throw new IllegalArgumentException("Cannot override built-in adapter for " + type);
}
if (typeAdapter instanceof InstanceCreator<?>) {
instanceCreators.put(type, (InstanceCreator<?>) typeAdapter);
}
if (typeAdapter instanceof JsonSerializer<?> || typeAdapter instanceof JsonDeserializer<?>) {
TypeToken<?> typeToken = TypeToken.get(type);
factories.add(TreeTypeAdapter.newFactoryWithMatchRawType(typeToken, typeAdapter));
}
if (typeAdapter instanceof TypeAdapter<?>) {
@SuppressWarnings({"unchecked", "rawtypes"})View on GitHub (pinned to 8b8628c656)
Solutions
- Make the object implement at least one of TypeAdapter<?>, JsonSerializer<?>, JsonDeserializer<?>, or InstanceCreator<?>.
- Confirm you are passing an instance, not a Class literal (drop the .class, add new).
- If using generics, declare the type parameter on the implements clause, e.g. class FooAdapter extends TypeAdapter<Foo>.
- If you want factory-based dispatch, use registerTypeAdapterFactory(factory) instead.
Example fix
// before
gsonBuilder.registerTypeAdapter(Foo.class, new FooSerializer()); // FooSerializer implements nothing Gson knows
// after
class FooSerializer implements JsonSerializer<Foo> {
public JsonElement serialize(Foo src, Type t, JsonSerializationContext c) { ... }
}
gsonBuilder.registerTypeAdapter(Foo.class, new FooSerializer()); Defensive patterns
Strategy: validation
Validate before calling
// Assert the contract before registering
Object adapter = new FooAdapter();
boolean ok = adapter instanceof com.google.gson.TypeAdapter
|| adapter instanceof com.google.gson.JsonSerializer
|| adapter instanceof com.google.gson.JsonDeserializer
|| adapter instanceof com.google.gson.InstanceCreator;
if (!ok) throw new IllegalStateException("adapter implements no Gson interface");
gsonBuilder.registerTypeAdapter(Foo.class, adapter); Type guard
// Type guard narrowing to a Gson-supported adapter
boolean isGsonAdapter(Object o) {
return o instanceof com.google.gson.TypeAdapter
|| o instanceof com.google.gson.JsonSerializer
|| o instanceof com.google.gson.JsonDeserializer
|| o instanceof com.google.gson.InstanceCreator;
} Prevention
- Implement the Gson interface on the adapter class, not just matching method names.
- Pass new FooAdapter(), never FooAdapter.class.
- Register adapters in a single builder-config method you can unit-test.
When it happens
Trigger: Calling gsonBuilder.registerTypeAdapter(MyType.class, obj) where obj is a plain POJO, a lambda with no matching functional interface, a class whose generic implements were erased, or the Class literal instead of an instance.
Common situations: Forgetting to extend TypeAdapter<T> and only adding a method named write/read; passing new MyClass() where MyClass implements only a custom (non-Gson) interface; passing a JsonSerializer with a raw/missing type parameter; accidentally registering the adapter class object (FooAdapter.class) instead of new FooAdapter().
Related errors
- Cannot override built-in adapter for {type}
- GSON ({GsonBuildConfig.VERSION}) cannot handle {type}
- GSON cannot serialize or deserialize {type}
- Invalid style: {style}
- Invalid attempt to bind an instance of {className} as a @Jso
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/7f636ae9a998dfbd.json.
Report an issue: GitHub.