{"id":"69823b111c05897e","repo":"google/gson","slug":"expected-an-int-but-was-peekedstring-locatio","errorCode":null,"errorMessage":"Expected an int but was \" + peekedString + locationString()","messagePattern":"Expected an int but was \" \\+ peekedString \\+ locationString\\(\\)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonReader.java","lineNumber":1377,"sourceCode":"        // Fall back to parse as a double below.\n      }\n    } else {\n      throw unexpectedTokenError(\"an int\");\n    }\n\n    peeked = PEEKED_BUFFERED;\n    double asDouble;\n    try {\n      asDouble = Double.parseDouble(peekedString);\n    } catch (NumberFormatException e) {\n      NumberFormatException rethrown =\n          new NumberFormatException(\"Expected an int but was \" + peekedString + locationString());\n      rethrown.initCause(e);\n      throw rethrown;\n    }\n    result = (int) asDouble;\n    if (result != asDouble) { // Make sure no precision was lost casting to 'int'.\n      throw new NumberFormatException(\"Expected an int but was \" + peekedString + locationString());\n    }\n    peekedString = null;\n    peeked = PEEKED_NONE;\n    pathIndices[stackSize - 1]++;\n    return result;\n  }\n\n  /**\n   * Closes this JSON reader and the underlying {@link Reader}.\n   *\n   * <p>Using the JSON reader after it has been closed will throw an {@link IllegalStateException}\n   * in most cases.\n   */\n  @Override\n  public void close() throws IOException {\n    peeked = PEEKED_NONE;\n    stack[0] = JsonScope.CLOSED;\n    stackSize = 1;","sourceCodeStart":1359,"sourceCodeEnd":1395,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1359-L1395","documentation":"Thrown by JsonReader.nextInt() as a NumberFormatException on the fallback path: the value could not be parsed as an int directly (Integer.parseInt failed) and either Double.parseDouble failed (rethrown at line 1371) or the double value loses precision when cast to int (line 1377). The message echoes the offending literal string.","triggerScenarios":"Calling nextInt() on a quoted string like \"abc\", a fractional number \"1.5\", or a value that parses as a double but does not equal its int truncation. Also when the token is a numeric string that is not a valid number at all.","commonSituations":"Numeric fields that occasionally contain non-numeric or floating-point content; lenient JSON where numbers arrive as strings; schema mismatches; debug/test data with placeholder strings.","solutions":["Use nextString() and parse with Integer.parseInt / NumberFormat yourself, handling failures explicitly.","Use nextDouble() if the value can be fractional.","Sanitize the data source to emit integer literals only.","peek() first; if the token is a STRING, decide whether to coerce or skip."],"exampleFix":"// before\nint n = reader.nextInt(); // throws for \"1.5\" or \"abc\"\n\n// after\nString raw = reader.nextString();\nint n = Integer.parseInt(raw); // or NumberFormat.getInstance().parse(raw).intValue()","handlingStrategy":"validation","validationCode":"// If the field may be a string or non-integer, read as string and parse defensively\nString raw = reader.nextString();\ntry {\n  return Integer.parseInt(raw);\n} catch (NumberFormatException e) {\n  double d = Double.parseDouble(raw);\n  return (int) d; // or handle as needed\n}","typeGuard":null,"tryCatchPattern":"try {\n  return reader.nextInt();\n} catch (NumberFormatException e) {\n  // log and apply a default, or re-read from the underlying source\n  return defaultValue;\n}","preventionTips":["When producers may emit numbers as strings, read with nextString() and parse explicitly.","Use nextDouble() if fractional values are valid for the field.","Sanitize or reject non-numeric content in numeric fields at the source.","peek() first and branch on JsonToken.STRING vs NUMBER."],"tags":["gson","jsonreader","number-parsing","type-mismatch"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}