AI-generated Key Takeaways
-
JSONStringeris a utility for manually building JSON strings but most developers should useJSONObject.toString()orJSONArray.toString()instead. -
It enforces strict JSON structure rules, such as balanced scopes and correct key-value pairs, throwing a
JSONExceptionfor violations. -
While it provides methods for building JSON arrays and objects, it doesn't handle pretty-printing, for which the
toString(int)methods ofJSONObjectandJSONArrayshould be used. -
Note that nesting is limited and excessive nesting might lead to a
JSONException. -
Instances of
JSONStringerare not thread-safe and should not be shared between threads.
Implements 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
valuecalls, 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.
Public Constructor Summary
Public Method Summary
| JSONStringer |
array()
Begins encoding a new array.
|
| JSONStringer |
endArray()
Ends encoding the current array.
|
| JSONStringer |
endObject()
Ends encoding the current object.
|
| 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(long value)
Encodes
value to this stringer. |
| JSONStringer |
value(double value)
Encodes
value to this stringer. |
| JSONStringer |
value(boolean value)
Encodes
value to this stringer. |
| JSONStringer |
value(Object value)
Encodes
value. |
Inherited Method Summary
Public Constructors
public JSONStringer ()
Public Methods
public JSONStringer array ()
Begins encoding a new array. Each call to this method must be paired with
a call to endArray().
Returns
- this stringer.
Throws
| JSONException |
|---|
public JSONStringer endArray ()
public JSONStringer endObject ()
public JSONStringer key (String name)
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 |
|---|
public JSONStringer object ()
Begins encoding a new object. Each call to this method must be paired
with a call to endObject().
Returns
- this stringer.
Throws
| JSONException |
|---|
public String 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.
public JSONStringer value (long value)
public JSONStringer value (double value)
Encodes value to this stringer.
Parameters
| value | a finite value. May not be NaNs or
infinities. |
|---|
Returns
- this stringer.
Throws
| JSONException |
|---|
public JSONStringer value (boolean value)
public JSONStringer value (Object value)
Encodes value.
Parameters
| value | a JSONObject, JSONArray, String, Boolean,
Integer, Long, Double or null. May not be NaNs
or infinities. |
|---|
Returns
- this stringer.
Throws
| JSONException |
|---|