{"id":"04d04c99f2f3278d","repo":"google/gson","slug":"expecting-character-got-str-at-in-g","errorCode":null,"errorMessage":"Expecting character, got: \" + str + \"; at \" + in.getPreviousPath()","messagePattern":"Expecting character, got: \" \\+ str \\+ \"; at \" \\+ in\\.getPreviousPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java","lineNumber":542,"sourceCode":"  }\n\n  public static final TypeAdapter<Number> FLOAT = new FloatAdapter(false);\n  public static final TypeAdapter<Number> FLOAT_STRICT = new FloatAdapter(true);\n\n  public static final TypeAdapter<Number> DOUBLE = new DoubleAdapter(false);\n  public static final TypeAdapter<Number> DOUBLE_STRICT = new DoubleAdapter(true);\n\n  public static final TypeAdapter<Character> CHARACTER =\n      new TypeAdapter<Character>() {\n        @Override\n        public Character read(JsonReader in) throws IOException {\n          if (in.peek() == JsonToken.NULL) {\n            in.nextNull();\n            return null;\n          }\n          String str = in.nextString();\n          if (str.length() != 1) {\n            throw new JsonSyntaxException(\n                \"Expecting character, got: \" + str + \"; at \" + in.getPreviousPath());\n          }\n          return str.charAt(0);\n        }\n\n        @Override\n        public void write(JsonWriter out, Character value) throws IOException {\n          out.value(value == null ? null : String.valueOf(value));\n        }\n      };\n\n  public static final TypeAdapterFactory CHARACTER_FACTORY =\n      newFactory(char.class, Character.class, CHARACTER);\n\n  public static final TypeAdapter<String> STRING =\n      new TypeAdapter<String>() {\n        @Override\n        public String read(JsonReader in) throws IOException {","sourceCodeStart":524,"sourceCodeEnd":560,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/TypeAdapters.java#L524-L560","documentation":"The CHARACTER TypeAdapter reads a JSON string and expects exactly one character. If the string length is not 1 (empty, or two+ characters), Gson throws JsonSyntaxException with the offending string and previous path. A null token is handled separately (returns null), so this fires only on non-empty multi-char or empty strings.","triggerScenarios":"Deserialifying a JSON string value into a char/Character field where the value is empty (\"\") or has more than one character (e.g., \"ab\"). Triggered at line 542 when str.length()!=1.","commonSituations":"API fields returning full words or codes for what was modeled as a single char; empty-string defaults from optional fields; unicode code points represented as surrogate pairs (two chars); status fields that grew beyond one character.","solutions":["Change the field type from char/Character to String if multi-character values are expected.","Sanitize the source to emit exactly one character (or empty -> '\\u0000').","Register a custom TypeAdapter<Character> that picks the first char or maps empty to a default.","Validate the JSON value length before deserialization."],"exampleFix":"// before\nclass Flag { char code; }\ngson.fromJson(\"{\\\"code\\\":\\\"OK\\\"}\", Flag.class); // throws\n\n// after\nclass Flag { String code; }\n// or custom adapter\nregisterTypeAdapter(char.class, new TypeAdapter<Character>() {\n  public Character read(JsonReader r) throws IOException {\n    String s = r.nextString(); return s.isEmpty() ? '\\0' : s.charAt(0);\n  }\n  public void write(JsonWriter w, Character c) throws IOException { w.value(String.valueOf(c)); }\n}.nullSafe());","handlingStrategy":"validation","validationCode":"// Validate single-char constraint before deserializing into a char field\nString s = JsonParser.parseString(json).getAsJsonObject().get(\"code\").getAsString();\nif (s == null || s.length() != 1) throw new IllegalArgumentException(\"Expecting char, got: \" + s);","typeGuard":"null","tryCatchPattern":"try {\n  gson.fromJson(json, Flag.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage().startsWith(\"Expecting character, got:\")) {\n    // widen field to String or sanitize to first char\n  } else throw e;\n}","preventionTips":["Model free-text fields as String, not char.","Validate source length at the trust boundary.","Register a tolerant char adapter if single-char semantics are intentional."],"tags":["gson","character","deserialization","string","validation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}