Json Writer
Writes a JSON (RFC 7159) encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans and nulls) as well as the begin and end delimiters of objects and arrays.
Encoding JSON
To encode your data as JSON, create a newJsonWriter. 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: - To write arrays, first call 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.
- To write objects, first call 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.
Example
Suppose we'd like to encode a stream of messages such as the following:
[
{
"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
}
}
]
Content copied to clipboard
public void writeJsonStream(BufferedSink sink, List<Message> messages) throws IOException {
JsonWriter writer = JsonWriter.of(sink);
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();
}
Content copied to clipboard
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 .
Functions
Link copied to clipboard
Begins encoding a new array.
Link copied to clipboard
Cancels immediately-nested calls to beginArray or beginObject and their matching calls to endArray or endObject.
Link copied to clipboard
Begins encoding a new object.
Link copied to clipboard
Ends encoding the current array.
Link copied to clipboard
Ends nested call flattening created by beginFlatten.
Link copied to clipboard
Ends encoding the current object.
Link copied to clipboard
Returns a JsonPath to the current location in the JSON value.
Link copied to clipboard
Returns true if this writer has relaxed syntax rules.
Link copied to clipboard
Encodes the value which may be a string, number, boolean, null, map, or list.
Link copied to clipboard
Encodes the property name.
Link copied to clipboard
Encodes
null.Link copied to clipboard
Returns a new instance that writes UTF-8 encoded JSON to
sink.Link copied to clipboard
Changes the writer to treat the next value as a string name.
Link copied to clipboard
Configure this writer to relax its syntax rules.
Link copied to clipboard
Encodes
value.Writes
source directly without encoding its contents.Link copied to clipboard
Returns a BufferedSink into which arbitrary data can be written without any additional encoding.