Class JSONStringer
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:
- The stringer must have exactly one top-level array or object.
- Lexical scopes must be balanced: every call to
array()
must have a matching call toendArray()
and every call toobject()
must have a matching call toendObject()
. - Arrays may not contain keys (property names).
- Objects must alternate keys (property names) and values.
- Values are inserted with either literal
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionarray()
Begins encoding a new array.endArray()
Ends encoding the current array.Ends encoding the current object.Encodes the key (property name) to this stringer.object()
Begins encoding a new object.toString()
Returns the encoded JSON string.value
(boolean value) Encodesvalue
to this stringer.value
(double value) Encodesvalue
to this stringer.value
(long value) Encodesvalue
to this stringer.Encodesvalue
.
-
Constructor Details
-
JSONStringer
public JSONStringer()
-
-
Method Details
-
array
Begins encoding a new array. Each call to this method must be paired with a call toendArray()
.- Returns:
- this stringer.
- Throws:
JSONException
-
endArray
Ends encoding the current array.- Returns:
- this stringer.
- Throws:
JSONException
-
object
Begins encoding a new object. Each call to this method must be paired with a call toendObject()
.- Returns:
- this stringer.
- Throws:
JSONException
-
endObject
Ends encoding the current object.- Returns:
- this stringer.
- Throws:
JSONException
-
value
Encodesvalue
.- Parameters:
value
- aJSONObject
,JSONArray
, String, Boolean, Integer, Long, Double or null. May not beNaNs
orinfinities
.- Returns:
- this stringer.
- Throws:
JSONException
-
value
Encodesvalue
to this stringer.- Returns:
- this stringer.
- Throws:
JSONException
-
value
Encodesvalue
to this stringer.- Parameters:
value
- a finite value. May not beNaNs
orinfinities
.- Returns:
- this stringer.
- Throws:
JSONException
-
value
Encodesvalue
to this stringer.- Returns:
- this stringer.
- Throws:
JSONException
-
key
Encodes the key (property name) to this stringer.- Parameters:
name
- the name of the forthcoming value. May not be null.- Returns:
- this stringer.
- Throws:
JSONException
-
toString
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
Object.toString()
, this method returns null if the stringer contains no data.
-