public class JSONStringer extends JSONWriter
JSONObject.toString()
and JSONArray.toString()
. Most
application developers should use those methods directly and disregard this
API. For example:JSONObject object = ... String json = object.toString();
Stringers only encode well-formed JSON strings. In particular:
array()
must
have a matching call to endArray()
and every call to object()
must have a matching call to endObject()
.
value
calls, or by nesting arrays or objects.
JSONException
.
This class provides no facility for pretty-printing (ie. indenting)
output. To encode indented output, use JSONObject.toString(int)
or
JSONArray.toString(int)
.
Some implementations of the API support at most 20 levels of nesting.
Attempts to create more than 20 levels of nesting may fail with a JSONException
.
Each stringer may be used to encode a single top level value. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
Modifier and Type | Field and Description |
---|---|
protected StringBuilder |
out
The output data, containing at most one top-level array or object.
|
mode, writer
Constructor and Description |
---|
JSONStringer() |
Modifier and Type | Method and Description |
---|---|
JSONStringer |
array()
Begins encoding a new array.
|
protected JSONStringer |
createKey(String name)
Creates String representation of the key (property name) to this stringer
Override this method to provide your own representation of the name.
|
JSONStringer |
endArray()
Ends encoding the current array.
|
JSONStringer |
endObject()
Ends encoding the current object.
|
JSONStringer |
entry(Map.Entry<String,Object> entry)
Encodes
key /value pair to this stringer. |
JSONStringer |
key(String name)
Encodes the key (property name) to this stringer.
|
JSONStringer |
object()
Begins encoding a new object.
|
String |
toString()
Returns the encoded JSON string.
|
JSONStringer |
value(boolean value)
Encodes
value to this stringer. |
JSONStringer |
value(double value)
Encodes
value to this stringer. |
JSONStringer |
value(long value)
Encodes
value to this stringer. |
JSONStringer |
value(Object value)
Encodes
value . |
protected final StringBuilder out
public JSONStringer array() throws JSONException
endArray()
.array
in class JSONWriter
JSONException
- On internal errors. Shouldn't happen.public JSONStringer endArray() throws JSONException
endArray
in class JSONWriter
JSONException
- On internal errors. Shouldn't happen.public JSONStringer object() throws JSONException
endObject()
.object
in class JSONWriter
JSONException
- On internal errors. Shouldn't happen.public JSONStringer endObject() throws JSONException
endObject
in class JSONWriter
JSONException
- On internal errors. Shouldn't happen.public JSONStringer value(Object value) throws JSONException
value
.value
in class JSONWriter
value
- a JSONObject
, JSONArray
, String, Boolean,
Integer, Long, Double or null. May not be NaNs
or infinities
.JSONException
- On internal errors. Shouldn't happen.public JSONStringer value(boolean value) throws JSONException
value
to this stringer.value
in class JSONWriter
value
- The value to encode.JSONException
- On internal errors. Shouldn't happen.public JSONStringer value(double value) throws JSONException
value
to this stringer.value
in class JSONWriter
value
- a finite value. May not be NaNs
or
infinities
.JSONException
- On internal errors. Shouldn't happen.public JSONStringer value(long value) throws JSONException
value
to this stringer.value
in class JSONWriter
value
- The value to encode.JSONException
- If we have an internal error. Shouldn't happen.public JSONStringer entry(Map.Entry<String,Object> entry)
key
/value
pair to this stringer.entry
- The entry to encode.JSONException
- If we have an internal error. Shouldn't happen.protected JSONStringer createKey(String name)
name
- the name of the forthcoming value.public JSONStringer key(String name) throws JSONException
key
in class JSONWriter
name
- the name of the forthcoming value. May not be null.JSONException
- on internal errors, shouldn't happen.public String toString()
If invoked with unterminated arrays or unclosed objects, this method's return value is undefined.
Warning: although it contradicts the general contract
of Object.toString()
, this method returns null if the stringer
contains no data.
Copyright © 2006–2018 Apache Software Foundation. All rights reserved.