google/gson · warning · UnsupportedOperationException
String value is empty
Error message
String value is empty
What it means
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).
Source
Thrown at gson/src/main/java/com/google/gson/JsonPrimitive.java:253
/**
* @throws NumberFormatException {@inheritDoc}
*/
@Override
public byte getAsByte() {
return isNumber() ? getAsNumber().byteValue() : Byte.parseByte(getAsString());
}
/**
* @throws UnsupportedOperationException if the string value of this primitive is empty.
* @deprecated This method is misleading, as it does not get this element as a char but rather as
* a string's first character.
*/
@Deprecated
@Override
public char getAsCharacter() {
String s = getAsString();
if (s.isEmpty()) {
throw new UnsupportedOperationException("String value is empty");
} else {
return s.charAt(0);
}
}
/** Returns the hash code of this object. */
@Override
public int hashCode() {
if (value == null) {
return 31;
}
// The conditions below parallel the structure of equals(Object). Unlike in equals, every
// numeric branch must delegate to the same double-based hash: equals can consider primitives
// from *different* branches equal (for example Integer 42 equals Double 42.0 and a lazily
// parsed number 42 through the floating-point comparison), so a branch hashing anything other
// than the double value would give equal primitives different hash codes, see
// https://github.com/google/gson/issues/992
if (isIntegral(this)) {View on GitHub (pinned to 8b8628c656)
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.
Example fix
// before (deprecated) char c = prim.getAsCharacter(); // after String s = prim.getAsString(); char c = s.isEmpty() ? '\0' : s.charAt(0);
Defensive patterns
Strategy: validation
Validate before calling
String s = prim.getAsString(); if (s.isEmpty()) return /* default */;
Type guard
boolean hasChar(JsonPrimitive p) {
return p.isString() && !p.getAsString().isEmpty();
}
Try / catch
try {
char c = prim.getAsCharacter();
} catch (UnsupportedOperationException ex) {
c = '\0';
}
Prevention
- Avoid the deprecated getAsCharacter(); use getAsString().charAt(0) with length check.
- Prefer String-typed fields over char for JSON interchange.
When it happens
Trigger: Calling getAsCharacter() on a JsonPrimitive built from "" or parsed from a JSON empty string "".
Common situations: Legacy code still using the deprecated method; APIs that return empty strings for optional char-like fields; data migration leaving empty placeholders.
Related errors
- Primitive is neither a number nor a string
- JsonReader is closed
- Custom JsonElement subclass {className} is not supported
- Expected {expected} but was {peek}{location}
- Expected STRING but was {token}{location}
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/0e3c1b3363f7d4c9.json.
Report an issue: GitHub.