{"record":{"id":"e4e56a1fee93f340","repo":"google/gson","slug":"class-declaringtypename-declares-multiple-json","errorCode":null,"errorMessage":"Class ${declaringTypeName} declares multiple JSON fields named '${duplicateName}'; conflict is caused by fields ${field1} and ${field2}\nSee ${url}","messagePattern":"Class (.+?) declares multiple JSON fields named '(.+?)'; conflict is caused by fields (.+?) and (.+?)\nSee (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java","lineNumber":307,"sourceCode":"  }\n\n  private static class FieldsData {\n    static final FieldsData EMPTY = new FieldsData(Collections.emptyMap(), Collections.emptyList());\n\n    /** Maps from JSON member name to field */\n    final Map<String, BoundField> deserializedFields;\n\n    final List<BoundField> serializedFields;\n\n    FieldsData(Map<String, BoundField> deserializedFields, List<BoundField> serializedFields) {\n      this.deserializedFields = deserializedFields;\n      this.serializedFields = serializedFields;\n    }\n  }\n\n  private static IllegalArgumentException createDuplicateFieldException(\n      Class<?> declaringType, String duplicateName, Field field1, Field field2) {\n    throw new IllegalArgumentException(\n        \"Class \"\n            + declaringType.getName()\n            + \" declares multiple JSON fields named '\"\n            + duplicateName\n            + \"'; conflict is caused by fields \"\n            + ReflectionHelper.fieldToString(field1)\n            + \" and \"\n            + ReflectionHelper.fieldToString(field2)\n            + \"\\nSee \"\n            + TroubleshootingGuide.createUrl(\"duplicate-fields\"));\n  }\n\n  private FieldsData getBoundFields(\n      Gson context, TypeToken<?> type, Class<?> raw, boolean blockInaccessible, boolean isRecord) {\n    if (raw.isInterface()) {\n      return FieldsData.EMPTY;\n    }\n","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java#L289-L325","documentation":"When building a reflective adapter, Gson maps JSON names to fields. If two fields resolve to the same JSON name it cannot disambiguate, so it throws IllegalArgumentException during Gson.create() (before any data is processed). This catches both deserialization-name and serialization-name collisions across the entire class hierarchy.","triggerScenarios":"A subclass field shadows a parent field with the same name or @SerializedName; two fields with different Java names map to the same JSON key under a FieldNamingStrategy; two @SerializedName annotations specify the same value.","commonSituations":"Inheritance hierarchies where a child redeclares a parent field; refactoring a field name without checking the naming policy output; bulk-applying @SerializedName aliases that collide.","solutions":["Annotate one field with @SerializedName to give it a distinct JSON name","Exclude the duplicate field from serialization/deserialization with transient or @Expose","Rename one of the Java fields so the FieldNamingStrategy produces distinct JSON keys"],"exampleFix":"// before\nclass Parent { @SerializedName(\"name\") String fullName; }\nclass Child extends Parent { @SerializedName(\"name\") String nickName; }\nnew GsonBuilder().create(); // throws at build time\n\n// after\nclass Child extends Parent { @SerializedName(\"nickName\") String nickName; }","handlingStrategy":"validation","validationCode":"// At app startup, build Gson eagerly to catch duplicate-field errors before processing data\nGson gson;\ntry {\n    gson = new GsonBuilder().create();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"multiple JSON fields named\")) {\n        throw new IllegalStateException(\"Gson field-name collision detected: \" + e.getMessage(), e);\n    }\n    throw e;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build the Gson instance once at startup so duplicate-field errors surface immediately, not at runtime","When using inheritance, check that child fields do not shadow parent fields with the same JSON name","Review @SerializedName values across the class hierarchy for collisions"],"tags":["serialization","field-naming","duplicate","inheritance"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}