public class JSONStringer
extends java.lang.Object
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 | Class and Description |
|---|---|
(package private) static class |
JSONStringer.Scope
Lexical scoping elements within this stringer, necessary to insert the
appropriate separator characters (ie.
|
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
indent
A string containing a full set of spaces for a single level of
indentation, or null for no pretty printing.
|
(package private) java.lang.StringBuilder |
out
The output data, containing at most one top-level array or object.
|
private java.util.List<JSONStringer.Scope> |
stack
Unlike the original implementation, this stack isn't limited to 20
levels of nesting.
|
| Constructor and Description |
|---|
JSONStringer() |
JSONStringer(int indentSpaces) |
| Modifier and Type | Method and Description |
|---|---|
JSONStringer |
array()
Begins encoding a new array.
|
private void |
beforeKey()
Inserts any necessary separators and whitespace before a name.
|
private void |
beforeValue()
Inserts any necessary separators and whitespace before a literal value,
inline array, or inline object.
|
(package private) JSONStringer |
close(JSONStringer.Scope empty,
JSONStringer.Scope nonempty,
java.lang.String closeBracket)
Closes the current scope by appending any necessary whitespace and the
given bracket.
|
JSONStringer |
endArray()
Ends encoding the current array.
|
JSONStringer |
endObject()
Ends encoding the current object.
|
JSONStringer |
key(java.lang.String name)
Encodes the key (property name) to this stringer.
|
private void |
newline() |
JSONStringer |
object()
Begins encoding a new object.
|
(package private) JSONStringer |
open(JSONStringer.Scope empty,
java.lang.String openBracket)
Enters a new scope by appending any necessary whitespace and the given
bracket.
|
private JSONStringer.Scope |
peek()
Returns the value on the top of the stack.
|
private void |
replaceTop(JSONStringer.Scope topOfStack)
Replace the value on the top of the stack with the given value.
|
private void |
string(java.lang.String value) |
java.lang.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(java.lang.Object value)
Encodes
value. |
final java.lang.StringBuilder out
private final java.util.List<JSONStringer.Scope> stack
private final java.lang.String indent
public JSONStringer array() throws JSONException
endArray().JSONExceptionpublic JSONStringer endArray() throws JSONException
JSONExceptionpublic JSONStringer object() throws JSONException
endObject().JSONExceptionpublic JSONStringer endObject() throws JSONException
JSONExceptionJSONStringer open(JSONStringer.Scope empty, java.lang.String openBracket) throws JSONException
JSONExceptionJSONStringer close(JSONStringer.Scope empty, JSONStringer.Scope nonempty, java.lang.String closeBracket) throws JSONException
JSONExceptionprivate JSONStringer.Scope peek() throws JSONException
JSONExceptionprivate void replaceTop(JSONStringer.Scope topOfStack)
public JSONStringer value(java.lang.Object value) throws JSONException
value.value - a JSONObject, JSONArray, String, Boolean,
Integer, Long, Double or null. May not be NaNs
or infinities.JSONExceptionpublic JSONStringer value(boolean value) throws JSONException
value to this stringer.JSONExceptionpublic JSONStringer value(double value) throws JSONException
value to this stringer.value - a finite value. May not be NaNs or
infinities.JSONExceptionpublic JSONStringer value(long value) throws JSONException
value to this stringer.JSONExceptionprivate void string(java.lang.String value)
private void newline()
public JSONStringer key(java.lang.String name) throws JSONException
name - the name of the forthcoming value. May not be null.JSONExceptionprivate void beforeKey()
throws JSONException
JSONExceptionprivate void beforeValue()
throws JSONException
JSONExceptionpublic java.lang.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.
toString in class java.lang.Object