- java.lang.Object
-
- com.google.gson.stream.JsonWriter
-
- com.google.gson.internal.bind.JsonTreeWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public final class JsonTreeWriter extends JsonWriter
This writer creates a JsonElement.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
pendingName
The name for the next JSON object value.private JsonElement
product
the JSON element constructed by this writer.private static JsonPrimitive
SENTINEL_CLOSED
Added to the top of the stack when this writer is closed to cause following ops to fail.private java.util.List<JsonElement>
stack
The JsonElements and JsonArrays under modification, outermost to innermost.private static java.io.Writer
UNWRITABLE_WRITER
-
Constructor Summary
Constructors Constructor Description JsonTreeWriter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonWriter
beginArray()
Begins encoding a new array.JsonWriter
beginObject()
Begins encoding a new object.void
close()
Flushes and closes this writer and the underlyingWriter
.JsonWriter
endArray()
Ends encoding the current array.JsonWriter
endObject()
Ends encoding the current object.void
flush()
Ensures all buffered data is written to the underlyingWriter
and flushes that writer.JsonElement
get()
Returns the top level object produced by this writer.JsonWriter
name(java.lang.String name)
Encodes the property name.JsonWriter
nullValue()
Encodesnull
.private JsonElement
peek()
private void
put(JsonElement value)
JsonWriter
value(boolean value)
Encodesvalue
.JsonWriter
value(double value)
Encodesvalue
.JsonWriter
value(float value)
Encodesvalue
.JsonWriter
value(long value)
Encodesvalue
.JsonWriter
value(java.lang.Boolean value)
Encodesvalue
.JsonWriter
value(java.lang.Number value)
Encodesvalue
.JsonWriter
value(java.lang.String value)
Encodesvalue
.-
Methods inherited from class com.google.gson.stream.JsonWriter
getSerializeNulls, isHtmlSafe, isLenient, jsonValue, setHtmlSafe, setIndent, setLenient, setSerializeNulls
-
-
-
-
Field Detail
-
UNWRITABLE_WRITER
private static final java.io.Writer UNWRITABLE_WRITER
-
SENTINEL_CLOSED
private static final JsonPrimitive SENTINEL_CLOSED
Added to the top of the stack when this writer is closed to cause following ops to fail.
-
stack
private final java.util.List<JsonElement> stack
The JsonElements and JsonArrays under modification, outermost to innermost.
-
pendingName
private java.lang.String pendingName
The name for the next JSON object value. If non-null, the top of the stack is a JsonObject.
-
product
private JsonElement product
the JSON element constructed by this writer.
-
-
Method Detail
-
get
public JsonElement get()
Returns the top level object produced by this writer.
-
peek
private JsonElement peek()
-
put
private void put(JsonElement value)
-
beginArray
public JsonWriter beginArray() throws java.io.IOException
Description copied from class:JsonWriter
Begins encoding a new array. Each call to this method must be paired with a call toJsonWriter.endArray()
.- Overrides:
beginArray
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
endArray
public JsonWriter endArray() throws java.io.IOException
Description copied from class:JsonWriter
Ends encoding the current array.- Overrides:
endArray
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
beginObject
public JsonWriter beginObject() throws java.io.IOException
Description copied from class:JsonWriter
Begins encoding a new object. Each call to this method must be paired with a call toJsonWriter.endObject()
.- Overrides:
beginObject
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
endObject
public JsonWriter endObject() throws java.io.IOException
Description copied from class:JsonWriter
Ends encoding the current object.- Overrides:
endObject
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
name
public JsonWriter name(java.lang.String name) throws java.io.IOException
Description copied from class:JsonWriter
Encodes the property name.- Overrides:
name
in classJsonWriter
- Parameters:
name
- the name of the forthcoming value. May not be null.- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(java.lang.String value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Parameters:
value
- the literal string value, or null to encode a null literal.- Returns:
- this writer.
- Throws:
java.io.IOException
-
nullValue
public JsonWriter nullValue() throws java.io.IOException
Description copied from class:JsonWriter
Encodesnull
.- Overrides:
nullValue
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(boolean value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(java.lang.Boolean value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(float value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Parameters:
value
- a finite value, or iflenient
, alsoNaN
orinfinity
.- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(double value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Parameters:
value
- a finite value, or iflenient
, alsoNaN
orinfinity
.- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(long value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
.- Overrides:
value
in classJsonWriter
- Returns:
- this writer.
- Throws:
java.io.IOException
-
value
public JsonWriter value(java.lang.Number value) throws java.io.IOException
Description copied from class:JsonWriter
Encodesvalue
. The value is written by directly writing theObject.toString()
result to JSON. Implementations must make sure that the result represents a valid JSON number.- Overrides:
value
in classJsonWriter
- Parameters:
value
- a finite value, or iflenient
, alsoNaN
orinfinity
.- Returns:
- this writer.
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
Description copied from class:JsonWriter
Ensures all buffered data is written to the underlyingWriter
and flushes that writer.- Specified by:
flush
in interfacejava.io.Flushable
- Overrides:
flush
in classJsonWriter
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Description copied from class:JsonWriter
Flushes and closes this writer and the underlyingWriter
.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classJsonWriter
- Throws:
java.io.IOException
- if the JSON document is incomplete.
-
-