google/gson · error · UnsupportedOperationException
Primitive is neither a number nor a string
Error message
Primitive is neither a number nor a string
What it means
JsonPrimitive.getAsNumber() returns the stored Number or wraps a String in LazilyParsedNumber; if the wrapped value is neither (effectively a Boolean, the only other primitive type), it throws UnsupportedOperationException. The primitive exists but cannot be viewed as a Number.
Source
Thrown at gson/src/main/java/com/google/gson/JsonPrimitive.java:145
public boolean isNumber() {
return value instanceof Number;
}
/**
* Convenience method to get this element as a {@link Number}. If this primitive {@linkplain
* #isString() is a string}, a lazily parsed {@code Number} is constructed which parses the string
* when any of its methods are called (which can lead to a {@link NumberFormatException}).
*
* @throws UnsupportedOperationException if this primitive is neither a number nor a string.
*/
@Override
public Number getAsNumber() {
if (value instanceof Number) {
return (Number) value;
} else if (value instanceof String) {
return new LazilyParsedNumber((String) value);
}
throw new UnsupportedOperationException("Primitive is neither a number nor a string");
}
/**
* Check whether this primitive contains a String value.
*
* @return true if this primitive contains a String value, false otherwise.
*/
public boolean isString() {
return value instanceof String;
}
// Don't add Javadoc, inherit it from super implementation; no exceptions are thrown here
@Override
public String getAsString() {
if (value instanceof String) {
return (String) value;
} else if (isNumber()) {
return getAsNumber().toString();View on GitHub (pinned to 8b8628c656)
Solutions
- Check isBoolean() / isNumber() on the JsonPrimitive before numeric coercion.
- Normalize the source data so the field is consistently typed.
- Use a TypeAdapter with explicit handling rather than ad-hoc tree coercion.
- Provide a fallback default when the primitive is not numeric.
Example fix
// before
Number n = prim.getAsNumber();
// after
Number n = prim.isNumber() ? prim.getAsNumber()
: prim.isBoolean() ? (prim.getAsBoolean() ? 1 : 0)
: null; Defensive patterns
Strategy: type-guard
Validate before calling
if (!prim.isNumber()) {
// not numeric; bail or coerce
}
Type guard
boolean isNumeric(JsonPrimitive p) {
return p.isNumber();
}
Try / catch
try {
Number n = prim.getAsNumber();
} catch (UnsupportedOperationException ex) {
// boolean or unsupported primitive; supply fallback
n = prim.isBoolean() ? (prim.getAsBoolean() ? 1 : 0) : null;
}
Prevention
- Check isNumber()/isBoolean() before coercion.
- Normalize the source schema for consistent primitive types.
- Use typed deserialization instead of ad-hoc tree access.
When it happens
Trigger: Constructing JsonPrimitive(true) or parsing true/false and calling getAsNumber(); chaining getAsNumber().doubleValue() on a boolean primitive; calling getAsInt()/getAsDouble() on a boolean (those route through getAsNumber()).
Common situations: Schema drift where a boolean field replaces a numeric one; polymorphic JSON where the same key holds different primitive kinds; loose duck-typing over a parsed tree.
Related errors
- String value is empty
- 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/3991664f5b6f2e49.json.
Report an issue: GitHub.