google/gson · critical · JsonParseException
Failed parsing JSON source to Json
Error message
Failed parsing JSON source to Json
What it means
JsonStreamParser.next() delegates to Streams.parse and catches StackOverflowError/OutOfMemoryError, rethrowing as JsonParseException. It is the streaming counterpart of error 42: the next value in a concatenated stream is too large or too deeply nested for the JVM.
Source
Thrown at gson/src/main/java/com/google/gson/JsonStreamParser.java:91
/**
* Returns the next available {@link JsonElement} on the reader. Throws a {@link
* NoSuchElementException} if no element is available.
*
* @return the next available {@code JsonElement} on the reader.
* @throws JsonParseException if the incoming stream is malformed JSON.
* @throws NoSuchElementException if no {@code JsonElement} is available.
* @since 1.4
*/
@Override
public JsonElement next() throws JsonParseException {
if (!hasNext()) {
throw new NoSuchElementException();
}
try {
return Streams.parse(parser);
} catch (StackOverflowError | OutOfMemoryError e) {
throw new JsonParseException("Failed parsing JSON source to Json", e);
}
}
/**
* Returns true if a {@link JsonElement} is available on the input for consumption
*
* @return true if a {@link JsonElement} is available on the input, false otherwise
* @throws JsonParseException if the incoming stream is malformed JSON.
* @since 1.4
*/
@Override
public boolean hasNext() {
synchronized (lock) {
try {
return parser.peek() != JsonToken.END_DOCUMENT;
} catch (MalformedJsonException e) {
throw new JsonSyntaxException(e);
} catch (IOException e) {View on GitHub (pinned to 8b8628c656)
Solutions
- Increase -Xss/-Xmx for legitimate workloads.
- Enforce per-record size and depth caps before parsing.
- Use a bounded JsonReader pass and skip/abort oversized records.
- Switch from tree-based Streams.parse to direct token streaming for memory control.
Example fix
// before
while (parser.hasNext()) {
JsonElement e = parser.next();
}
// after (bounded reader per record)
try (JsonReader r = new JsonReader(reader)) {
r.setStrictness(Strictness.STRICT);
// walk tokens, enforce MAX_DEPTH and MAX_BYTES
} Defensive patterns
Strategy: validation
Validate before calling
// enforce per-record size before adding to stream if (recordBytes > MAX_RECORD_BYTES) skip();
Try / catch
try {
JsonElement e = parser.next();
} catch (JsonParseException ex) {
if (ex.getCause() instanceof StackOverflowError
|| ex.getCause() instanceof OutOfMemoryError) {
// log and skip oversized record rather than abort stream
} else throw ex;
}
Prevention
- Bound per-record size and depth.
- Prefer token-level JsonReader streaming for untrusted input.
- Tune -Xss/-Xmx for record peak sizes.
When it happens
Trigger: Iterating a JsonStreamParser over a stream where one element is pathologically nested or huge; processing NDJSON records where a single record overflows memory.
Common situations: Log/event ingestion pipelines; NDJSON consumers; adversarial input in a streaming endpoint.
Related errors
- Failed parsing JSON source: {reader} to Json
- No time zone indicator
- Invalid time zone indicator {timezoneIndicator}
- Failed to parse date [{input}]: {fail.getMessage()}
- {value}
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/6a67d06d653c2709.json.
Report an issue: GitHub.