{"id":"da957932e0f0bb3d","repo":"google/gson","slug":"expecting-number-got-jsontoken-at-path","errorCode":null,"errorMessage":"Expecting number, got: \" + jsonToken + \"; at path \" + in.getPath()","messagePattern":"Expecting number, got: \" \\+ jsonToken \\+ \"; at path \" \\+ in\\.getPath\\(\\)","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/internal/bind/NumberTypeAdapter.java","lineNumber":73,"sourceCode":"    if (toNumberStrategy == ToNumberPolicy.LAZILY_PARSED_NUMBER) {\n      return LAZILY_PARSED_NUMBER_FACTORY;\n    } else {\n      return newFactory(toNumberStrategy);\n    }\n  }\n\n  @Override\n  public Number read(JsonReader in) throws IOException {\n    JsonToken jsonToken = in.peek();\n    switch (jsonToken) {\n      case NULL:\n        in.nextNull();\n        return null;\n      case NUMBER:\n      case STRING:\n        return toNumberStrategy.readNumber(in);\n      default:\n        throw new JsonSyntaxException(\n            \"Expecting number, got: \" + jsonToken + \"; at path \" + in.getPath());\n    }\n  }\n\n  @Override\n  public void write(JsonWriter out, Number value) throws IOException {\n    out.value(value);\n  }\n}\n","sourceCodeStart":55,"sourceCodeEnd":83,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/internal/bind/NumberTypeAdapter.java#L55-L83","documentation":"Thrown by NumberTypeAdapter.read() as a JsonSyntaxException when deserializing into java.lang.Number and the current token is not NULL, NUMBER, or STRING. The adapter accepts a quoted numeric string (STRING) and plain numbers, but booleans, objects, arrays, names, and end tokens cannot be coerced to Number.","triggerScenarios":"Deserializing JSON where a Number-typed field (or Number.class target) receives a non-numeric value: a boolean (true/false), an object {}, an array [], etc. For example, a field declared as Number receiving {\"val\":true} or a JSON value of true at that position.","commonSituations":"Loosely-typed domain models using Number as a catch-all then receiving boolean markers from an API; schema drift where a numeric field becomes an object; wrong target type passed to fromJson (e.g. fromJson(\"true\", Number.class)); producers sending null vs absent vs boolean inconsistently.","solutions":["Correct the source data so the value at that path is a number or numeric string.","Change the target field type to match the actual JSON value (Boolean, Object, String), or use Object to accept any primitive.","Register a custom TypeAdapter<Number> that coerces allowed alternatives (e.g. treat booleans as 1/0) if that semantics is intended.","Configure a more lenient object/number policy (GsonBuilder.setObjectToNumberStrategy) if the issue is at the Object level."],"exampleFix":"// before\n// JSON: true\nclass Data { Number n; }\nData d = gson.fromJson(\"{\\\"n\\\":true}\", Data.class); // throws: Expecting number, got: BOOLEAN\n\n// after: match the type to the data\nclass Data { Boolean n; } // or Object n; or String n;\nData d = gson.fromJson(\"{\\\"n\\\":true}\", Data.class);","handlingStrategy":"validation","validationCode":"// Peek and validate token shape before reading as Number\nJsonToken t = reader.peek();\nif (t != JsonToken.NUMBER && t != JsonToken.STRING && t != JsonToken.NULL) {\n  throw new IllegalStateException(\"Not a number: \" + t);\n}\nNumber n = gson.getAdapter(Number.class).read(reader);","typeGuard":null,"tryCatchPattern":"try {\n  return gson.fromJson(json, Number.class);\n} catch (JsonSyntaxException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Expecting number, got:\")) {\n    // value isn't numeric; fall back to Object or String\n    return gson.fromJson(json, Object.class);\n  }\n  throw e;\n}","preventionTips":["Match the target field type to the actual JSON value type.","Use Object or JsonElement for genuinely polymorphic values.","Validate incoming payloads against a schema before deserialization."],"tags":["json","gson","deserialization","numeric","type-mismatch"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}