{"record":{"id":"1b059d4232aeda78","repo":"google/gson","slug":"json-document-was-not-fully-consumed","errorCode":null,"errorMessage":"JSON document was not fully consumed.","messagePattern":"JSON document was not fully consumed\\.","errorType":"exception","errorClass":"JsonSyntaxException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/Gson.java","lineNumber":1222,"sourceCode":"   *\n   * @return an object of type T from the JSON. Returns {@code null} if {@code json} is {@code null}\n   *     or if {@code json} is empty.\n   * @throws JsonSyntaxException if json is not a valid representation for an object of type typeOfT\n   * @see #fromJson(Reader, TypeToken)\n   * @see #fromJson(JsonElement, Class)\n   * @since 2.10\n   */\n  public <T> T fromJson(JsonElement json, TypeToken<T> typeOfT) throws JsonSyntaxException {\n    if (json == null) {\n      return null;\n    }\n    return fromJson(new JsonTreeReader(json), typeOfT);\n  }\n\n  private static void assertFullConsumption(Object obj, JsonReader reader) {\n    try {\n      if (obj != null && reader.peek() != JsonToken.END_DOCUMENT) {\n        throw new JsonSyntaxException(\"JSON document was not fully consumed.\");\n      }\n    } catch (MalformedJsonException e) {\n      throw new JsonSyntaxException(e);\n    } catch (IOException e) {\n      throw new JsonIOException(e);\n    }\n  }\n\n  /**\n   * Proxy type adapter for cyclic type graphs.\n   *\n   * <p><b>Important:</b> Setting the delegate adapter is not thread-safe; instances of {@code\n   * FutureTypeAdapter} must only be published to other threads after the delegate has been set.\n   *\n   * @see Gson#threadLocalAdapterResults\n   */\n  static class FutureTypeAdapter<T> extends SerializationDelegatingTypeAdapter<T> {\n    private TypeAdapter<T> delegate = null;","sourceCodeStart":1204,"sourceCodeEnd":1240,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/Gson.java#L1204-L1240","documentation":"Thrown by Gson.assertFullConsumption when, after successfully reading a value, the reader has not reached END_DOCUMENT - meaning there is trailing non-whitespace content after the top-level JSON value. It is raised as a JsonSyntaxException because a valid JSON document must consist of exactly one top-level value. This catches concatenated/malformed input early.","triggerScenarios":"Parsing a string that contains two JSON objects back-to-back (e.g. \"{}{}\"); trailing garbage after the JSON value (a stray comma, semicolon, or text); reading from a stream that appended log lines; lenient concatenation of multiple documents into one input.","commonSituations":"NDJSON/multi-document content accidentally fed to a single-document parser; server response with trailing debug text; buffered reader reused with leftover content; copy-paste artifacts after the closing brace.","solutions":["Feed only a single JSON document to fromJson; if you have multiple, parse them individually (e.g. line by line for NDJSON).","Trim and inspect the input string for trailing non-JSON characters before parsing.","Use a JsonReader in a loop with hasNext-style handling when multiple top-level values are expected.","If lenient trailing content is intentional and acceptable, strip it before calling fromJson."],"exampleFix":"// before\ngson.fromJson(\"{}{}\", Map.class); // trailing object -> throws\n\n// after\nString[] docs = input.split(\"\\n\");\nfor (String d : docs) gson.fromJson(d, Map.class);","handlingStrategy":"validation","validationCode":"// Ensure a single top-level JSON value before parsing\nString trimmed = json.strip();\nJsonReader r = new JsonReader(new StringReader(trimmed));\nr.setStrictness(Strictness.STRICT);\n// parse once; assertFullConsumption-style check\nObject v = gson.getAdapter(Object.class).read(r);\nif (r.peek() != JsonToken.END_DOCUMENT) {\n  throw new IllegalStateException(\"Trailing content after JSON document\");\n}","typeGuard":null,"tryCatchPattern":"try { gson.fromJson(json, type); }\ncatch (JsonSyntaxException e) {\n  if (e.getMessage().contains(\"not fully consumed\")) {\n    // split NDJSON or strip trailing content, then retry\n  } else throw e;\n}","preventionTips":["Do not feed NDJSON/multi-doc input to single-document fromJson.","Trim and inspect input for stray trailing characters.","Use a JsonReader loop when multiple top-level values are expected."],"tags":["gson-core","deserialization","malformed-json","strict"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}