Spiffy UI Framework

org.spiffyui.client
Class JSONUtil

java.lang.Object
  extended by org.spiffyui.client.JSONUtil

public final class JSONUtil
extends java.lang.Object

A set of static JavaScript utilities for handling JSON data structures.


Method Summary
static boolean containsKeyIgnoreCase(JSONObject obj, java.lang.String key)
          Determines if the specified JSONObject contains the specified key.
static boolean getBooleanValue(JSONObject obj, java.lang.String key)
          Get a boolean from the JSON object false if it doesn't exist or isn't a boolean
static boolean getBooleanValue(JSONObject obj, java.lang.String key, boolean defaultValue)
          Get a boolean from the JSON object or defaultValue if it doesn't exist or isn't a boolean
static java.util.Date getDateValue(JSONObject obj, java.lang.String key)
          Get a java.util.Date from the JSON object as an epoch time in milliseconds or return null if it doesn't exist or cannot be converted from epoch to Date
static double getDoubleValue(JSONObject obj, java.lang.String key)
          Get a double from the JSON object or 0 if it doesn't exist or isn't a double.
static double getDoubleValue(JSONObject obj, java.lang.String key, double defaultValue)
          Get a double from the JSON object or the defaultValue if it doesn't exist or isn't a double.
static JSONValue getIgnoreCase(JSONObject obj, java.lang.String key)
          Get the JSONValue from the specified object ignoring the case of the key.
static int getIntValue(JSONObject obj, java.lang.String key)
          Get an int from the JSON object or -1 if it doesn't exist or isn't an int.
static int getIntValue(JSONObject obj, java.lang.String key, int defaultValue)
          Get an int from the JSON object or the defaultValue if it doesn't exist or isn't an int.
static JSONArray getJSONArray(JSONObject obj, java.lang.String key)
          Get a JSONArray from the JSON object or null if it doesn't exist or isn't a JSONArray
static JSONObject getJSONObject(JSONObject obj, java.lang.String key)
          Get a JSONObject from the JSON object or null if it doesn't exist or isn't a JSONObject
static long getLongValue(JSONObject obj, java.lang.String key)
          Get a long from the JSON object or -1 if it doesn't exist or isn't a long.
static long getLongValue(JSONObject obj, java.lang.String key, long defaultValue)
          Get a long from the JSON object or the defaultValue if it doesn't exist or isn't a long.
static RESTException getRESTException(JSONValue val, int statusCode, java.lang.String url)
          Get the RESTException containing the information in the specified JSON or null if the JSON object does not correspond to the NCAC exception format.
static java.lang.String getStringValue(JSONObject obj, java.lang.String key)
          Get a string from the JSON object or null if it doesn't exist or isn't a string
static java.lang.String getStringValue(JSONObject obj, java.lang.String key, java.lang.String defaultValue)
          Get a string from the JSON object or defaultValue if it doesn't exist or isn't a string
static java.lang.String getStringValueIgnoreCase(JSONObject obj, java.lang.String key)
          Get a string from the JSON object or null if it doesn't exist or isn't a string
static java.lang.String getStringValueIgnoreCase(JSONObject obj, java.lang.String key, java.lang.String defaultValue)
          Get a string from the JSON object or defaultValue if it doesn't exist or isn't a string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getStringValue

public static java.lang.String getStringValue(JSONObject obj,
                                              java.lang.String key)
Get a string from the JSON object or null if it doesn't exist or isn't a string

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or null if it could not be decoded

getStringValue

public static java.lang.String getStringValue(JSONObject obj,
                                              java.lang.String key,
                                              java.lang.String defaultValue)
Get a string from the JSON object or defaultValue if it doesn't exist or isn't a string

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value to return if the key could not be found
Returns:
the value or the defaultValue if it could not be decoded

getStringValueIgnoreCase

public static java.lang.String getStringValueIgnoreCase(JSONObject obj,
                                                        java.lang.String key)
Get a string from the JSON object or null if it doesn't exist or isn't a string

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or null it could not be decoded

getStringValueIgnoreCase

public static java.lang.String getStringValueIgnoreCase(JSONObject obj,
                                                        java.lang.String key,
                                                        java.lang.String defaultValue)
Get a string from the JSON object or defaultValue if it doesn't exist or isn't a string

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value to return if the key could not be found
Returns:
the value or the defaultValue if it could not be decoded

getBooleanValue

public static boolean getBooleanValue(JSONObject obj,
                                      java.lang.String key)
Get a boolean from the JSON object false if it doesn't exist or isn't a boolean

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or false if it could not be decoded

getBooleanValue

public static boolean getBooleanValue(JSONObject obj,
                                      java.lang.String key,
                                      boolean defaultValue)
Get a boolean from the JSON object or defaultValue if it doesn't exist or isn't a boolean

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value if the key can not be found
Returns:
the value or false it could not be decoded

getJSONArray

public static JSONArray getJSONArray(JSONObject obj,
                                     java.lang.String key)
Get a JSONArray from the JSON object or null if it doesn't exist or isn't a JSONArray

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or null it could not be decoded

getIntValue

public static int getIntValue(JSONObject obj,
                              java.lang.String key)
Get an int from the JSON object or -1 if it doesn't exist or isn't an int. This will handle JSON numbers like this: "val": 5 It will also handle numbers in strings like: "val": "5"

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or -1 if it could not be decoded

getIntValue

public static int getIntValue(JSONObject obj,
                              java.lang.String key,
                              int defaultValue)
Get an int from the JSON object or the defaultValue if it doesn't exist or isn't an int. This will handle JSON numbers like this: "val": 5 It will also handle numbers in strings like: "val": "5"

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value if the specified key isn't found.
Returns:
the value or the defaultValue if it could not be decoded

getDoubleValue

public static double getDoubleValue(JSONObject obj,
                                    java.lang.String key)
Get a double from the JSON object or 0 if it doesn't exist or isn't a double. This will handle JSON numbers like this: "val": 0.5 or "val": -0.5 or "val": +0.5 It will also handle numbers in strings like: "val": "0.5" or "val": "-0.5" or "val": "+0.5"

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or 0 if it could not be decoded

getDoubleValue

public static double getDoubleValue(JSONObject obj,
                                    java.lang.String key,
                                    double defaultValue)
Get a double from the JSON object or the defaultValue if it doesn't exist or isn't a double. This will handle JSON numbers like this: "val": 0.5 or "val": -0.5 or "val": +0.5 It will also handle numbers in strings like: "val": "0.5" or "val": "-0.5" or "val": "+0.5"

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value if the specified key isn't found.
Returns:
the value or the defaultValue if it could not be decoded

getLongValue

public static long getLongValue(JSONObject obj,
                                java.lang.String key)
Get a long from the JSON object or -1 if it doesn't exist or isn't a long. This will handle JSON numbers like this: "val": 5 It will also handle numbers in strings like: "val": "5"

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or -1 if it could not be decoded

getLongValue

public static long getLongValue(JSONObject obj,
                                java.lang.String key,
                                long defaultValue)
Get a long from the JSON object or the defaultValue if it doesn't exist or isn't a long. This will handle JSON numbers like this: "val": 5 It will also handle numbers in strings like: "val": "5"

Parameters:
obj - the object with the value
key - the key for the object
defaultValue - the default value if the specified key isn't found.
Returns:
the value or the defaultValue if it could not be decoded

getJSONObject

public static JSONObject getJSONObject(JSONObject obj,
                                       java.lang.String key)
Get a JSONObject from the JSON object or null if it doesn't exist or isn't a JSONObject

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or null it could not be decoded

getDateValue

public static java.util.Date getDateValue(JSONObject obj,
                                          java.lang.String key)
Get a java.util.Date from the JSON object as an epoch time in milliseconds or return null if it doesn't exist or cannot be converted from epoch to Date

Parameters:
obj - the object with the value
key - the key for the object
Returns:
the value or null it could not be decoded/converted

containsKeyIgnoreCase

public static boolean containsKeyIgnoreCase(JSONObject obj,
                                            java.lang.String key)
Determines if the specified JSONObject contains the specified key.

Parameters:
obj - the object containing the key
key - the key to look for
Returns:
true if this object contains the specified key and false otherwise

getIgnoreCase

public static JSONValue getIgnoreCase(JSONObject obj,
                                      java.lang.String key)
Get the JSONValue from the specified object ignoring the case of the key.

Parameters:
obj - the object containing the value
key - the key of the value
Returns:
the value for the specified key or null if the key can't be found

getRESTException

public static RESTException getRESTException(JSONValue val,
                                             int statusCode,
                                             java.lang.String url)
Get the RESTException containing the information in the specified JSON or null if the JSON object does not correspond to the NCAC exception format.

Parameters:
val - the JSON value containing the exception
statusCode - the status code of the response that generated this data
url - the URL that was called to get this JSON
Returns:
the RESTException if this JSON represented an NCAC fault or null if it wasn't an NCAC fault

Spiffy UI Framework