{"record":{"id":"580e3850fefd529d","repo":"google/gson","slug":"expected-an-int-but-was","errorCode":null,"errorMessage":"Expected an int but was {}{}","messagePattern":"Expected an int but was (.+?)(.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonReader.java","lineNumber":1336,"sourceCode":"   * Returns the {@link JsonToken#NUMBER int} value of the next token, consuming it. If the next\n   * token is a string, this method will attempt to parse it as an int. If the next token's numeric\n   * value cannot be exactly represented by a Java {@code int}, this method throws.\n   *\n   * @throws IllegalStateException if the next token is neither a number nor a string.\n   * @throws NumberFormatException if the next literal value cannot be parsed as a number, or\n   *     exactly represented as an int.\n   */\n  public int nextInt() throws IOException {\n    int p = peeked;\n    if (p == PEEKED_NONE) {\n      p = doPeek();\n    }\n\n    int result;\n    if (p == PEEKED_LONG) {\n      result = (int) peekedLong;\n      if (peekedLong != result) { // Make sure no precision was lost casting to 'int'.\n        throw new NumberFormatException(\"Expected an int but was \" + peekedLong + locationString());\n      }\n      peeked = PEEKED_NONE;\n      pathIndices[stackSize - 1]++;\n      return result;\n    }\n\n    if (p == PEEKED_NUMBER) {\n      peekedString = new String(buffer, pos, peekedNumberLength);\n      pos += peekedNumberLength;\n    } else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {\n      if (p == PEEKED_UNQUOTED) {\n        peekedString = nextUnquotedValue();\n      } else {\n        peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\\'' : '\"');\n      }\n      validateAscii(peekedString);\n      try {\n        result = Integer.parseInt(peekedString);","sourceCodeStart":1318,"sourceCodeEnd":1354,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1318-L1354","documentation":"Thrown by JsonReader.nextInt() (JsonReader.java:1334-1337) when the literal was recognized as a PEEKED_LONG (an integer literal that fit in a long) but does not fit in an int: (int) peekedLong != peekedLong. It is a NumberFormatException indicating an overflow when narrowing a valid long to int. The reader refuses to silently truncate.","triggerScenarios":"Calling nextInt() on a JSON number larger than Integer.MAX_VALUE (2147483647) or smaller than Integer.MIN_VALUE, e.g. 9007199254740993 or a 64-bit id field read into an int.","commonSituations":"Reading a field that grew beyond int range over time (e.g. snowflake ids, timestamps in millis); schema mismatch where the producer emits 64-bit ids but the consumer calls nextInt; assuming a count fits in int.","solutions":["Use nextLong() for fields that may exceed Integer.MAX_VALUE.","Validate magnitude before reading, or guard with try/catch NumberFormatException and fall back to nextLong.","Update the consuming field type from int to long.","Check the JSON schema/producer for the field's numeric range."],"exampleFix":"// before\nint id = reader.nextInt(); // fails on 9007199254740993\n\n// after\nlong id = reader.nextLong();","handlingStrategy":"validation","validationCode":"// If the value may exceed Integer.MAX_VALUE, read as long.\nlong v = reader.nextLong();\nint narrowed = (int) v; // explicit, only if you accept truncation","typeGuard":"static boolean fitsInInt(long v) {\n  return v >= Integer.MIN_VALUE && v <= Integer.MAX_VALUE;\n}","tryCatchPattern":"try {\n  return reader.nextInt();\n} catch (NumberFormatException e) {\n  return Math.toIntExact(reader.nextLong());\n}","preventionTips":["Use nextLong for id/timestamp fields that may grow past int range.","Document the numeric range of each field at the schema level.","Add tests with boundary values (Integer.MAX_VALUE, MAX_VALUE+1)."],"tags":["java","gson","jsonreader","numeric","overflow"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}