{"record":{"id":"2706ed57eb8d436a","repo":"google/gson","slug":"did-not-consume-the-entire-document","errorCode":null,"errorMessage":"Did not consume the entire document.","messagePattern":"Did not consume the entire document\\.","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/JsonParser.java","lineNumber":112,"sourceCode":"\n  /**\n   * Parses the complete JSON string provided by the reader into a parse tree. An exception is\n   * thrown if the JSON string has multiple top-level JSON elements, or if there is trailing data.\n   *\n   * <p>The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode}.\n   *\n   * @param reader JSON text\n   * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON\n   * @throws JsonParseException if there is an IOException or if the specified text is not valid\n   *     JSON\n   * @since 2.8.6\n   */\n  public static JsonElement parseReader(Reader reader) throws JsonIOException, JsonSyntaxException {\n    try {\n      JsonReader jsonReader = new JsonReader(reader);\n      JsonElement element = parseReader(jsonReader);\n      if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {\n        throw new JsonSyntaxException(\"Did not consume the entire document.\");\n      }\n      return element;\n    } catch (MalformedJsonException | NumberFormatException e) {\n      throw new JsonSyntaxException(e);\n    } catch (IOException e) {\n      throw new JsonIOException(e);\n    }\n  }\n\n  /**\n   * Returns the next value from the JSON stream as a parse tree. Unlike the other {@code parse}\n   * methods, no exception is thrown if the JSON data has multiple top-level JSON elements, or if\n   * there is trailing data.\n   *\n   * <p>If the {@linkplain JsonReader#getStrictness() strictness of the reader} is {@link\n   * Strictness#STRICT}, that strictness will be used for parsing. Otherwise the strictness will be\n   * temporarily changed to {@link Strictness#LENIENT} and will be restored once this method\n   * returns.","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/JsonParser.java#L94-L130","documentation":"Thrown as JsonSyntaxException by JsonParser.parseReader(Reader) (JsonParser.java:112) after successfully parsing one top-level JSON value if the reader still has content — i.e. element is not JsonNull and jsonReader.peek() != JsonToken.END_DOCUMENT. It means the input contained more than a single top-level JSON document or had trailing tokens. (Trailing whitespace is fine; trailing tokens are not.)","triggerScenarios":"Input with multiple concatenated top-level values or trailing characters: '{ }{ }', '1 2 3', '{}' followed by stray text, two JSON objects joined, or a value followed by a stray comma. Also feeding newline-delimited JSON (NDJSON) to parseString, which expects exactly one document.","commonSituations":"NDJSON/log streams read as a single document; a buffer that accidentally concatenated two responses; stray BOM or paste artifacts; a trailing comma after the top-level value.","solutions":["For NDJSON/streaming, parse one document per line or drive a JsonReader loop with parseReader(JsonReader) which does NOT enforce single-document consumption","Trim and inspect the raw input for trailing tokens before parsing","Verify the source emits exactly one top-level JSON value","If multiple documents are intentional, switch to the JsonReader overload and loop until END_DOCUMENT"],"exampleFix":"// before\nJsonElement e = JsonParser.parseString(ndjsonLine); // fails if two docs on one line\n// after (per-line NDJSON)\nfor (String line : raw.split(\"\\n\")) {\n    if (!line.isBlank()) JsonParser.parseString(line);\n}\n// after (streaming, tolerates trailing docs)\ntry (JsonReader r = new JsonReader(new StringReader(raw))) {\n    while (r.peek() != JsonToken.END_DOCUMENT) {\n        JsonElement e = JsonParser.parseReader(r);\n    }\n}","handlingStrategy":"validation","validationCode":"// ensure exactly one top-level JSON value before parsing\nString trimmed = raw.strip();\n// simplest: parse and then re-peek via the JsonReader overload which does NOT enforce single-doc:\ntry (JsonReader r = new JsonReader(new StringReader(trimmed))) {\n    JsonElement e = JsonParser.parseReader(r);\n    // r.peek() == END_DOCUMENT here means no trailing tokens\n}","typeGuard":null,"tryCatchPattern":"try {\n    JsonElement e = JsonParser.parseString(raw);\n} catch (JsonSyntaxException ex) {\n    if (ex.getMessage().contains(\"Did not consume the entire document\")) {\n        // trailing data: switch to per-line or JsonReader streaming parse\n    }\n}","preventionTips":["For NDJSON, parse one document per line instead of the whole blob","Use parseReader(JsonReader) when trailing/multiple documents are valid","Inspect raw input for stray tokens after the top-level value"],"tags":["gson","json","parsing","syntax"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}