# Print output for @column tags ?>
public
final
class
JsonWriter
extends Object
implements
Closeable
java.lang.Object | |
↳ | android.util.JsonWriter |
Writes a JSON (RFC 4627) 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.
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(boolean)
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(String)
with the property's value. Write property values with the
appropriate value(boolean)
method or by nesting other objects or arrays.
Finally close the object using endObject()
.
[
{
"id": 912345678901,
"text": "How do I write JSON on Android?",
"geo": null,
"user": {
"name": "android_newb",
"followers_count": 41
}
},
{
"id": 912345678902,
"text": "@android_newb just use android.util.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
.
Public constructors | |
---|---|
JsonWriter(Writer out)
Creates a new instance that writes a JSON-encoded stream to |
Public methods | |
---|---|
JsonWriter
|
beginArray()
Begins encoding a new array. |
JsonWriter
|
beginObject()
Begins encoding a new object. |
void
|
close()
Flushes and closes this writer and the underlying |
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 |
boolean
|
isLenient()
Returns true if this writer has relaxed syntax rules. |
JsonWriter
|
name(String name)
Encodes the property name. |
JsonWriter
|
nullValue()
Encodes |
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. |
JsonWriter
|
value(double value)
Encodes |
JsonWriter
|
value(Number value)
Encodes |
JsonWriter
|
value(boolean value)
Encodes |
JsonWriter
|
value(long value)
Encodes |
JsonWriter
|
value(String value)
Encodes |
Inherited methods | |
---|---|
public JsonWriter (Writer out)
Creates a new instance that writes a JSON-encoded stream to out
.
For best performance, ensure Writer
is buffered; wrapping in
BufferedWriter
if necessary.
Parameters | |
---|---|
out |
Writer |
public JsonWriter beginArray ()
Begins encoding a new array. Each call to this method must be paired with
a call to endArray()
.
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter beginObject ()
Begins encoding a new object. Each call to this method must be paired
with a call to endObject()
.
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public void close ()
Flushes and closes this writer and the underlying Writer
.
Throws | |
---|---|
IOException |
if the JSON document is incomplete. |
public JsonWriter endArray ()
Ends encoding the current array.
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter endObject ()
Ends encoding the current object.
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public void flush ()
Ensures all buffered data is written to the underlying Writer
and flushes that writer.
Throws | |
---|---|
IOException |
public boolean isLenient ()
Returns true if this writer has relaxed syntax rules.
Returns | |
---|---|
boolean |
public JsonWriter name (String name)
Encodes the property name.
Parameters | |
---|---|
name |
String : the name of the forthcoming value. May not be null. |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter nullValue ()
Encodes null
.
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public void setIndent (String indent)
Sets the indentation string to be repeated for each level of indentation
in the encoded document. If indent.isEmpty()
the encoded document
will be compact. Otherwise the encoded document will be more
human-readable.
Parameters | |
---|---|
indent |
String : a string containing only whitespace. |
public void setLenient (boolean lenient)
Configure this writer to relax its syntax rules. By default, this writer only emits well-formed JSON as specified by RFC 4627. Setting the writer to lenient permits the following:
Double#isNaN()
or Double#isInfinite()
.
Parameters | |
---|---|
lenient |
boolean |
public JsonWriter value (double value)
Encodes value
.
Parameters | |
---|---|
value |
double : a finite value. May not be Double#isNaN() or
Double#isInfinite() unless this writer is lenient. |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter value (Number value)
Encodes value
.
Parameters | |
---|---|
value |
Number : a finite value. May not be Double#isNaN() or
Double#isInfinite() unless this writer is lenient. |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter value (boolean value)
Encodes value
.
Parameters | |
---|---|
value |
boolean |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter value (long value)
Encodes value
.
Parameters | |
---|---|
value |
long |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |
public JsonWriter value (String value)
Encodes value
.
Parameters | |
---|---|
value |
String : the literal string value, or null to encode a null literal. |
Returns | |
---|---|
JsonWriter |
this writer. |
Throws | |
---|---|
IOException |