xv.json
Class JSONObject

java.lang.Object
  extended by xv.json.JSONVariant
      extended by xv.json.JSONObject
All Implemented Interfaces:
Map<String,JSONVariant>

public class JSONObject
extends JSONVariant
implements Map<String,JSONVariant>

A mutable JSON object.

(Note that, at present, you're allowed to insert null keys. A null key is not valid JSON, but at present, I'm too lazy to write all the wrapper code required to catch such a thing. A future version will definitely do that, so don't use null keys. Null values, on the other hand, are permitted.)

Author:
xv

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
JSONObject()
           
 
Method Summary
 void clear()
          
 boolean containsKey(Object key)
          
 boolean containsValue(Object value)
          
 Set<Map.Entry<String,JSONVariant>> entrySet()
          
 boolean equals(Object o)
           
 JSONVariant get(Object key)
          
 JSONArray getArray(String property)
          Gets the given property as an array.
 boolean getBoolean(String property)
           
 boolean getBoolean(String property, boolean defaultValue)
           
 byte getByte(String property)
          Gets the given property as a byte.
 byte getByte(String property, byte defaultValue)
          Gets the given property as a byte.
 double getDouble(String property)
          Gets the given property as a double.
 double getDouble(String property, double defaultValue)
          Gets the given property as a double.
 float getFloat(String property)
          Gets the given property as a float.
 float getFloat(String property, float defaultValue)
          Gets the given property as a float.
 int getInt(String property)
          Gets the given property as an integer.
 int getInt(String property, int defaultValue)
          Gets the given property as an integer.
 long getLong(String property)
          Gets the given property as an integer.
 long getLong(String property, long defaultValue)
          Gets the given property as an integer.
 Number getNumber(String property)
          Gets the given property as a number.
 JSONObject getObject(String property)
          Gets the given property as an object.
 short getShort(String property)
          Gets the given property as a short.
 short getShort(String property, short defaultValue)
          Gets the given property as a short.
 String getString(String property)
          Gets the given property as a string.
 String getType()
          Returns "object".
 int hashCode()
           
 boolean isEmpty()
          
 Set<String> keySet()
          
 JSONVariant put(String key, JSONVariant value)
          
 void putAll(Map<? extends String,? extends JSONVariant> t)
          
 JSONVariant remove(Object key)
          
 JSONArray requireArray(String property)
          Gets the given property as an array.
 boolean requireBoolean(String property)
          Gets the given property as a boolean.
 Number requireNumber(String property)
          Gets the given property as a number.
 JSONObject requireObject(String property)
          Gets the given property as an object.
 String requireString(String property)
          Gets the given property as a string.
 int size()
          
 Collection<JSONVariant> values()
          
 
Methods inherited from class xv.json.JSONVariant
toJSON, write
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONObject

public JSONObject()
Method Detail

clear

public void clear()

Specified by:
clear in interface Map<String,JSONVariant>

containsKey

public boolean containsKey(Object key)

Specified by:
containsKey in interface Map<String,JSONVariant>

containsValue

public boolean containsValue(Object value)

Specified by:
containsValue in interface Map<String,JSONVariant>

entrySet

public Set<Map.Entry<String,JSONVariant>> entrySet()

Specified by:
entrySet in interface Map<String,JSONVariant>

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<String,JSONVariant>
Overrides:
equals in class Object

get

public JSONVariant get(Object key)

Specified by:
get in interface Map<String,JSONVariant>

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<String,JSONVariant>
Overrides:
hashCode in class Object

isEmpty

public boolean isEmpty()

Specified by:
isEmpty in interface Map<String,JSONVariant>

keySet

public Set<String> keySet()

Specified by:
keySet in interface Map<String,JSONVariant>

getObject

public JSONObject getObject(String property)
Gets the given property as an object. If it does not exist or is not an object, this returns null.

Parameters:
property -
Returns:

requireObject

public JSONObject requireObject(String property)
                         throws JSONException
Gets the given property as an object. If it does not exist or is not an object, this throws a JSONException.

Parameters:
property - the property to look up
Returns:
the object value of the property
Throws:
JSONException - if the property does not exist or is not an object

getArray

public JSONArray getArray(String property)
Gets the given property as an array. If it does not exist or is not an array, this returns null.

Parameters:
property -
Returns:

requireArray

public JSONArray requireArray(String property)
                       throws JSONException
Gets the given property as an array. If it does not exist or is not an array, this throws a JSONException.

Parameters:
property - the property to look up
Returns:
the array
Throws:
JSONException - if the property does not exist or is not an array

getNumber

public Number getNumber(String property)
Gets the given property as a number. If it does not exist or is not a number, this returns null.

Parameters:
property -
Returns:

requireNumber

public Number requireNumber(String property)
                     throws JSONException
Gets the given property as a number. If it does not exist or is not a number, this returns null.

Parameters:
property - the property to look up
Returns:
the value of the property as a Number
Throws:
JSONException - if the property does not exist or is not a number

getString

public String getString(String property)
                 throws JSONException
Gets the given property as a string. If it does not exist or is not a string, this returns null.

Parameters:
property -
Returns:
Throws:
JSONException

requireString

public String requireString(String property)
                     throws JSONException
Gets the given property as a string. If it does not exist or is not a string, this throws an exception.

Parameters:
property - the property to look up
Returns:
the string value of the property
Throws:
JSONException - if the property does not exist or is not a string

getBoolean

public boolean getBoolean(String property)

getBoolean

public boolean getBoolean(String property,
                          boolean defaultValue)

requireBoolean

public boolean requireBoolean(String property)
                       throws JSONException
Gets the given property as a boolean. If it does not exist or is not a boolean, this throws an exception.

Parameters:
property - the property to look up
Returns:
the boolean value of the property
Throws:
JSONException - if the property does not exist or is not a boolean

getByte

public byte getByte(String property,
                    byte defaultValue)
Gets the given property as a byte.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getByte

public byte getByte(String property)
Gets the given property as a byte. If the property does not exist or is not a byte, this returns null.

Parameters:
property -
Returns:

getShort

public short getShort(String property,
                      short defaultValue)
Gets the given property as a short.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getShort

public short getShort(String property)
Gets the given property as a short.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getInt

public int getInt(String property,
                  int defaultValue)
Gets the given property as an integer.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getInt

public int getInt(String property)
Gets the given property as an integer.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getLong

public long getLong(String property,
                    long defaultValue)
Gets the given property as an integer.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getLong

public long getLong(String property)
Gets the given property as an integer.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getFloat

public float getFloat(String property,
                      float defaultValue)
Gets the given property as a float.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getFloat

public float getFloat(String property)
Gets the given property as a float. If the property does not exist or is not a number, this returns 0.

Parameters:
property - the property to look up
Returns:
the property value as a float or 0 if it isn't a number

getDouble

public double getDouble(String property,
                        double defaultValue)
Gets the given property as a double.

Parameters:
property -
defaultValue - the value to return if the property is not a number
Returns:

getDouble

public double getDouble(String property)
Gets the given property as a double. If the property does not exist or is not a number, this returns 0.

Parameters:
property - the property to look up
Returns:
the property value as a float or 0 if it isn't a number

put

public JSONVariant put(String key,
                       JSONVariant value)

Specified by:
put in interface Map<String,JSONVariant>

putAll

public void putAll(Map<? extends String,? extends JSONVariant> t)

Specified by:
putAll in interface Map<String,JSONVariant>

remove

public JSONVariant remove(Object key)

Specified by:
remove in interface Map<String,JSONVariant>

size

public int size()

Specified by:
size in interface Map<String,JSONVariant>

values

public Collection<JSONVariant> values()

Specified by:
values in interface Map<String,JSONVariant>

getType

public String getType()
Returns "object".

Specified by:
getType in class JSONVariant
Returns:


Copyright © 2008-2011. All Rights Reserved.