JSONStringer
bookmark_borderbookmark
Stay organized with collections
Save and categorize content based on your preferences.
public
class
JSONStringer
extends Object
Implements JSONObject.toString
and 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
Inherited methods |
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this object.
|
boolean
|
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
void
|
finalize()
Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
|
final
Class<?>
|
getClass()
Returns the runtime class of this Object .
|
int
|
hashCode()
Returns a hash code value for the object.
|
final
void
|
notify()
Wakes up a single thread that is waiting on this object's
monitor.
|
final
void
|
notifyAll()
Wakes up all threads that are waiting on this object's monitor.
|
String
|
toString()
Returns a string representation of the object.
|
final
void
|
wait(long timeoutMillis, int nanos)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait(long timeoutMillis)
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted, or until a
certain amount of real time has elapsed.
|
final
void
|
wait()
Causes the current thread to wait until it is awakened, typically
by being notified or interrupted.
|
|
Public constructors
JSONStringer
public JSONStringer ()
Public methods
array
public JSONStringer array ()
Begins encoding a new array. Each call to this method must be paired with
a call to endArray()
.
endArray
public JSONStringer endArray ()
Ends encoding the current array.
endObject
public JSONStringer endObject ()
Ends encoding the current object.
key
public JSONStringer key (String name)
Encodes the key (property name) to this stringer.
Parameters |
name |
String : the name of the forthcoming value. May not be null. |
object
public JSONStringer object ()
Begins encoding a new object. Each call to this method must be paired
with a call to endObject()
.
toString
public String 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 Object.toString
, this method returns null if the stringer
contains no data.
Returns |
String |
a string representation of the object. |
value
public JSONStringer value (long value)
Encodes value
to this stringer.
value
public JSONStringer value (double value)
Encodes value
to this stringer.
Parameters |
value |
double : a finite value. May not be NaNs or
infinities . |
value
public JSONStringer value (boolean value)
Encodes value
to this stringer.