org.apache.pivot.json
Class JSON

java.lang.Object
  extended by org.apache.pivot.json.JSON

public class JSON
extends Object

Contains utility methods for working with JSON or JSON-like data structures.

Special treatment is afforded to java.util.Map and org.apache.pivot.collections.Map objects at any level of the hierarchy. Otherwise a BeanAdapter is used to fetch the value from the object.

If, however, the object at a given level is a Sequence then the key is assumed to be an integer index into the sequence.

Also, special consideration is given to an object that implements the Dictionary interface.


Constructor Summary
JSON()
           
 
Method Summary
static
<T> boolean
containsKey(Object root, String path)
          Tests the existence of a path in a given object.
static
<T> T
get(Object root, Sequence<String> keys)
          Returns the value at a given path.
static
<T> T
get(Object root, String path)
          Returns the value at a given path.
static byte getByte(Object root, String path)
           
static double getDouble(Object root, String path)
           
static float getFloat(Object root, String path)
           
static int getInt(Object root, String path)
           
static long getLong(Object root, String path)
           
static short getShort(Object root, String path)
           
static Sequence<String> parse(String path)
          Parses a JSON path into a sequence of string keys.
static
<T> T
put(Object root, String path, T value)
          Sets the value at the given path.
static
<T> T
remove(Object root, String path)
          Removes the value at the given path.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSON

public JSON()
Method Detail

get

public static <T> T get(Object root,
                        String path)
Returns the value at a given path.

Parameters:
root - The root object.
path - The path to the value as a JavaScript path.
Returns:
The value at the given path.
See Also:
get(Object, Sequence)

get

public static <T> T get(Object root,
                        Sequence<String> keys)
Returns the value at a given path.

Parameters:
root - The root object.
keys - The path to the value as a sequence of keys.
Returns:
The value at the given path.

getByte

public static byte getByte(Object root,
                           String path)

getShort

public static short getShort(Object root,
                             String path)

getInt

public static int getInt(Object root,
                         String path)

getLong

public static long getLong(Object root,
                           String path)

getFloat

public static float getFloat(Object root,
                             String path)

getDouble

public static double getDouble(Object root,
                               String path)

put

public static <T> T put(Object root,
                        String path,
                        T value)
Sets the value at the given path.

Parameters:
root -
path -
value -
Returns:
The value previously associated with the path.

remove

public static <T> T remove(Object root,
                           String path)
Removes the value at the given path.

Parameters:
root -
path -
Returns:
The value that was removed.

containsKey

public static <T> boolean containsKey(Object root,
                                      String path)
Tests the existence of a path in a given object.

Parameters:
root -
path -
Returns:
true if the path exists; false, otherwise.

parse

public static Sequence<String> parse(String path)
Parses a JSON path into a sequence of string keys.

Parameters:
path -