public class JsonWriter
extends java.lang.Object
implements java.io.Closeable, java.io.Flushable
JsonWriter
. Each JSON
document must contain one top-level array or object. 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(java.lang.String)
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(java.lang.String)
with the property's value. Write property values with the
appropriate value(java.lang.String)
method or by nesting other objects or arrays.
Finally close the object using endObject()
.
[
{
"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.setIndentSpaces(4);
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 java.lang.String |
deferredName |
private static java.lang.String[] |
HTML_SAFE_REPLACEMENT_CHARS |
private boolean |
htmlSafe |
private java.lang.String |
indent
A string containing a full set of spaces for a single level of
indentation, or null for no pretty printing.
|
private boolean |
lenient |
private java.io.Writer |
out
The output data, containing at most one top-level array or object.
|
private static java.lang.String[] |
REPLACEMENT_CHARS |
private java.lang.String |
separator
The name/value separator; either ":" or ": ".
|
private boolean |
serializeNulls |
private int[] |
stack |
private int |
stackSize |
Constructor and Description |
---|
JsonWriter(java.io.Writer out)
Creates a new instance that writes a JSON-encoded stream to
out . |
Modifier and Type | Method and Description |
---|---|
private void |
beforeName()
Inserts any necessary separators and whitespace before a name.
|
private void |
beforeValue(boolean root)
Inserts any necessary separators and whitespace before a literal value,
inline array, or inline object.
|
JsonWriter |
beginArray()
Begins encoding a new array.
|
JsonWriter |
beginObject()
Begins encoding a new object.
|
void |
close()
Flushes and closes this writer and the underlying
Writer . |
private JsonWriter |
close(int empty,
int nonempty,
java.lang.String closeBracket)
Closes the current scope by appending any necessary whitespace and the
given bracket.
|
JsonWriter |
endArray()
Ends encoding the current array.
|
JsonWriter |
endObject()
Ends encoding the current object.
|
void |
flush()
Ensures all buffered data is written to the underlying
Writer
and flushes that writer. |
boolean |
getSerializeNulls()
Returns true if object members are serialized when their value is null.
|
boolean |
isHtmlSafe()
Returns true if this writer writes JSON that's safe for inclusion in HTML
and XML documents.
|
boolean |
isLenient()
Returns true if this writer has relaxed syntax rules.
|
JsonWriter |
name(java.lang.String name)
Encodes the property name.
|
private void |
newline() |
JsonWriter |
nullValue()
Encodes
null . |
private JsonWriter |
open(int empty,
java.lang.String openBracket)
Enters a new scope by appending any necessary whitespace and the given
bracket.
|
private int |
peek()
Returns the value on the top of the stack.
|
private void |
push(int newTop) |
private void |
replaceTop(int topOfStack)
Replace the value on the top of the stack with the given value.
|
void |
setHtmlSafe(boolean htmlSafe)
Configure this writer to emit JSON that's safe for direct inclusion in HTML
and XML documents.
|
void |
setIndent(java.lang.String indent)
Sets the indentation string to be repeated for each level of indentation
in the encoded document.
|
void |
setLenient(boolean lenient)
Configure this writer to relax its syntax rules.
|
void |
setSerializeNulls(boolean serializeNulls)
Sets whether object members are serialized when their value is null.
|
private void |
string(java.lang.String value) |
JsonWriter |
value(boolean value)
Encodes
value . |
JsonWriter |
value(double value)
Encodes
value . |
JsonWriter |
value(long value)
Encodes
value . |
JsonWriter |
value(java.lang.Number value)
Encodes
value . |
JsonWriter |
value(java.lang.String value)
Encodes
value . |
private void |
writeDeferredName() |
private static final java.lang.String[] REPLACEMENT_CHARS
private static final java.lang.String[] HTML_SAFE_REPLACEMENT_CHARS
private final java.io.Writer out
private int[] stack
private int stackSize
private java.lang.String indent
private java.lang.String separator
private boolean lenient
private boolean htmlSafe
private java.lang.String deferredName
private boolean serializeNulls
public JsonWriter(java.io.Writer out)
out
.
For best performance, ensure Writer
is buffered; wrapping in
BufferedWriter
if necessary.public final void setIndent(java.lang.String indent)
indent.isEmpty()
the encoded document
will be compact. Otherwise the encoded document will be more
human-readable.indent
- a string containing only whitespace.public final void setLenient(boolean lenient)
NaNs
or infinities
.
public boolean isLenient()
public final void setHtmlSafe(boolean htmlSafe)
<
, >
,
&
and =
before writing them to the stream. Without this
setting, your XML/HTML encoder should replace these characters with the
corresponding escape sequences.public final boolean isHtmlSafe()
public final void setSerializeNulls(boolean serializeNulls)
public final boolean getSerializeNulls()
public JsonWriter beginArray() throws java.io.IOException
endArray()
.java.io.IOException
public JsonWriter endArray() throws java.io.IOException
java.io.IOException
public JsonWriter beginObject() throws java.io.IOException
endObject()
.java.io.IOException
public JsonWriter endObject() throws java.io.IOException
java.io.IOException
private JsonWriter open(int empty, java.lang.String openBracket) throws java.io.IOException
java.io.IOException
private JsonWriter close(int empty, int nonempty, java.lang.String closeBracket) throws java.io.IOException
java.io.IOException
private void push(int newTop)
private int peek()
private void replaceTop(int topOfStack)
public JsonWriter name(java.lang.String name) throws java.io.IOException
name
- the name of the forthcoming value. May not be null.java.io.IOException
private void writeDeferredName() throws java.io.IOException
java.io.IOException
public JsonWriter value(java.lang.String value) throws java.io.IOException
value
.value
- the literal string value, or null to encode a null literal.java.io.IOException
public JsonWriter nullValue() throws java.io.IOException
null
.java.io.IOException
public JsonWriter value(boolean value) throws java.io.IOException
value
.java.io.IOException
public JsonWriter value(double value) throws java.io.IOException
value
.value
- a finite value. May not be NaNs
or
infinities
.java.io.IOException
public JsonWriter value(long value) throws java.io.IOException
value
.java.io.IOException
public JsonWriter value(java.lang.Number value) throws java.io.IOException
value
.value
- a finite value. May not be NaNs
or
infinities
.java.io.IOException
public void flush() throws java.io.IOException
Writer
and flushes that writer.flush
in interface java.io.Flushable
java.io.IOException
public void close() throws java.io.IOException
Writer
.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if the JSON document is incomplete.private void string(java.lang.String value) throws java.io.IOException
java.io.IOException
private void newline() throws java.io.IOException
java.io.IOException
private void beforeName() throws java.io.IOException
java.io.IOException
private void beforeValue(boolean root) throws java.io.IOException
root
- true if the value is a new array or object, the two values
permitted as top-level elements.java.io.IOException