{"record":{"id":"9ed6d8bf44e0fd54","repo":"Grasscutters/Grasscutter","slug":"invalid-enum-definition","errorCode":null,"errorMessage":"Invalid Enum definition - ","messagePattern":"Invalid Enum definition - ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/emu/grasscutter/utils/JsonAdapters.java","lineNumber":194,"sourceCode":"                        for (var constant : enumConstants) {\n                            var accessible = f.canAccess(constant);\n                            f.setAccessible(true);\n                            map.put(String.valueOf(f.getInt(constant)), constant);\n                            f.setAccessible(accessible);\n                        }\n                    } catch (IllegalAccessException e) {\n                        // System.out.println(\"Failed to access enum id field.\");\n                    }\n                    break;\n                }\n            }\n\n            return new TypeAdapter<>() {\n                public T read(JsonReader reader) throws IOException {\n                    return switch (reader.peek()) {\n                        case STRING -> map.get(reader.nextString());\n                        case NUMBER -> map.get(String.valueOf(reader.nextInt()));\n                        default -> throw new IOException(\"Invalid Enum definition - \" + reader.peek().name());\n                    };\n                }\n\n                public void write(JsonWriter writer, T value) throws IOException {\n                    writer.value(value.toString());\n                }\n            };\n        }\n    }\n}\n","sourceCodeStart":176,"sourceCodeEnd":205,"githubUrl":"https://github.com/Grasscutters/Grasscutter/blob/f373827a83d3e688fd3c6afb1e1c3759ca1d61c9/src/main/java/emu/grasscutter/utils/JsonAdapters.java#L176-L205","documentation":"The enum TypeAdapter factory maps enum constants by name string and by integer ordinal-as-string; any other JSON token (object, array, boolean, null) cannot represent an enum and throws this IOException. Note the returned map.get(...) may itself yield null for unknown names/numbers, but non-scalar tokens hit this throw directly.","triggerScenarios":"Deserializing an enum-typed field where the JSON value is an object, array, boolean, or null token; e.g. \"openState\": {\"value\":1} or \"openState\": [1] instead of \"openState\": 1 or \"openState\": \"OPEN\".","commonSituations":"Configs exported with wrapped enum objects by other frameworks; null enum values in JSON where the field is non-nullable in Java; hand-edited resources using wrong value types.","solutions":["Change the JSON value to the enum's name string (e.g. \"OPEN\") or its integer form","Replace JSON null with a valid enum name, or declare the field nullable/skip it","Preprocess input JSON to unwrap nested enum objects before Gson parsing","Check which enum and file failed (message appends reader.peek().name()) and fix that value"],"exampleFix":"// before\n\"openState\": {\"id\": 1}\n// after\n\"openState\": 1","handlingStrategy":"validation","validationCode":"boolean isValidEnumValue(Class<? extends Enum<?>> type, com.google.gson.JsonElement el) {\n  if (el == null || !el.isJsonPrimitive()) return false;\n  String s = el.getAsString();\n  for (Enum<?> c : type.getEnumConstants())\n    if (c.name().equals(s) || String.valueOf(c.ordinal()).equals(s)) return true;\n  return false;\n}","typeGuard":"boolean isEnumToken(com.google.gson.JsonElement el) {\n  return el != null && el.isJsonPrimitive()\n      && (el.getAsJsonPrimitive().isString() || el.getAsJsonPrimitive().isNumber());\n}","tryCatchPattern":"try {\n  MyEnum v = gson.fromJson(el, MyEnum.class);\n} catch (IOException e) {\n  logger.warn(\"Bad enum token: {}\", e.getMessage());\n}","preventionTips":["Write enums as name strings or plain integers in JSON","Never put null or objects in enum-typed fields","Validate enum fields against Enum.valueOf before parsing","Keep configs generated by the same schema that reads them"],"tags":["json","gson","enum","deserialization"],"backgroundTag":"enum-deserialization-failed","analyzedSha":"f373827a83d3e688fd3c6afb1e1c3759ca1d61c9","analyzedAt":"2026-09-03T22:43:49.407Z","contentChangedAt":"2026-09-03T22:43:49.407Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}