java.lang.Object | |
↳ | org.json.JSONStringer |
Implements toString()
and 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 toString(int)
or
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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Begins encoding a new array.
| |||||||||||
Ends encoding the current array.
| |||||||||||
Ends encoding the current object.
| |||||||||||
Encodes the key (property name) to this stringer.
| |||||||||||
Begins encoding a new object.
| |||||||||||
Returns the encoded JSON string.
| |||||||||||
Encodes
value to this stringer.
| |||||||||||
Encodes
value to this stringer.
| |||||||||||
Encodes
value to this stringer.
| |||||||||||
Encodes
value .
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Begins encoding a new array. Each call to this method must be paired with
a call to endArray()
.
JSONException |
---|
Encodes the key (property name) to this stringer.
name | the name of the forthcoming value. May not be null. |
---|
JSONException |
---|
Begins encoding a new object. Each call to this method must be paired
with a call to endObject()
.
JSONException |
---|
Returns the encoded JSON string.
If invoked with unterminated arrays or unclosed objects, this method's return value is undefined.
Warning: although it contradicts the general contract
of toString()
, this method returns null if the stringer
contains no data.
Encodes value
to this stringer.
value | a finite value. May not be NaNs or
infinities . |
---|
JSONException |
---|
Encodes value
.
value | a JSONObject , JSONArray , String, Boolean,
Integer, Long, Double or null. May not be NaNs
or infinities . |
---|
JSONException |
---|