google/gson · error · EOFException
End of input" + locationString()
Error message
End of input" + locationString()
What it means
Thrown by JsonReader.nextNonWhitespace (as EOFException) when the input ends unexpectedly while a non-whitespace character is required (throwOnEof=true). This means the JSON document is truncated mid-token, e.g. an unclosed object/array or an incomplete literal.
Source
Thrown at gson/src/main/java/com/google/gson/stream/JsonReader.java:1629
}
} else if (c == '#') {
pos = p;
/*
* Skip a # hash end-of-line comment. The JSON RFC doesn't
* specify this behaviour, but it's required to parse
* existing documents. See http://b/2571423.
*/
checkLenient();
skipToEndOfLine();
p = pos;
l = limit;
} else {
pos = p;
return c;
}
}
if (throwOnEof) {
throw new EOFException("End of input" + locationString());
} else {
return -1;
}
}
private void checkLenient() throws MalformedJsonException {
if (strictness != Strictness.LENIENT) {
throw syntaxError(
"Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON");
}
}
/**
* Advances the position until after the next newline character. If the line is terminated by
* "\r\n", the '\n' must be consumed as whitespace by the caller.
*/
private void skipToEndOfLine() throws IOException {
while (pos < limit || fillBuffer(1)) {View on GitHub (pinned to 8b8628c656)
Solutions
- Ensure the upstream source delivers the complete JSON document before parsing.
- If streaming over a network, buffer the full payload (or use chunked reassembly) before constructing the JsonReader.
- Handle EOFException specifically and retry/request-resent the payload.
- Validate that the input is non-empty and well-formed at the source.
Example fix
// before
JsonReader reader = new JsonReader(new StringReader("{\"a\":1")); // missing }
reader.beginObject(); reader.nextName(); reader.nextInt();
reader.endObject(); // throws EOFException 'End of input'
// after
String json = "{"a":1}"; // complete document
JsonReader reader = new JsonReader(new StringReader(json)); Defensive patterns
Strategy: try-catch
Validate before calling
// Buffer the full payload before parsing to avoid mid-stream EOF String json = readEntireStream(inputStream); // fully consume before constructing reader JsonReader reader = new JsonReader(new StringReader(json));
Try / catch
try {
reader.beginObject();
} catch (EOFException e) {
// input was truncated; request retransmission or report upstream
throw new IOException("Truncated JSON input", e);
} Prevention
- Fully buffer network/file input before parsing when possible.
- Validate that the response Content-Length matches the received bytes before parsing.
- Handle EOFException separately to trigger retries or re-requests.
- Reject empty bodies explicitly before constructing a JsonReader.
When it happens
Trigger: Parsing a JSON string/stream that ends prematurely, e.g. '{"a":1' (no closing brace), an empty or whitespace-only input read with beginObject(), or a network stream closed before the full payload arrived.
Common situations: Truncated HTTP responses; file reads interrupted; off-by-one buffer issues; empty request bodies; streaming parsers fed partial chunks without reassembly.
Related errors
- Invalid nesting limit: " + limit
- JsonReader is closed
- Expected a long but was " + peekedString + locationString()
- Expected an int but was " + peekedLong + locationString()
- Expected an int but was " + peekedString + locationString()
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/756b2671ca83d787.json.
Report an issue: GitHub.