JSONStringer
open class JSONStringer
kotlin.Any | |
↳ | org.json.JSONStringer |
Implements org.json.JSONObject#toString and org.json.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.
Summary
Public constructors | |
---|---|
Public methods | |
---|---|
open JSONStringer! |
array() Begins encoding a new array. |
open JSONStringer! |
endArray() Ends encoding the current array. |
open JSONStringer! |
Ends encoding the current object. |
open JSONStringer! |
Encodes the key (property name) to this stringer. |
open JSONStringer! |
object() Begins encoding a new object. |
open String |
toString() Returns the encoded JSON string. |
open JSONStringer! |
Encodes |
open JSONStringer! |
Encodes |
open JSONStringer! |
Encodes |
open JSONStringer! |
Encodes |
Public constructors
Public methods
array
open fun array(): JSONStringer!
Begins encoding a new array. Each call to this method must be paired with a call to endArray
.
Return | |
---|---|
JSONStringer! |
this stringer. |
endArray
open fun endArray(): JSONStringer!
Ends encoding the current array.
Return | |
---|---|
JSONStringer! |
this stringer. |
endObject
open fun endObject(): JSONStringer!
Ends encoding the current object.
Return | |
---|---|
JSONStringer! |
this stringer. |
key
open fun key(name: String!): JSONStringer!
Encodes the key (property name) to this stringer.
Parameters | |
---|---|
name |
String!: the name of the forthcoming value. May not be null. |
Return | |
---|---|
JSONStringer! |
this stringer. |
object
open fun object(): JSONStringer!
Begins encoding a new object. Each call to this method must be paired with a call to endObject
.
Return | |
---|---|
JSONStringer! |
this stringer. |
toString
open fun toString(): String
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.
Return | |
---|---|
String |
a string representation of the object. |
value
open fun value(value: Any!): JSONStringer!
Encodes value
.
Parameters | |
---|---|
value |
Any!: a JSONObject , JSONArray , String, Boolean, Integer, Long, Double or null. May not be NaNs or infinities . |
Return | |
---|---|
JSONStringer! |
this stringer. |
value
open fun value(value: Boolean): JSONStringer!
Encodes value
to this stringer.
Return | |
---|---|
JSONStringer! |
this stringer. |
value
open fun value(value: Double): JSONStringer!
Encodes value
to this stringer.
Parameters | |
---|---|
value |
Double: a finite value. May not be NaNs or infinities . |
Return | |
---|---|
JSONStringer! |
this stringer. |
value
open fun value(value: Long): JSONStringer!
Encodes value
to this stringer.
Return | |
---|---|
JSONStringer! |
this stringer. |