public class JSONWriter
extends java.lang.Object
implements java.io.Closeable
A JSONWriter instance provides a value
method for appending
values to the
text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and
object
and endObject
methods which make and bound
object values. All of these methods return the JSONWriter instance,
permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
,
unless this object has been constructed with allowSimpleValues
set to true.
There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested arbitrarily deep.
This can be less memory intensive than using a JSONObject to build a string.
Modifier and Type | Field and Description |
---|---|
protected boolean |
done
Indicate when writing has finished.
|
protected java.lang.Appendable |
writer
The writer that will receive the output.
|
Constructor and Description |
---|
JSONWriter(java.lang.Appendable w)
Make a fresh JSONWriter.
|
JSONWriter(java.lang.Appendable w,
boolean allowSimpleValues)
Make a fresh JSONWriter.
|
JSONWriter(java.lang.Appendable w,
int indentFactor)
Make a fresh JSONWriter with the given indent factor.
|
JSONWriter(java.lang.Appendable w,
int indentFactor,
int indent)
Make a fresh JSONWriter with the given indent factor and initial
indentation.
|
JSONWriter(java.lang.Appendable w,
int indentFactor,
int indent,
boolean allowSimpleValues)
Make a fresh JSONWriter with the given indent factor and initial
indentation.
|
Modifier and Type | Method and Description |
---|---|
JSONWriter |
array()
Begin appending a new array.
|
void |
close()
Asserts the JSON writer is finished, and close any underlying
Closeable writer. |
JSONWriter |
endArray()
End an array.
|
JSONWriter |
endObject()
End an object.
|
JSONWriter |
entries(java.util.Map<?,?> kvPairs)
Append a sequence of keys and values in an object.
|
JSONWriter |
key(java.lang.String string)
Append a key.
|
JSONWriter |
nullValue()
Append a
null value. |
JSONWriter |
object()
Begin appending a new object.
|
JSONWriter |
value(boolean b)
Append either the value
true or the value
false . |
JSONWriter |
value(double d)
Append a double value.
|
JSONWriter |
value(long l)
Append a long value.
|
JSONWriter |
value(java.lang.Object object)
Append an object value.
|
JSONWriter |
values(java.lang.Iterable<?> values)
Append a sequence of values into an array.
|
protected boolean done
protected final java.lang.Appendable writer
public JSONWriter(java.lang.Appendable w)
w
- the Writer to which JSON content will be writtenpublic JSONWriter(java.lang.Appendable w, boolean allowSimpleValues)
w
- the Writer to which JSON content will be writtenallowSimpleValues
- true
to allow the root value to be
a simple value, otherwise false
to only allow objects and arrays
at the root levelpublic JSONWriter(java.lang.Appendable w, int indentFactor)
w
- the Writer to which JSON content will be writtenindentFactor
- indent level, or 0 for compact outputpublic JSONWriter(java.lang.Appendable w, int indentFactor, int indent)
w
- the Writer to which JSON content will be writtenindentFactor
- indent level, or 0 for compact outputindent
- initial indentpublic JSONWriter(java.lang.Appendable w, int indentFactor, int indent, boolean allowSimpleValues)
w
- the Writer to which JSON content will be writtenindentFactor
- indent level, or 0 for compact outputindent
- initial indentallowSimpleValues
- true
to allow the root value to be
a simple value, otherwise false
to only allow objects and arrays
at the root levelpublic JSONWriter array() throws JSONException
endArray
will be appended to this array. The
endArray
method must be called to mark the array's end.JSONException
- If the nesting is too deep, or if the object is
started in the wrong place (for example as a key or after the end of the
outermost array or object).public JSONWriter endArray() throws JSONException
array
.JSONException
- If incorrectly nested.public JSONWriter endObject() throws JSONException
object
.JSONException
- If incorrectly nested.public JSONWriter key(java.lang.String string) throws JSONException
string
- A key string.JSONException
- If the key is out of place. For example, keys
do not belong in arrays or if the key is null.public JSONWriter object() throws JSONException
endObject
will be appended to this object. The
endObject
method must be called to mark the object's end.JSONException
- If the nesting is too deep, or if the object is
started in the wrong place (for example as a key or after the end of the
outermost array or object).public JSONWriter nullValue() throws JSONException
null
value.JSONException
- there was a problem writing the null valuepublic JSONWriter value(boolean b) throws JSONException
true
or the value
false
.b
- A boolean.JSONException
- there was a problem writing the boolean valuepublic JSONWriter value(double d) throws JSONException
d
- A double.JSONException
- If the number is not finite.public JSONWriter value(long l) throws JSONException
l
- A long.JSONException
- there was a problem writing the long valuepublic JSONWriter value(java.lang.Object object) throws JSONException
object
- The object to appendString. It can be null, or a Boolean, Number,
String, JSONObject, or JSONArray, or an object that implements JSONString.JSONException
- If the value is out of sequence.public JSONWriter values(java.lang.Iterable<?> values) throws JSONException
values
- The objects to appendString. They can be null, or a Boolean, Number,
String, JSONObject, or JSONArray, or an object that implements JSONString.JSONException
- If a value is out of place. For example, a value
occurs where a key is expected.public JSONWriter entries(java.util.Map<?,?> kvPairs) throws JSONException
kvPairs
- The objects to appendString. The values can be null, or a Boolean,
Number, String, JSONObject, or JSONArray, or an object that implements
JSONString.JSONException
- If a key or value is out of place. For example, keys
do not belong in arrays or if the key is null.public void close() throws java.io.IOException
Closeable
writer.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- the writer cannot be closed