Expand Minimize Picture-in-picture Power Device Status Voice Recognition Skip Back Skip Forward Minus Plus Play Search
Documentation
Sidebar

Class JSONStringer

Hierarchy

java.lang.Object

Package
org.json

Overview

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 JSONException.

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 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.

Constructors

JSONStringer()

public JSONStringer()

Methods

array()

Begins encoding a new array. Each call to this method must be paired with a call to {@link #endArray}.

public org.json.JSONStringer array() throws org.json.JSONException

Throws

endArray()

Ends encoding the current array.

public org.json.JSONStringer endArray() throws org.json.JSONException

Throws

object()

Begins encoding a new object. Each call to this method must be paired with a call to {@link #endObject}.

public org.json.JSONStringer object() throws org.json.JSONException

Throws

endObject()

Ends encoding the current object.

public org.json.JSONStringer endObject() throws org.json.JSONException

Throws

value( Object value )

Encodes {@code value}.

public org.json.JSONStringer value(Object value) throws org.json.JSONException

Parameters

value
a {@link JSONObject}, {@link JSONArray}, String, Boolean, Integer, Long, Double or null. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.

Throws

value( boolean value )

Encodes {@code value} to this stringer.

public org.json.JSONStringer value(boolean value) throws org.json.JSONException

Parameters

value

Throws

value( double value )

Encodes {@code value} to this stringer.

public org.json.JSONStringer value(double value) throws org.json.JSONException

Parameters

value
a finite value. May not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.

Throws

value( long value )

Encodes {@code value} to this stringer.

public org.json.JSONStringer value(long value) throws org.json.JSONException

Parameters

value

Throws

key( String name )

Encodes the key (property name) to this stringer.

public org.json.JSONStringer key(String name) throws org.json.JSONException

Parameters

name
the name of the forthcoming value. May not be null.

Throws

toString()

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 {@link Object#toString}, this method returns null if the stringer contains no data.

public java.lang.String toString()

Inherited Methods

From Class Methods
java.lang.Object getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize
View on GitHub.com
Previous Section Next Section