org.apache.wicket.ajax.json
Class JSONStringer

java.lang.Object
  extended by org.apache.wicket.ajax.json.JSONWriter
      extended by org.apache.wicket.ajax.json.JSONStringer

public class JSONStringer
extends JSONWriter

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:

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.


Field Summary
protected  StringBuilder out
          The output data, containing at most one top-level array or object.
 
Fields inherited from class org.apache.wicket.ajax.json.JSONWriter
mode, writer
 
Constructor Summary
JSONStringer()
           
 
Method Summary
 JSONStringer array()
          Begins encoding a new array.
protected  JSONStringer createKey(String name)
          Creates String representation of the key (property name) to this stringer Override this method to provide your own representation of the name.
 JSONStringer endArray()
          Ends encoding the current array.
 JSONStringer endObject()
          Ends encoding the current object.
 JSONStringer entry(Map.Entry<String,Object> entry)
          Encodes key/value pair to this stringer.
 JSONStringer key(String name)
          Encodes the key (property name) to this stringer.
 JSONStringer object()
          Begins encoding a new object.
 String toString()
          Returns the encoded JSON string.
 JSONStringer value(boolean value)
          Encodes value to this stringer.
 JSONStringer value(double value)
          Encodes value to this stringer.
 JSONStringer value(long value)
          Encodes value to this stringer.
 JSONStringer value(Object value)
          Encodes value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

out

protected final StringBuilder out
The output data, containing at most one top-level array or object.

Constructor Detail

JSONStringer

public JSONStringer()
Method Detail

array

public JSONStringer array()
                   throws JSONException
Begins encoding a new array. Each call to this method must be paired with a call to endArray().

Overrides:
array in class JSONWriter
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

endArray

public JSONStringer endArray()
                      throws JSONException
Ends encoding the current array.

Overrides:
endArray in class JSONWriter
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

object

public JSONStringer object()
                    throws JSONException
Begins encoding a new object. Each call to this method must be paired with a call to endObject().

Overrides:
object in class JSONWriter
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

endObject

public JSONStringer endObject()
                       throws JSONException
Ends encoding the current object.

Overrides:
endObject in class JSONWriter
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

value

public JSONStringer value(Object value)
                   throws JSONException
Encodes value.

Overrides:
value in class JSONWriter
Parameters:
value - a JSONObject, JSONArray, String, Boolean, Integer, Long, Double or null. May not be NaNs or infinities.
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

value

public JSONStringer value(boolean value)
                   throws JSONException
Encodes value to this stringer.

Overrides:
value in class JSONWriter
Parameters:
value - The value to encode.
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

value

public JSONStringer value(double value)
                   throws JSONException
Encodes value to this stringer.

Overrides:
value in class JSONWriter
Parameters:
value - a finite value. May not be NaNs or infinities.
Returns:
this stringer.
Throws:
JSONException - On internal errors. Shouldn't happen.

value

public JSONStringer value(long value)
                   throws JSONException
Encodes value to this stringer.

Overrides:
value in class JSONWriter
Parameters:
value - The value to encode.
Returns:
this stringer.
Throws:
JSONException - If we have an internal error. Shouldn't happen.

entry

public JSONStringer entry(Map.Entry<String,Object> entry)
Encodes key/value pair to this stringer.

Parameters:
entry - The entry to encode.
Returns:
this stringer.
Throws:
JSONException - If we have an internal error. Shouldn't happen.

createKey

protected JSONStringer createKey(String name)
Creates String representation of the key (property name) to this stringer Override this method to provide your own representation of the name.

Parameters:
name - the name of the forthcoming value.
Returns:
this stringer.

key

public JSONStringer key(String name)
                 throws JSONException
Encodes the key (property name) to this stringer.

Overrides:
key in class JSONWriter
Parameters:
name - the name of the forthcoming value. May not be null.
Returns:
this stringer.
Throws:
JSONException - on internal errors, shouldn't happen.

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.

Overrides:
toString in class Object


Copyright © 2006–2020 Apache Software Foundation. All rights reserved.