public class JsonWriter extends Object
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
beginArray
and endArray
methods that make and bound beginArray values,
and beginObject
and endObject
methods which make and bound beginObject
values. All of these methods return the JsonWriter instance, permitting a cascade style. For
example,
new JsonWriter(myWriter) .beginObject() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be beginArray
or beginObject
. There are no
methods for adding commas or colons. JsonWriter adds them for you. Objects and arrays can be
nested up to 200 levels deep.
This can sometimes be easier than using a JsonObject to build a string.
Modifier and Type | Field and Description |
---|---|
protected char |
mode
The current mode.
|
protected Appendable |
writer
The writer that will receive the output.
|
Constructor and Description |
---|
JsonWriter(Appendable w)
Make a fresh JSONWriter.
|
Modifier and Type | Method and Description |
---|---|
JsonWriter |
beginArray()
Begin appending a new beginArray.
|
JsonWriter |
beginObject()
Begin appending a new beginObject.
|
JsonWriter |
endArray()
End an beginArray.
|
JsonWriter |
endObject()
End an beginObject.
|
JsonWriter |
key(String string)
Append a key.
|
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(Object object)
Append an beginObject value.
|
static String |
valueToString(Object value)
Make a JSON text of an Object value.
|
protected char mode
protected Appendable writer
public JsonWriter(Appendable w)
w
- public JsonWriter beginArray() throws JsonException
endArray
will be
appended to this beginArray. The endArray
method must be called to mark the
beginArray's end.JsonException
- If the nesting is too deep, or if the beginObject is started in the wrong
place (for example as a key or after the end of the outermost beginArray or beginObject).public JsonWriter endArray() throws JsonException
beginArray
.JsonException
- If incorrectly nested.public JsonWriter endObject() throws JsonException
beginObject
.JsonException
- If incorrectly nested.public JsonWriter key(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 beginObject() throws JsonException
endObject
will be appended to this beginObject. The endObject
method
must be called to mark the beginObject's end.JsonException
- If the nesting is too deep, or if the beginObject is started in the wrong
place (for example as a key or after the end of the outermost beginArray or beginObject).public static String valueToString(Object value) throws JsonException
Warning: This method assumes that the data structure is acyclical.
value
- The value to be serialized.{
(left brace) and ending with
}
(right brace).JsonException
- If the value is or contains an invalid number.public JsonWriter value(boolean b) throws JsonException
true
or the value false
.b
- A boolean.JsonException
public 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
public JsonWriter value(Object object) throws JsonException
object
- The beginObject to append. It can be null, or a Boolean, Number, String,
JsonObject, or JsonArray, or an beginObject that implements JsonString.JsonException
- If the value is out of sequence.Copyright © 2018 Terra Software Informatics LLC.. All rights reserved.