{"record":{"id":"c1f84bef5b08905f","repo":"google/gson","slug":"attempted-to-serialize-java-lang-class-classnam","errorCode":null,"errorMessage":"Attempted to serialize java.lang.Class: ${className}. Forgot to register a type adapter?\nSee ${url}","messagePattern":"Attempted to serialize java\\.lang\\.Class: (.+?)\\. Forgot to register a type adapter\\?\nSee (.+?)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":73,"sourceCode":"import java.util.concurrent.atomic.AtomicLong;\nimport java.util.concurrent.atomic.AtomicLongArray;\nimport java.util.regex.Pattern;\n\n/**\n * Type adapters for basic types. More complex adapters exist as separate classes in the enclosing\n * package.\n */\npublic final class TypeAdapters {\n  private TypeAdapters() {\n    throw new UnsupportedOperationException();\n  }\n\n  @SuppressWarnings(\"rawtypes\")\n  public static final TypeAdapter<Class> CLASS =\n      new TypeAdapter<Class>() {\n        @Override\n        public void write(JsonWriter out, Class value) throws IOException {\n          throw new UnsupportedOperationException(\n              \"Attempted to serialize java.lang.Class: \"\n                  + value.getName()\n                  + \". Forgot to register a type adapter?\"\n                  + \"\\nSee \"\n                  + TroubleshootingGuide.createUrl(\"java-lang-class-unsupported\"));\n        }\n\n        @Override\n        public Class read(JsonReader in) throws IOException {\n          throw new UnsupportedOperationException(\n              \"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?\"\n                  + \"\\nSee \"\n                  + TroubleshootingGuide.createUrl(\"java-lang-class-unsupported\"));\n        }\n      }.nullSafe();\n\n  public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS);\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L55-L91","documentation":"Gson registers a built-in CLASS adapter that throws UnsupportedOperationException on write because serializing a java.lang.Class object is not meaningful. The adapter fires whenever Gson encounters a Class field during serialization and no custom adapter was registered.","triggerScenarios":"Serializing an object that has a field of type Class<?> (e.g. Class<?> type) via gson.toJson() without registering a custom TypeAdapter for Class.","commonSituations":"Domain models that store a Class reference for runtime type checks; ORM entities with a discriminator class field; accidentally including a Class field in a serializable DTO.","solutions":["Mark the Class field with transient or @Expose(serialize = false) to exclude it from serialization","Register a custom JsonSerializer<Class<?>> that serializes the class name as a string","Remove the Class field from the model if it should not appear in JSON"],"exampleFix":"// before\nclass Entity {\n    String name;\n    Class<?> type; // causes UnsupportedOperationException on serialize\n}\n\n// after\nclass Entity {\n    String name;\n    @Expose(serialize = false)\n    transient Class<?> type;\n}","handlingStrategy":"validation","validationCode":"// Check model classes for Class fields before serializing\npublic static boolean hasClassField(Class<?> type) {\n    for (Field f : type.getDeclaredFields()) {\n        if (f.getType() == Class.class && !Modifier.isTransient(f.getModifiers())) {\n            return true; // will trigger UnsupportedOperationException on serialize\n        }\n    }\n    return false;\n}","typeGuard":null,"tryCatchPattern":"try {\n    String json = gson.toJson(obj);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().contains(\"Attempted to serialize java.lang.Class\")) {\n        // register a TypeAdapter<Class<?>> or mark the field transient\n    }\n}","preventionTips":["Mark Class<?> fields with transient or @Expose(serialize = false)","Avoid putting Class references in serializable DTOs; store a type-name string instead","Register a custom JsonSerializer<Class<?>> if class names must appear in JSON output"],"tags":["serialization","class","type-adapter"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}