{"record":{"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/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonPrimitive.java#L127-L163","documentation":"Thrown by JsonPrimitive.getAsNumber() (JsonPrimitive.java:145) as UnsupportedOperationException when the wrapped value is neither a Number nor a String. The only remaining value type a JsonPrimitive can hold is a Boolean; getAsNumber can lazily convert a String but cannot convert a Boolean, so it throws. Note: the other numeric accessors (getAsInt/getAsDouble) route through getAsString and would instead throw NumberFormatException on a boolean — this exact message is unique to getAsNumber().","triggerScenarios":"new JsonPrimitive(true).getAsNumber(); or generic code that calls getAsNumber() on every JsonPrimitive without checking isNumber()/isBoolean(). Also a JSON boolean field read through getAsNumber().","commonSituations":"Generic/genericized tree-walk code that assumes all primitives are numeric; schema mismatch where a boolean field is read as a number; mixing boolean and numeric values in the same field.","solutions":["Guard with primitive.isNumber() (or isString()) before calling getAsNumber()","Check isBoolean() first and read it with getAsBoolean() instead","Fix the schema expectation — booleans are not numbers","Use a typed POJO so Gson applies the correct field type"],"exampleFix":"// before\nNumber n = primitive.getAsNumber(); // throws if primitive is Boolean\n// after\nNumber n;\nif (primitive.isNumber()) n = primitive.getAsNumber();\nelse if (primitive.isString()) n = primitive.getAsNumber(); // lazy parse\nelse throw new IllegalArgumentException(\"not numeric: \" + primitive);","handlingStrategy":"type-guard","validationCode":"JsonPrimitive p = ...;\nboolean numeric = p.isNumber() || p.isString(); // getAsNumber() only safe in these cases","typeGuard":"static Number asNumberIfNumeric(JsonPrimitive p) {\n    if (p.isNumber() || p.isString()) return p.getAsNumber();\n    return null; // boolean primitive -> not numeric\n}","tryCatchPattern":"try {\n    Number n = primitive.getAsNumber();\n} catch (UnsupportedOperationException ex) {\n    // \"Primitive is neither a number nor a string\" -> value is Boolean\n}","preventionTips":["Check isNumber()/isString() before getAsNumber()","Check isBoolean() and use getAsBoolean() for boolean primitives","Use a typed POJO so the field type drives conversion"],"tags":["gson","json","type-cast","unsupported-operation"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}