JSONStringer class

Implements {@link JSONObject#toString} and {@link 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 {@link #array} must have a matching call to {@link #endArray} and every call to {@link #object} must have a matching call to {@link #endObject}.
  • Arrays may not contain keys (property names).
  • Objects must alternate keys (property names) and values.
  • Values are inserted with either literal {@link #value(Object) value} calls, or by nesting arrays or objects.
Calls that would result in a malformed JSON string will fail with a {@link EasyJSONException}.

This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use {@link JSONObject#toString(int)} or {@link 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 {@link EasyJSONException}.

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.

Constructors

JSONStringer([int indentSpaces])

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
indent String
A string containing a full set of spaces for a single level of indentation, or null for no pretty printing.
read / write
out StringBuffer
The output data, containing at most one top-level array or object.
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
stack List<Scope>
Unlike the original implementation, this stack isn't limited to 20 levels of nesting.
final

Methods

array() JSONStringer
Begins encoding a new array. Each call to this method must be paired with a call to {@link #endArray}. [...]
beforeKey() → void
Inserts any necessary separators and whitespace before a name. Also adjusts the stack to expect the key's value.
beforeValue() → void
Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element.
close(Scope empty, Scope nonempty, String closeBracket) JSONStringer
Closes the current scope by appending any necessary whitespace and the given bracket.
endArray() JSONStringer
Ends encoding the current array. [...]
endObject() JSONStringer
Ends encoding the current object. [...]
key(String name) JSONStringer
Encodes the key (property name) to this stringer. [...]
newline() → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
object() JSONStringer
Begins encoding a new object. Each call to this method must be paired with a call to {@link #endObject}. [...]
open(Scope empty, String openBracket) JSONStringer
Enters a new scope by appending any necessary whitespace and the given bracket.
peek() Scope
Returns the value on the top of the stack.
replaceTop(Scope topOfStack) → void
Replace the value on the top of the stack with the given value.
string(String value) → void
toString() String
Returns the encoded JSON string. [...]
override
value(Object value) JSONStringer
Encodes {@code value}. [...]

Operators

operator ==(Object other) bool
The equality operator. [...]
inherited