ikrs.json
Class JSONNumber

java.lang.Object
  extended by ikrs.json.AbstractJSONValue
      extended by ikrs.json.JSONNumber
All Implemented Interfaces:
JSONValue

public class JSONNumber
extends AbstractJSONValue

This is the JSON number subclass.


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
JSONNumber(java.lang.Number number)
          Create a new JSON number.
 
Method Summary
 JSONArray asJSONArray()
          This method tries to convert this JSONValue into a JSONArray.
 JSONNumber asJSONNumber()
          This method tries to convert this JSONValue into a JSONNumber.
 JSONString asJSONString()
          This method tries to convert this JSONValue into a JSONString.
 java.lang.Number getNumber()
          Get the number from this JSON value.
static JSONNumber parseJSONNumber(java.lang.String str)
          This static method is the equivalent to Java's Integer.parseInt(String) respective Double.parseDouble(String).
static java.lang.Number parseNumber(java.lang.String str)
          This static method is the equivalent to Java's Integer.parseInt(String) respective Double.parseDouble(String).
 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, asJSONObject, getArray, getBoolean, getObject, 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

JSONNumber

public JSONNumber(java.lang.Number number)
           throws java.lang.NullPointerException
Create a new JSON number.

Parameters:
number - The numeric value this JSON value should have.
Throws:
java.lang.NullPointerException - If number is null.
Method Detail

getNumber

public java.lang.Number getNumber()
                           throws JSONException
Get the number from this JSON value.

Specified by:
getNumber in interface JSONValue
Overrides:
getNumber in class AbstractJSONValue
Returns:
The number from this JSON value.
Throws:
JSONException - If this value does not represent a number.

asJSONNumber

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

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

asJSONString

public JSONString asJSONString()
                        throws JSONException
This method tries to convert this JSONValue into a JSONString. If that is not possible (because the contained value does not represent a string in any way) the method will throw an JSONException. Note: due to the fact that JSON is usually represented by a string EACH JSON VALUE SHOULD BE CONVERTIBLE TO A STRING.

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

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.

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.

toString

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

parseNumber

public static java.lang.Number parseNumber(java.lang.String str)
                                    throws java.lang.NullPointerException,
                                           java.lang.NumberFormatException
This static method is the equivalent to Java's Integer.parseInt(String) respective Double.parseDouble(String). It expects a String containing a number and converts it into a Java Number object. If the passed string does not represent a number the method throws a NumberFormatException.

Parameters:
str - The string to be parsed.
Returns:
The parsed number value.
Throws:
java.lang.NullPointerException - If the passed string is null.
JSONException - If the passed string does not represent a number.
java.lang.NumberFormatException

parseJSONNumber

public static JSONNumber parseJSONNumber(java.lang.String str)
                                  throws java.lang.NullPointerException,
                                         JSONException
This static method is the equivalent to Java's Integer.parseInt(String) respective Double.parseDouble(String). It expects a String containing a number and converts it into a JSONNumber object. If the passed string does not represent a number the method throws a JSONException (would be NumberFormatException in Java).

Parameters:
str - The string to be parsed.
Returns:
The parsed JSON number value.
Throws:
java.lang.NullPointerException - If the passed string is null.
JSONException - If the passed string does not represent a number.