ikrs.json
Class JSONObject

java.lang.Object
  extended by ikrs.json.AbstractJSONValue
      extended by ikrs.json.JSONObject
All Implemented Interfaces:
JSONValue
Direct Known Subclasses:
DefaultJSONRPCError, DefaultJSONRPCRequest, DefaultJSONRPCResponse

public class JSONObject
extends AbstractJSONValue


Field Summary
 
Fields inherited from interface ikrs.json.JSONValue
FALSE, NULL, TRUE, TYPE_ARRAY, TYPE_BOOLEAN, TYPE_NULL, TYPE_NUMBER, TYPE_OBJECT, TYPE_STRING
 
Constructor Summary
JSONObject()
          Create a new JSON object.
JSONObject(java.util.Map<java.lang.String,JSONValue> object)
          Create a new JSON object.
 
Method Summary
 JSONArray asJSONArray()
          This method tries to convert this JSONValue into a JSONArray.
 JSONObject asJSONObject()
          This method tries to convert this JSONValue into a JSONObject.
protected  java.util.Map<java.lang.String,JSONValue> getMap()
          This method is a copy of getObject(), but with two differences: it is protected (only for subclasses) and does not thrown any exceptions.
 java.util.Map<java.lang.String,JSONValue> getObject()
          Get the object value as a Map from this JSON value.
static void main(java.lang.String[] argv)
          For testing purposes only.
 java.lang.String toString()
           
 void write(java.io.Writer writer)
          This method MUST write a valid JSON value to the passed writer.
 
Methods inherited from class ikrs.json.AbstractJSONValue
asJSONBoolean, asJSONNumber, asJSONString, getArray, getBoolean, getNumber, getString, getTypeName, isArray, isBoolean, isNull, isNumber, isObject, isString, isValidTypeID, toJSONString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JSONObject

public JSONObject()
Create a new JSON object. The constructor creates a new Map (TreeMap).


JSONObject

public JSONObject(java.util.Map<java.lang.String,JSONValue> object)
           throws java.lang.NullPointerException
Create a new JSON object.

Parameters:
object - The object value this JSON value should have.
Throws:
java.lang.NullPointerException
Method Detail

getObject

public java.util.Map<java.lang.String,JSONValue> getObject()
                                                    throws JSONException
Get the object value as a Map from this JSON value.

Specified by:
getObject in interface JSONValue
Overrides:
getObject in class AbstractJSONValue
Returns:
The object from this JSON value. The object is returned as a Map.
Throws:
JSONException - If this value does not represent an object.

asJSONArray

public JSONArray asJSONArray()
                      throws JSONException
This method tries to convert this JSONValue into a JSONArray. If that is not possible (because the contained value does not represent an array in any way) the method will throw an JSONException.

Specified by:
asJSONArray in interface JSONValue
Overrides:
asJSONArray in class AbstractJSONValue
Returns:
This JSON value as a JSON array.
Throws:
JSONException - If this value is not convertible to an array.

asJSONObject

public JSONObject asJSONObject()
                        throws JSONException
This method tries to convert this JSONValue into a JSONObject. If that is not possible (because the contained value does not represent an object in any way) the method will throw an JSONException.

Specified by:
asJSONObject in interface JSONValue
Overrides:
asJSONObject in class AbstractJSONValue
Returns:
This JSON value as a JSON object.
Throws:
JSONException - If this value is not convertible to an object.

write

public void write(java.io.Writer writer)
           throws java.io.IOException
This method MUST write a valid JSON value to the passed writer.

Specified by:
write in interface JSONValue
Specified by:
write in class AbstractJSONValue
Parameters:
writer - The writer to write to.
Throws:
java.io.IOException - If any IO errors occur.

getMap

protected java.util.Map<java.lang.String,JSONValue> getMap()
This method is a copy of getObject(), but with two differences: it is protected (only for subclasses) and does not thrown any exceptions. Note that this method does not create a copy of the internal map, so changes to the map reflect this JSONObject!

Returns:
The internal map, which is never null.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] argv)
For testing purposes only.