JsonWriter
public final class JsonWriter
extends Object
implements Closeable
将JSON( RFC 4627 )编码值写入一个流,一次一个令牌。 该流包含字面值(字符串,数字,布尔值和空值)以及对象和数组的开始和结束分隔符。
Encoding JSON
To encode your data as JSON, create a new
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:
- To write arrays, first call
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()
.
- To write objects, first call
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()
.
Example
Suppose we'd like to encode a stream of messages such as the following:
[
{
"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();
}
每个JsonWriter
可用于编写单个JSON流。 这个类的实例不是线程安全的。 导致格式错误的JSON字符串的调用将失败,并显示IllegalStateException
。
Summary
Public constructors
Public methods
close
void close ()
刷新并关闭此作者和底层的 Writer
。
flush
void flush ()
确保将所有缓冲数据写入底层 Writer
并刷新该作者。
isLenient
boolean isLenient ()
如果此作者放宽了语法规则,则返回true。
name
JsonWriter name (String name)
编码属性名称。
Parameters |
name |
String : the name of the forthcoming value. May not be null. |
setIndent
void setIndent (String indent)
设置要在编码文档中为缩进的每个级别重复的缩进字符串。 如果indent.isEmpty()
编码的文件将是紧凑的。 否则,编码后的文档将会更加人性化。
Parameters |
indent |
String : a string containing only whitespace. |
setLenient
void setLenient (boolean lenient)
配置此编写器以放宽其语法规则。 默认情况下,该作者只发出RFC 4627指定的格式良好的JSON。 将作者设置为宽松允许以下内容:
- Top-level values of any type. With strict writing, the top-level value must be an object or an array.
- Numbers may be
NaNs
or infinities
.
Parameters |
lenient |
boolean
|
value
JsonWriter value (double value)
编码 value
。
Parameters |
value |
double : a finite value. May not be NaNs or infinities unless this writer is lenient. |
value
JsonWriter value (Number value)
编码 value
。
Parameters |
value |
Number : a finite value. May not be NaNs or infinities unless this writer is lenient. |
value
JsonWriter value (String value)
编码 value
。
Parameters |
value |
String : the literal string value, or null to encode a null literal. |