{"id":"3991664f5b6f2e49","repo":"google/gson","slug":"primitive-is-neither-a-number-nor-a-string","errorCode":null,"errorMessage":"Primitive is neither a number nor a string","messagePattern":"Primitive is neither a number nor a string","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonPrimitive.java","lineNumber":145,"sourceCode":"  public boolean isNumber() {\n    return value instanceof Number;\n  }\n\n  /**\n   * Convenience method to get this element as a {@link Number}. If this primitive {@linkplain\n   * #isString() is a string}, a lazily parsed {@code Number} is constructed which parses the string\n   * when any of its methods are called (which can lead to a {@link NumberFormatException}).\n   *\n   * @throws UnsupportedOperationException if this primitive is neither a number nor a string.\n   */\n  @Override\n  public Number getAsNumber() {\n    if (value instanceof Number) {\n      return (Number) value;\n    } else if (value instanceof String) {\n      return new LazilyParsedNumber((String) value);\n    }\n    throw new UnsupportedOperationException(\"Primitive is neither a number nor a string\");\n  }\n\n  /**\n   * Check whether this primitive contains a String value.\n   *\n   * @return true if this primitive contains a String value, false otherwise.\n   */\n  public boolean isString() {\n    return value instanceof String;\n  }\n\n  // Don't add Javadoc, inherit it from super implementation; no exceptions are thrown here\n  @Override\n  public String getAsString() {\n    if (value instanceof String) {\n      return (String) value;\n    } else if (isNumber()) {\n      return getAsNumber().toString();","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/JsonPrimitive.java#L127-L163","documentation":"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.","triggerScenarios":"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()).","commonSituations":"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.","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."],"exampleFix":"// before\nNumber n = prim.getAsNumber();\n\n// after\nNumber n = prim.isNumber() ? prim.getAsNumber()\n          : prim.isBoolean() ? (prim.getAsBoolean() ? 1 : 0)\n          : null;","handlingStrategy":"type-guard","validationCode":"if (!prim.isNumber()) {\n  // not numeric; bail or coerce\n}\n","typeGuard":"boolean isNumeric(JsonPrimitive p) {\n  return p.isNumber();\n}\n","tryCatchPattern":"try {\n  Number n = prim.getAsNumber();\n} catch (UnsupportedOperationException ex) {\n  // boolean or unsupported primitive; supply fallback\n  n = prim.isBoolean() ? (prim.getAsBoolean() ? 1 : 0) : null;\n}\n","preventionTips":["Check isNumber()/isBoolean() before coercion.","Normalize the source schema for consistent primitive types.","Use typed deserialization instead of ad-hoc tree access."],"tags":["json-tree","type-coercion","unsupported-operation"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}