-
public class JSONStringerImplements 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:
- 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 to endArray and every call to must have a matching call to endObject.
- 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.
This class provides no facility for pretty-printing (ie. indenting) output. To encode indented output, use toString or toString.
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 .
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 JavaItem 17, "Design and Document or inheritance or else prohibit it" for further information.
-
-
Constructor Summary
Constructors Constructor Description JSONStringer()
-
Method Summary
Modifier and Type Method Description JSONStringerarray()Begins encoding a new array. JSONStringerendArray()Ends encoding the current array. JSONStringerobject()Begins encoding a new object. JSONStringerendObject()Ends encoding the current object. JSONStringervalue(Object value)Encodes {@code value}.JSONStringervalue(boolean value)Encodes {@code value}to this stringer.JSONStringervalue(double value)Encodes {@code value}to this stringer.JSONStringervalue(long value)Encodes {@code value}to this stringer.JSONStringerkey(String name)Encodes the key (property name) to this stringer. StringtoString()Returns the encoded JSON string. -
-
Method Detail
-
array
JSONStringer array()
Begins encoding a new array. Each call to this method must be paired witha call to endArray.
-
endArray
JSONStringer endArray()
Ends encoding the current array.
-
object
JSONStringer object()
Begins encoding a new object. Each call to this method must be pairedwith a call to endObject.
-
endObject
JSONStringer endObject()
Ends encoding the current object.
-
value
JSONStringer value(Object value)
Encodes
{@code value}.- Parameters:
value- a JSONObject, JSONArray, String, Boolean,Integer, Long, Double or null.
-
value
JSONStringer value(boolean value)
Encodes
{@code value}to this stringer.
-
value
JSONStringer value(double value)
Encodes
{@code value}to this stringer.- Parameters:
value- a finite value.
-
value
JSONStringer value(long value)
Encodes
{@code value}to this stringer.
-
key
JSONStringer key(String name)
Encodes the key (property name) to this stringer.
- Parameters:
name- the name of the forthcoming value.
-
-
-
-