Added in API level 1

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 to endArray and every call to object 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.
Calls that would result in a malformed JSON string will fail with a 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!

Begins encoding a new array.

open JSONStringer!

Ends encoding the current array.

open JSONStringer!

Ends encoding the current object.

open JSONStringer!
key(name: String!)

Encodes the key (property name) to this stringer.

open JSONStringer!

Begins encoding a new object.

open String

Returns the encoded JSON string.

open JSONStringer!
value(value: Boolean)

Encodes value to this stringer.

open JSONStringer!
value(value: Double)

Encodes value to this stringer.

open JSONStringer!
value(value: Any!)

Encodes value.

open JSONStringer!
value(value: Long)

Encodes value to this stringer.

Public constructors

JSONStringer

Added in API level 1
JSONStringer()

Public methods

array

Added in API level 1
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

Added in API level 1
open fun endArray(): JSONStringer!

Ends encoding the current array.

Return
JSONStringer! this stringer.

endObject

Added in API level 1
open fun endObject(): JSONStringer!

Ends encoding the current object.

Return
JSONStringer! this stringer.

key

Added in API level 1
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

Added in API level 1
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

Added in API level 1
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

Added in API level 1
open fun value(value: Boolean): JSONStringer!

Encodes value to this stringer.

Return
JSONStringer! this stringer.

value

Added in API level 1
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

Added in API level 1
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

Added in API level 1
open fun value(value: Long): JSONStringer!

Encodes value to this stringer.

Return
JSONStringer! this stringer.