public class DefaultJsonWriter extends Object implements JsonWriter
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) {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndentSpaces(4);
writeMessagesArray(writer, messages);
writer.close();
}
<p>
public void writeMessagesArray(JsonWriter writer, List<Message> messages) {
writer.beginArray();
for (Message message : messages) {
writeMessage(writer, message);
}
writer.endArray();
}
<p>
public void writeMessage(JsonWriter writer, Message message) {
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();
}
<p>
public void writeUser(JsonWriter writer, User user) {
writer.beginObject();
writer.name("name").value(user.getName());
writer.name("followers_count").value(user.getFollowersCount());
writer.endObject();
}
<p>
public void writeDoublesArray(JsonWriter writer, List<Double> doubles) {
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.
| Constructor and Description |
|---|
DefaultJsonWriter(StringBuilder out)
Creates a new instance that writes a JSON-encoded stream to
out. |
| Modifier and Type | Method and Description |
|---|---|
DefaultJsonWriter |
beginArray()
Begins encoding a new array.
|
DefaultJsonWriter |
beginObject()
Begins encoding a new object.
|
DefaultJsonWriter |
cancelName()
cancelName
|
void |
close()
Flushes and closes this writer and the underlying
StringBuilder. |
static String |
encodeString(String value)
encodeString
|
static void |
encodeString(String value,
StringBuilder out)
encodeString.
|
DefaultJsonWriter |
endArray()
Ends encoding the current array.
|
DefaultJsonWriter |
endObject()
Ends encoding the current object.
|
void |
flush()
Ensures all buffered data is written to the underlying
StringBuilder
and flushes that writer. |
String |
getOutput()
getOutput
|
boolean |
getSerializeNulls()
getSerializeNulls
|
boolean |
isLenient()
Returns true if this writer has relaxed syntax rules.
|
DefaultJsonWriter |
name(String name)
Encodes the property name.
|
DefaultJsonWriter |
nullValue()
Encodes
null. |
DefaultJsonWriter |
rawValue(Object value)
Encodes
value.toString() as is. |
void |
setIndent(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.
|
DefaultJsonWriter |
unescapeName(String name)
Encodes the property name without escaping it.
|
DefaultJsonWriter |
unescapeValue(String value)
Encodes
value without escaping it. |
DefaultJsonWriter |
value(boolean value)
Encodes
value. |
DefaultJsonWriter |
value(double value)
Encodes
value. |
DefaultJsonWriter |
value(long value)
Encodes
value. |
DefaultJsonWriter |
value(Number value)
Encodes
value. |
DefaultJsonWriter |
value(String value)
Encodes
value. |
public DefaultJsonWriter(StringBuilder out)
out.out - a StringBuilder object.public final void setIndent(String indent)
indent.isEmpty() the encoded document
will be compact. Otherwise the encoded document will be more
human-readable.setIndent in interface JsonWriterindent - a string containing only whitespace.public final void setLenient(boolean lenient)
NaNs or infinities.
setLenient in interface JsonWriterlenient - a boolean.public boolean isLenient()
public final void setSerializeNulls(boolean serializeNulls)
setSerializeNulls in interface JsonWriterserializeNulls - a boolean.public final boolean getSerializeNulls()
getSerializeNulls
Returns true if object members are serialized when their value is null. This has no impact on array elements. The default is true.
getSerializeNulls in interface JsonWriterpublic DefaultJsonWriter beginArray()
JsonWriter.endArray().beginArray in interface JsonWriterpublic DefaultJsonWriter endArray()
endArray in interface JsonWriterpublic DefaultJsonWriter beginObject()
JsonWriter.endObject().beginObject in interface JsonWriterpublic DefaultJsonWriter endObject()
endObject in interface JsonWriterpublic DefaultJsonWriter name(String name)
name in interface JsonWritername - the name of the forthcoming value. May not be null.public DefaultJsonWriter unescapeName(String name)
unescapeName in interface JsonWritername - the name of the forthcoming value. May not be null.public DefaultJsonWriter value(String value)
value.value in interface JsonWritervalue - the literal string value, or null to encode a null literal.public DefaultJsonWriter unescapeValue(String value)
value without escaping it.unescapeValue in interface JsonWritervalue - the literal string value, or null to encode a null literal.public DefaultJsonWriter nullValue()
null.nullValue in interface JsonWriterpublic DefaultJsonWriter cancelName()
cancelName
cancelName in interface JsonWriterJsonWriter object.public DefaultJsonWriter value(boolean value)
value.value in interface JsonWritervalue - a boolean.public DefaultJsonWriter value(double value)
value.value in interface JsonWritervalue - a finite value. May not be NaNs or
infinities.public DefaultJsonWriter value(long value)
value.value in interface JsonWritervalue - a long.public DefaultJsonWriter value(Number value)
value.value in interface JsonWritervalue - a finite value. May not be NaNs or
infinities.public DefaultJsonWriter rawValue(Object value)
value.toString() as is.rawValue in interface JsonWritervalue - a value .public void flush()
StringBuilder
and flushes that writer.flush in interface JsonWriterpublic void close()
StringBuilder.close in interface JsonWriterpublic static void encodeString(String value, StringBuilder out)
encodeString.
value - a String object.out - a StringBuilder object.public String getOutput()
getOutput
getOutput in interface JsonWriterCopyright © 2019. All rights reserved.