{"id":"0e3c1b3363f7d4c9","repo":"google/gson","slug":"string-value-is-empty","errorCode":null,"errorMessage":"String value is empty","messagePattern":"String value is empty","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"gson/src/main/java/com/google/gson/JsonPrimitive.java","lineNumber":253,"sourceCode":"  /**\n   * @throws NumberFormatException {@inheritDoc}\n   */\n  @Override\n  public byte getAsByte() {\n    return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());\n  }\n\n  /**\n   * @throws UnsupportedOperationException if the string value of this primitive is empty.\n   * @deprecated This method is misleading, as it does not get this element as a char but rather as\n   *     a string's first character.\n   */\n  @Deprecated\n  @Override\n  public char getAsCharacter() {\n    String s = getAsString();\n    if (s.isEmpty()) {\n      throw new UnsupportedOperationException(\"String value is empty\");\n    } else {\n      return s.charAt(0);\n    }\n  }\n\n  /** Returns the hash code of this object. */\n  @Override\n  public int hashCode() {\n    if (value == null) {\n      return 31;\n    }\n    // The conditions below parallel the structure of equals(Object). Unlike in equals, every\n    // numeric branch must delegate to the same double-based hash: equals can consider primitives\n    // from *different* branches equal (for example Integer 42 equals Double 42.0 and a lazily\n    // parsed number 42 through the floating-point comparison), so a branch hashing anything other\n    // than the double value would give equal primitives different hash codes, see\n    // https://github.com/google/gson/issues/992\n    if (isIntegral(this)) {","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonPrimitive.java#L235-L271","documentation":"JsonPrimitive.getAsCharacter() (deprecated) returns the first char of the stored string; if the string is empty there is no character to return, so it throws UnsupportedOperationException. The deprecation note says the method is misleading (it is not a real char coercion).","triggerScenarios":"Calling getAsCharacter() on a JsonPrimitive built from \"\" or parsed from a JSON empty string \"\".","commonSituations":"Legacy code still using the deprecated method; APIs that return empty strings for optional char-like fields; data migration leaving empty placeholders.","solutions":["Stop using the deprecated getAsCharacter(); use getAsString() and handle length explicitly.","Guard with if (!s.isEmpty()) charAt(0);","Replace char handling with String throughout to avoid the single-char assumption."],"exampleFix":"// before (deprecated)\nchar c = prim.getAsCharacter();\n\n// after\nString s = prim.getAsString();\nchar c = s.isEmpty() ? '\\0' : s.charAt(0);","handlingStrategy":"validation","validationCode":"String s = prim.getAsString();\nif (s.isEmpty()) return /* default */;\n","typeGuard":"boolean hasChar(JsonPrimitive p) {\n  return p.isString() && !p.getAsString().isEmpty();\n}\n","tryCatchPattern":"try {\n  char c = prim.getAsCharacter();\n} catch (UnsupportedOperationException ex) {\n  c = '\\0';\n}\n","preventionTips":["Avoid the deprecated getAsCharacter(); use getAsString().charAt(0) with length check.","Prefer String-typed fields over char for JSON interchange."],"tags":["json-tree","deprecated","type-coercion"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}