Public 成员函数 | |
| JsonWriter (Writer out) | |
| final void | setIndent (String indent) |
| final void | setLenient (boolean lenient) |
| boolean | isLenient () |
| final void | setHtmlSafe (boolean htmlSafe) |
| final boolean | isHtmlSafe () |
| final void | setSerializeNulls (boolean serializeNulls) |
| final boolean | getSerializeNulls () |
| JsonWriter | beginArray () throws IOException |
| JsonWriter | endArray () throws IOException |
| JsonWriter | beginObject () throws IOException |
| JsonWriter | endObject () throws IOException |
| JsonWriter | name (String name) throws IOException |
| JsonWriter | value (String value) throws IOException |
| JsonWriter | nullValue () throws IOException |
| JsonWriter | value (boolean value) throws IOException |
| JsonWriter | value (double value) throws IOException |
| JsonWriter | value (long value) throws IOException |
| JsonWriter | value (Number value) throws IOException |
| void | flush () throws IOException |
| void | close () throws IOException |
包函数 | |
| [instance initializer] | |
Private 成员函数 | |
| JsonWriter | open (JsonScope empty, String openBracket) throws IOException |
| JsonWriter | close (JsonScope empty, JsonScope nonempty, String closeBracket) throws IOException |
| JsonScope | peek () |
| void | replaceTop (JsonScope topOfStack) |
| void | writeDeferredName () throws IOException |
| void | string (String value) throws IOException |
| void | newline () throws IOException |
| void | beforeName () throws IOException |
| void | beforeValue (boolean root) throws IOException |
Private 属性 | |
| final Writer | out |
| final List< JsonScope > | stack = new ArrayList<JsonScope>() |
| String | indent |
| String | separator = ":" |
| boolean | lenient |
| boolean | htmlSafe |
| String | deferredName |
| boolean | serializeNulls = true |
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.
To encode your data as JSON, create a new
. 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:
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}}]
This code encodes the above structure:
writer.setIndentSpaces(4);writeMessagesArray(writer, messages);writer.close();}writer.beginArray();for (Message message : messages) {writeMessage(writer, message);}writer.endArray();}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();}writer.beginObject();writer.name("name").value(user.getName());writer.name("followers_count").value(user.getFollowersCount());writer.endObject();}writer.beginArray();for (Double value : doubles) {writer.value(value);}writer.endArray();}
Each
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.
|
inline |
Creates a new instance that writes a JSON-encoded stream to
. For best performance, ensure Writer is buffered; wrapping in BufferedWriter if necessary.
|
inlinepackage |
|
inlineprivate |
Inserts any necessary separators and whitespace before a name. Also adjusts the stack to expect the name's value.
|
inlineprivate |
Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element.
| root | true if the value is a new array or object, the two values permitted as top-level elements. |
|
inline |
Begins encoding a new array. Each call to this method must be paired with a call to endArray.
|
inline |
Begins encoding a new object. Each call to this method must be paired with a call to endObject.
|
inlineprivate |
Closes the current scope by appending any necessary whitespace and the given bracket.
|
inline |
Flushes and closes this writer and the underlying Writer.
| IOException | if the JSON document is incomplete. |
|
inline |
Ends encoding the current array.
|
inline |
Ends encoding the current object.
|
inline |
Ensures all buffered data is written to the underlying Writer and flushes that writer.
|
inline |
Returns true if object members are serialized when their value is null. This has no impact on array elements. The default is true.
|
inline |
Returns true if this writer writes JSON that's safe for inclusion in HTML and XML documents.
|
inline |
Returns true if this writer has relaxed syntax rules.
|
inline |
Encodes the property name.
| name | the name of the forthcoming value. May not be null. |
|
inlineprivate |
|
inline |
Encodes
.
|
inlineprivate |
Enters a new scope by appending any necessary whitespace and the given bracket.
|
inlineprivate |
Returns the value on the top of the stack.
|
inlineprivate |
Replace the value on the top of the stack with the given value.
|
inline |
Configure this writer to emit JSON that's safe for direct inclusion in HTML and XML documents. This escapes the HTML characters
,
,
and
before writing them to the stream. Without this setting, your XML/HTML encoder should replace these characters with the corresponding escape sequences.
|
inline |
Sets the indentation string to be repeated for each level of indentation in the encoded document. If
the encoded document will be compact. Otherwise the encoded document will be more human-readable.
| indent | a string containing only whitespace. |
|
inline |
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:
|
inline |
Sets whether object members are serialized when their value is null. This has no impact on array elements. The default is true.
|
inlineprivate |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inlineprivate |
|
private |
|
private |
|
private |
A string containing a full set of spaces for a single level of indentation, or null for no pretty printing.
|
private |
|
private |
The output data, containing at most one top-level array or object.
|
private |
The name/value separator; either ":" or ": ".
|
private |
1.8.8