{"id":"f6ba52ca65470062","repo":"google/gson","slug":"expected-an-int-but-was-peekedlong-locations","errorCode":null,"errorMessage":"Expected an int but was \" + peekedLong + locationString()","messagePattern":"Expected an int but was \" \\+ peekedLong \\+ locationString\\(\\)","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/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1318-L1354","documentation":"Thrown by JsonReader.nextInt() as a NumberFormatException when the JSON literal was recognized as a complete integer (PEEKED_LONG) but its value does not fit in a Java int. The check `peekedLong != result` after casting to int detects the overflow at JsonReader.java:1335.","triggerScenarios":"Calling nextInt() on a JSON number larger than Integer.MAX_VALUE or smaller than Integer.MIN_VALUE, e.g. 3000000000 or -3000000000. The value fits in a long, but the narrowing cast to int changes it, so the precision check fails.","commonSituations":"32-bit IDs or counters that have grown beyond Integer range; large counters/timestamps in milliseconds; producer using long IDs while consumer reads nextInt(); porting code from nextLong() to nextInt() without range checks.","solutions":["Use nextLong() instead of nextInt() for values that may exceed Integer range.","Ensure the data source emits values within int range, or switch the field type to long.","Validate range before reading: peek() and consume as nextLong() then check bounds yourself."],"exampleFix":"// before\nint count = reader.nextInt(); // throws for 3000000000\n\n// after\nlong count = reader.nextLong();","handlingStrategy":"validation","validationCode":"// Read as long first, then range-check for int\nlong v = reader.nextLong();\nif (v < Integer.MIN_VALUE || v > Integer.MAX_VALUE) {\n  throw new ArithmeticException(\"Value \" + v + \" out of int range\");\n}\nint i = (int) v;","typeGuard":null,"tryCatchPattern":"try {\n  return reader.nextInt();\n} catch (NumberFormatException e) {\n  // reposition or re-read as long and handle overflow\n  throw e;\n}","preventionTips":["Use nextLong() for any field that might exceed Integer range (IDs, timestamps, counters).","Keep Java field types aligned with the producer's numeric range.","Add schema validation or range checks before narrowing to int.","Beware of millisecond timestamps which commonly exceed Integer.MAX_VALUE."],"tags":["gson","jsonreader","number-parsing","overflow","int-range"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}