java.lang.Object | |
↳ | com.neomades.json.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 ?", "geo": null, "user": { "name": "_newb", "followers_count": 41 } }, { "id": 912345678902, "text": "@_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, Vector messages) throws IOException { JSONWriter writer = new JSONWriter(new OutputStreamWriter(out, "UTF-8")); writer.setIndent(" "); writeMessagesArray(writer, messages); writer.close(); } public void writeMessagesArray(JSONWriter writer, Vector messages) throws IOException { writer.beginArray(); Message message = null; for (int i = 0; i < messages.size(); i++) { message = (Message) messages.elementAt(i); 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, Vector doubles) throws IOException { writer.beginArray(); Double value = null; for (int i = 0; i < doubles.size(); i++) { value = (Double) value; 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
out . | |||||||||||
JSONWriter()
Creates a new instance that writes a JSON-encoded stream, using an internal
StringWriter.
|
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
Writer . | ||||||||||
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
Writer and
flushes that writer. | ||||||||||
boolean |
isLenient()
Returns true if this writer has relaxed syntax rules.
| ||||||||||
JSONWriter |
name(String name)
Encodes the property name.
| ||||||||||
JSONWriter |
nullValue()
Encodes
null . | ||||||||||
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.
| ||||||||||
String |
toString()
Retrieve the JSON-encoded string.
| ||||||||||
JSONWriter |
value(Boolean value)
Encodes
value . | ||||||||||
JSONWriter |
value(Integer value)
Encodes
value . | ||||||||||
JSONWriter |
value(double value)
Encodes
value . | ||||||||||
JSONWriter |
value(boolean value)
Encodes
value . | ||||||||||
JSONWriter |
value(Long value)
Encodes
value . | ||||||||||
JSONWriter |
value(long value)
Encodes
value . | ||||||||||
JSONWriter |
value(String value)
Encodes
value . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Creates a new instance that writes a JSON-encoded stream to out
. For
best performance, ensure Writer
is buffered.
Creates a new instance that writes a JSON-encoded stream, using an internal StringWriter.
To retrieve the built String, use the toString()
method.
Begins encoding a new array. Each call to this method must be paired with a
call to endArray()
.
IOException |
---|
Begins encoding a new object. Each call to this method must be paired with a
call to endObject()
.
IOException |
---|
Flushes and closes this writer and the underlying Writer
.
IOException | if the JSON document is incomplete. |
---|
Ensures all buffered data is written to the underlying Writer
and
flushes that writer.
IOException |
---|
Returns true if this writer has relaxed syntax rules.
Encodes the property name.
name | the name of the forthcoming value. May not be null. |
---|
IOException |
---|
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.
indent | a string containing only whitespace. |
---|
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:
Retrieve the JSON-encoded string.
Encodes value
.
value | a finite value. May not be NaNs or infinities unless this writer is lenient. |
---|
IOException |
---|
Encodes value
.
value | the literal string value, or null to encode a null literal. |
---|
IOException |
---|