JsonWriter. Call methods on the writer as you
walk the structure's contents, nesting arrays and objects as necessary:
beginArray(). Write each of the
array's elements with the appropriate value methods or by nesting other arrays and
objects. Finally close the array using endArray().
beginObject(). Write each of the
object's properties by alternating calls to name with the property's value. Write
property values with the appropriate value method or by nesting other objects or
arrays. Finally close the object using endObject().
setFormattingStyle(FormattingStyle), the default is FormattingStyle#COMPACT
setHtmlSafe(boolean), by default HTML characters are not escaped in the JSON
output
setStrictness(Strictness), the default is Strictness#LEGACY_STRICT
setSerializeNulls(boolean), by default null is serialized
JsonWriter instances used internally by the com.google.gson.Gson class differs, and can be adjusted with the various com.google.gson.GsonBuilder methods.
[
{
"id": 912345678901,
"text": "How do I stream JSON in Java?",
"geo": null,
"user": {
"name": "json_newb",
"followers_count": 41
}
},
{
"id": 912345678902,
"text": "@json_newb just use JsonWriter!",
"geo": [50.454722, -104.606667],
"user": {
"name": "jesse",
"followers_count": 2
}
}
]
This code encodes the above structure:
public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndent(" ");
writeMessagesArray(writer, messages);
writer.close();
}
public void writeMessagesArray(JsonWriter writer, List<Message> messages) throws IOException {
writer.beginArray();
for (Message message : messages) {
writeMessage(writer, message);
}
writer.endArray();
}
public void writeMessage(JsonWriter writer, Message message) throws IOException {
writer.beginObject();
writer.name("id").value(message.getId());
writer.name("text").value(message.getText());
if (message.getGeo() != null) {
writer.name("geo");
writeDoublesArray(writer, message.getGeo());
} else {
writer.name("geo").nullValue();
}
writer.name("user");
writeUser(writer, message.getUser());
writer.endObject();
}
public void writeUser(JsonWriter writer, User user) throws IOException {
writer.beginObject();
writer.name("name").value(user.getName());
writer.name("followers_count").value(user.getFollowersCount());
writer.endObject();
}
public void writeDoublesArray(JsonWriter writer, List<Double> doubles) throws IOException {
writer.beginArray();
for (Double value : doubles) {
writer.value(value);
}
writer.endArray();
}
Each JsonWriter may be used to write a single JSON stream. Instances of this class are
not thread safe. Calls that would result in a malformed JSON string will fail with an IllegalStateException.
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private String | |
| private String | |
| private FormattingStyle | |
| private static final String[] | |
| private boolean | |
| private final Writer | out
The JSON output destination |
| private static final String[] | |
| private boolean | |
| private int[] | |
| private int | |
| private Strictness | |
| private boolean | |
| private static final Pattern |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private static boolean | alwaysCreatesValidJsonNumber(Class<? extends Number> c)
Returns whether the |
| private void | |
| private void | beforeValue()
Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. |
| public JsonWriter | |
| public JsonWriter | |
| public void | close()
Implements java. Flushes and closes this writer and the underlying |
| private JsonWriter | closeScope(int empty, int nonempty, char closeBracket)
Closes the current scope by appending any necessary whitespace and the given bracket. |
| public JsonWriter | |
| public JsonWriter | |
| public void | flush()
Implements java. Ensures all buffered data is written to the underlying |
| public final FormattingStyle | Returns: theFormattingStyle that will be used.Returns the pretty printing style used by this writer. |
| public final boolean | |
| public final Strictness | |
| public final boolean | isHtmlSafe()
Returns true if this writer writes JSON that's safe for inclusion in HTML and XML documents. |
| public boolean | |
| public JsonWriter | |
| public JsonWriter | |
| private void | |
| public JsonWriter | |
| private JsonWriter | openScope(int empty, char openBracket)
Enters a new scope by appending any necessary whitespace and the given bracket. |
| private int | |
| private void | |
| private void | |
| public final void | setFormattingStyle(FormattingStyle
the formatting style to use, must not be formattingStyle)null.Sets the formatting style to be used in the encoded document. |
| public final void | setHtmlSafe(boolean htmlSafe)
Configures this writer to emit JSON that's safe for direct inclusion in HTML and XML documents. |
| public final void | |
| public final void | setLenient(boolean
whether this writer should be lenient. If true, the strictness is set to lenient)Strictness#LENIENT. If false, the strictness is set to Strictness#LEGACY_STRICT.
Deprecated
Please use
setStrictness(Strictness) instead.
Sets the strictness of this writer. |
| public final void | setSerializeNulls(boolean serializeNulls)
Sets whether object members are serialized when their value is null. |
| public final void | setStrictness(Strictness
the new strictness of this writer. May not be strictness)null.Configures how strict this writer is with regard to the syntax rules specified in RFC 8259. |
| private void | |
| public JsonWriter | |
| public JsonWriter | |
| public JsonWriter | |
| public JsonWriter | |
| public JsonWriter | |
| public JsonWriter | |
| public JsonWriter | |
| private void |
| deferredName | back to summary |
|---|---|
| private String deferredName | |
| formattedColon | back to summary |
|---|---|
| private String formattedColon | |
| formattedComma | back to summary |
|---|---|
| private String formattedComma | |
| formattingStyle | back to summary |
|---|---|
| private FormattingStyle formattingStyle | |
| HTML_SAFE_REPLACEMENT_CHARS | back to summary |
|---|---|
| private static final String[] HTML_SAFE_REPLACEMENT_CHARS | |
| htmlSafe | back to summary |
|---|---|
| private boolean htmlSafe | |
| out | back to summary |
|---|---|
| private final Writer out The JSON output destination | |
| REPLACEMENT_CHARS | back to summary |
|---|---|
| private static final String[] REPLACEMENT_CHARS | |
| serializeNulls | back to summary |
|---|---|
| private boolean serializeNulls | |
| stack | back to summary |
|---|---|
| private int[] stack | |
| stackSize | back to summary |
|---|---|
| private int stackSize | |
| strictness | back to summary |
|---|---|
| private Strictness strictness | |
| usesEmptyNewlineAndIndent | back to summary |
|---|---|
| private boolean usesEmptyNewlineAndIndent | |
| VALID_JSON_NUMBER_PATTERN | back to summary |
|---|---|
| private static final Pattern VALID_JSON_NUMBER_PATTERN | |
| JsonWriter | back to summary |
|---|---|
| public JsonWriter(Writer out) Creates a new instance that writes a JSON-encoded stream to | |
| alwaysCreatesValidJsonNumber | back to summary |
|---|---|
| private static boolean alwaysCreatesValidJsonNumber(Class<? extends Number> c) Returns whether the | |
| beforeName | back to summary |
|---|---|
| private void beforeName() throws IOException Inserts any necessary separators and whitespace before a name. Also adjusts the stack to expect the name's value. | |
| beforeValue | back to summary |
|---|---|
| private void beforeValue() throws IOException Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element.
| |
| beginArray | back to summary |
|---|---|
| public JsonWriter beginArray() throws IOException Begins encoding a new array. Each call to this method must be paired with a call to
| |
| beginObject | back to summary |
|---|---|
| public JsonWriter beginObject() throws IOException Begins encoding a new object. Each call to this method must be paired with a call to
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Flushes and closes this writer and the underlying
| |
| closeScope | back to summary |
|---|---|
| private JsonWriter closeScope(int empty, int nonempty, char closeBracket) throws IOException Closes the current scope by appending any necessary whitespace and the given bracket.
| |
| endArray | back to summary |
|---|---|
| public JsonWriter endArray() throws IOException Ends encoding the current array.
| |
| endObject | back to summary |
|---|---|
| public JsonWriter endObject() throws IOException Ends encoding the current object.
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Implements java. Ensures all buffered data is written to the underlying
| |
| getFormattingStyle | back to summary |
|---|---|
| public final FormattingStyle getFormattingStyle() Returns the pretty printing style used by this writer.
| |
| getSerializeNulls | back to summary |
|---|---|
| public final boolean getSerializeNulls() Returns true if object members are serialized when their value is null. This has no impact on array elements. The default is true.
| |
| getStrictness | back to summary |
|---|---|
| public final Strictness getStrictness() Returns the strictness of this writer.
| |
| isHtmlSafe | back to summary |
|---|---|
| public final boolean isHtmlSafe() Returns true if this writer writes JSON that's safe for inclusion in HTML and XML documents.
| |
| isLenient | back to summary |
|---|---|
| public boolean isLenient() Returns true if the
| |
| jsonValue | back to summary |
|---|---|
| public JsonWriter jsonValue(String value) throws IOException Writes
| |
| name | back to summary |
|---|---|
| public JsonWriter name(String name) throws IOException Encodes the property name.
| |
| newline | back to summary |
|---|---|
| private void newline() throws IOException | |
| nullValue | back to summary |
|---|---|
| public JsonWriter nullValue() throws IOException Encodes
| |
| openScope | back to summary |
|---|---|
| private JsonWriter openScope(int empty, char openBracket) throws IOException Enters a new scope by appending any necessary whitespace and the given bracket.
| |
| peek | back to summary |
|---|---|
| private int peek() Returns the value on the top of the stack. | |
| push | back to summary |
|---|---|
| private void push(int newTop) | |
| replaceTop | back to summary |
|---|---|
| private void replaceTop(int topOfStack) Replace the value on the top of the stack with the given value. | |
| setFormattingStyle | back to summary |
|---|---|
| public final void setFormattingStyle(FormattingStyle formattingStyle) Sets the formatting style to be used in the encoded document. The formatting style specifies for example the indentation string to be repeated for each level of indentation, or the newline style, to accommodate various OS styles.
| |
| setHtmlSafe | back to summary |
|---|---|
| public final void setHtmlSafe(boolean htmlSafe) Configures this writer to emit JSON that's safe for direct inclusion in HTML and XML documents.
This escapes the HTML characters
| |
| setIndent | back to summary |
|---|---|
| public final void setIndent(String indent) Sets the indentation string to be repeated for each level of indentation in the encoded
document. If This is a convenience method which overwrites any previously set formatting style with either
| |
| setLenient | back to summary |
|---|---|
| public final void setLenient(boolean lenient)
Deprecated Please use Sets the strictness of this writer.
| |
| setSerializeNulls | back to summary |
|---|---|
| public final void setSerializeNulls(boolean serializeNulls) Sets whether object members are serialized when their value is null. This has no impact on array elements. The default is true.
| |
| setStrictness | back to summary |
|---|---|
| public final void setStrictness(Strictness strictness) Configures how strict this writer is with regard to the syntax rules specified in RFC 8259. By default,
| |
| string | back to summary |
|---|---|
| private void string(String value) throws IOException | |
| value | back to summary |
|---|---|
| public JsonWriter value(String value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(boolean value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(Boolean value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(float value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(double value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(long value) throws IOException Encodes
| |
| value | back to summary |
|---|---|
| public JsonWriter value(Number value) throws IOException Encodes
| |
| writeDeferredName | back to summary |
|---|---|
| private void writeDeferredName() throws IOException | |