class JsonTreeWriter extends JsonWriter {
  private static final Writer UNWRITABLE_WRITER =
      new Writer() {
        @Override
        public void write(char[] buffer, int offset, int counter) {
          throw new AssertionError();
        }

        @Override
        public void flush() {
          throw new AssertionError();
        }

        @Override
        public void close() {
          throw new AssertionError();
        }
      };

  /** Added to the top of the stack when this writer is closed to cause following ops to fail. */
  private static final JsonPrimitive SENTINEL_CLOSED = new JsonPrimitive("closed");

  /** The JsonElements and JsonArrays under modification, outermost to innermost. */
  private final List<JsonElement> stack = new ArrayList<>();

  /** The name for the next JSON object value. If non-null, the top of the stack is a JsonObject. */
  private String pendingName;

  /** the JSON element constructed by this writer. */
  private JsonElement product = JsonNull.INSTANCE; // TODO: is this really what we want?;

  public JsonTreeWriter() {
    super(UNWRITABLE_WRITER);
  }

  /** Returns the top level object produced by this writer. */
  public JsonElement get() {
    if (!stack.isEmpty()) {
      throw new IllegalStateException("Expected one JSON element but was " + stack);
    }
    return product;
  }

  private JsonElement peek() {
    return stack.get(stack.size() - 1);
  }

  private void put(JsonElement value) {
    if (pendingName != null) {
      if (!value.isJsonNull() || getSerializeNulls()) {
        JsonObject object = (JsonObject) peek();
        object.add(pendingName, value);
      }
      pendingName = null;
    } else if (stack.isEmpty()) {
      product = value;
    } else {
      JsonElement element = peek();
      if (element instanceof JsonArray) {
        ((JsonArray) element).add(value);
      } else {
        throw new IllegalStateException();
      }
    }
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter beginArray() throws IOException {
    JsonArray array = new JsonArray();
    put(array);
    stack.add(array);
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter endArray() throws IOException {
    if (stack.isEmpty() || pendingName != null) {
      throw new IllegalStateException();
    }
    JsonElement element = peek();
    if (element instanceof JsonArray) {
      stack.remove(stack.size() - 1);
      return this;
    }
    throw new IllegalStateException();
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter beginObject() throws IOException {
    JsonObject object = new JsonObject();
    put(object);
    stack.add(object);
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter endObject() throws IOException {<FILL_FUNCTION_BODY>}

  @CanIgnoreReturnValue
  @Override
  public JsonWriter name(String name) throws IOException {
    Objects.requireNonNull(name, "name == null");
    if (stack.isEmpty() || pendingName != null) {
      throw new IllegalStateException("Did not expect a name");
    }
    JsonElement element = peek();
    if (element instanceof JsonObject) {
      pendingName = name;
      return this;
    }
    throw new IllegalStateException("Please begin an object before writing a name.");
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(String value) throws IOException {
    if (value == null) {
      return nullValue();
    }
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(boolean value) throws IOException {
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(Boolean value) throws IOException {
    if (value == null) {
      return nullValue();
    }
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(float value) throws IOException {
    if (!isLenient() && (Float.isNaN(value) || Float.isInfinite(value))) {
      throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
    }
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(double value) throws IOException {
    if (!isLenient() && (Double.isNaN(value) || Double.isInfinite(value))) {
      throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
    }
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(long value) throws IOException {
    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter value(Number value) throws IOException {
    if (value == null) {
      return nullValue();
    }

    if (!isLenient()) {
      double d = value.doubleValue();
      if (Double.isNaN(d) || Double.isInfinite(d)) {
        throw new IllegalArgumentException("JSON forbids NaN and infinities: " + value);
      }
    }

    put(new JsonPrimitive(value));
    return this;
  }

  @CanIgnoreReturnValue
  @Override
  public JsonWriter nullValue() throws IOException {
    put(JsonNull.INSTANCE);
    return this;
  }

  @Override
  public JsonWriter jsonValue(String value) throws IOException {
    throw new UnsupportedOperationException();
  }

  @Override
  public void flush() throws IOException {}

  @Override
  public void close() throws IOException {
    if (!stack.isEmpty()) {
      throw new IOException("Incomplete document");
    }
    stack.add(SENTINEL_CLOSED);
  }
}

    if (stack.isEmpty() || pendingName != null) {
      throw new IllegalStateException();
    }
    JsonElement element = peek();
    if (element instanceof JsonObject) {
      stack.remove(stack.size() - 1);
      return this;
    }
    throw new IllegalStateException();
  
<methods>public void <init>(java.io.Writer) ,public com.google.gson.stream.JsonWriter beginArray() throws java.io.IOException,public com.google.gson.stream.JsonWriter beginObject() throws java.io.IOException,public void close() throws java.io.IOException,public com.google.gson.stream.JsonWriter endArray() throws java.io.IOException,public com.google.gson.stream.JsonWriter endObject() throws java.io.IOException,public void flush() throws java.io.IOException,public final com.google.gson.FormattingStyle getFormattingStyle() ,public final boolean getSerializeNulls() ,public final com.google.gson.Strictness getStrictness() ,public final boolean isHtmlSafe() ,public boolean isLenient() ,public com.google.gson.stream.JsonWriter jsonValue(java.lang.String) throws java.io.IOException,public com.google.gson.stream.JsonWriter name(java.lang.String) throws java.io.IOException,public com.google.gson.stream.JsonWriter nullValue() throws java.io.IOException,public final void setFormattingStyle(com.google.gson.FormattingStyle) ,public final void setHtmlSafe(boolean) ,public final void setIndent(java.lang.String) ,public final void setLenient(boolean) ,public final void setSerializeNulls(boolean) ,public final void setStrictness(com.google.gson.Strictness) ,public com.google.gson.stream.JsonWriter value(java.lang.String) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(boolean) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(java.lang.Boolean) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(float) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(double) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(long) throws java.io.IOException,public com.google.gson.stream.JsonWriter value(java.lang.Number) throws java.io.IOException<variables>private static final non-sealed java.lang.String[] HTML_SAFE_REPLACEMENT_CHARS,private static final non-sealed java.lang.String[] REPLACEMENT_CHARS,private static final java.util.regex.Pattern VALID_JSON_NUMBER_PATTERN,private java.lang.String deferredName,private java.lang.String formattedColon,private java.lang.String formattedComma,private com.google.gson.FormattingStyle formattingStyle,private boolean htmlSafe,private final non-sealed java.io.Writer out,private boolean serializeNulls,private int[] stack,private int stackSize,private com.google.gson.Strictness strictness,private boolean usesEmptyNewlineAndIndent