{"record":{"id":"7dc22e6f035ca41b","repo":"google/gson","slug":"jsonwriter-is-closed","errorCode":null,"errorMessage":"JsonWriter is closed.","messagePattern":"JsonWriter is closed\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonWriter.java","lineNumber":482,"sourceCode":"    stackSize--;\n    if (context == nonempty) {\n      newline();\n    }\n    out.write(closeBracket);\n    return this;\n  }\n\n  private void push(int newTop) {\n    if (stackSize == stack.length) {\n      stack = Arrays.copyOf(stack, stackSize * 2);\n    }\n    stack[stackSize++] = newTop;\n  }\n\n  /** Returns the value on the top of the stack. */\n  private int peek() {\n    if (stackSize == 0) {\n      throw new IllegalStateException(\"JsonWriter is closed.\");\n    }\n    return stack[stackSize - 1];\n  }\n\n  /** Replace the value on the top of the stack with the given value. */\n  private void replaceTop(int topOfStack) {\n    stack[stackSize - 1] = topOfStack;\n  }\n\n  /**\n   * Encodes the property name.\n   *\n   * @param name the name of the forthcoming value. May not be {@code null}.\n   * @return this writer.\n   */\n  @CanIgnoreReturnValue\n  public JsonWriter name(String name) throws IOException {\n    Objects.requireNonNull(name, \"name == null\");","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/google/gson/blob/310ac341f2f92a454b229bf21f70d2d18b2b6db7/gson/src/main/java/com/google/gson/stream/JsonWriter.java#L464-L500","documentation":"Thrown by JsonWriter.peek() (JsonWriter.java:480-483) when stackSize == 0, which happens after close() sets stackSize to 0 (JsonWriter.java:719-727). Because every write operation calls peek() to inspect the current scope, any write or name() call after close triggers this IllegalStateException. It prevents writes to a writer whose underlying Writer has been released.","triggerScenarios":"Calling any write method, name(), begin*/end*, or value() after writer.close(); using a JsonWriter in a try-with-resources and writing in a finally or callback that runs post-close; reusing a cached writer instance that was closed.","commonSituations":"try-with-resources that closes the writer before a deferred flush/write; pooling/caching JsonWriter instances without resetting state; helper methods that close the writer and then the caller tries to append more.","solutions":["Finish all writes before close(); do not write after.","Do not reuse a JsonWriter across streams; create a new one per output.","Move close() to the outermost scope so nothing writes after it.","Buffer the full document in memory and write it once if you need post-close access."],"exampleFix":"// before\ntry (JsonWriter w = new JsonWriter(out)) {\n  w.beginObject().name(\"a\").value(1).endObject();\n}\nw.flush(); // throws: writer is closed\n\n// after\ntry (JsonWriter w = new JsonWriter(out)) {\n  w.beginObject().name(\"a\").value(1).endObject();\n  w.flush();\n}","handlingStrategy":"validation","validationCode":"try (JsonWriter w = new JsonWriter(out)) {\n  w.beginObject().name(\"a\").value(1).endObject();\n  w.flush();\n}\n// nothing writes after this block","typeGuard":"// JsonWriter has no isOpen(); track in a wrapper.\nclass SafeJsonWriter {\n  private final JsonWriter w;\n  private boolean closed;\n  boolean isOpen() { return !closed; }\n  public void close() throws IOException { w.close(); closed = true; }\n}","tryCatchPattern":"try {\n  w.value(1);\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"closed\")) return; // or reopen a new writer\n  throw e;\n}","preventionTips":["Close exactly once, at the outermost scope, after all writes.","Do not pool/cache JsonWriter instances.","Ensure framework shutdown hooks do not write after close."],"tags":["java","gson","jsonwriter","lifecycle","resource-management"],"backgroundTag":null,"analyzedSha":"310ac341f2f92a454b229bf21f70d2d18b2b6db7","analyzedAt":"2026-08-10T02:58:47.455Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}