com.googlecode.fascinator.common
Class JsonSimple

java.lang.Object
  extended by com.googlecode.fascinator.common.JsonSimple
Direct Known Subclasses:
JsonSimpleConfig, Manifest, ManifestNode, SolrDoc, SolrResult

public class JsonSimple
extends Object

Introduction

This class wraps objects and methods of the JSON.simple library.

Purpose

It provides the following functionality:

Author:
Greg Pendlebury

Constructor Summary
JsonSimple()
          Creates an empty JSON object
JsonSimple(File jsonFile)
          Creates a JSON object from the specified file
JsonSimple(InputStream jsonIn)
          Creates a JSON object from the specified input stream
JsonSimple(JsonObject newJsonObject)
          Wrap a JsonObject in this class
JsonSimple(String jsonString)
          Creates a JSON object from the specified string
 
Method Summary
static Map<String,JsonObject> fromJavaMap(Map<String,JsonSimple> from)
           Take all of the JsonSimple objects in the given Map and return a Map having replace them all with their base JsonObjects.
 org.json.simple.JSONArray getArray(Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target JSONArray.
 Boolean getBoolean(Boolean defaultValue, Object... path)
          Retrieve the Boolean value on the given path.
 Integer getInteger(Integer defaultValue, Object... path)
          Retrieve the Integer value on the given path.
 JsonObject getJsonObject()
          Return the JsonObject holding this object's JSON representation
 List<JsonSimple> getJsonSimpleList(Object... path)
           Retrieve a list of JsonSimple objects found on the given path.
 Map<String,JsonSimple> getJsonSimpleMap(Object... path)
           Retrieve a map of JsonSimple objects found on the given path.
 JsonObject getObject(Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target JsonObject.
 Object getPath(Object... path)
          Walk down the JSON nodes specified by the path and retrieve the target.
 String getString(String defaultValue, Object... path)
          Retrieve the String value on the given path.
static List<String> getStringList(org.json.simple.JSONArray json)
          Get a list of strings from the provided JSONArray
static List<String> getStringList(JsonObject json, String field)
          Get a list of strings found on the specified node
 List<String> getStringList(Object... path)
           Retrieve a list of Strings found on the given path.
 List<Object> search(String node)
           Search through the JSON for any nodes (at any depth) matching the requested name and return them.
 void setPropertySubstitution(boolean newFlag)
           Set method for behaviour flag on substituting system properties during string retrieval.
static List<JsonSimple> toJavaList(org.json.simple.JSONArray array)
           Take all of the JsonObjects found in a JSONArray, wrap them in JsonSimple objects, then add to a Java list and return.
static Map<String,JsonSimple> toJavaMap(JsonObject object)
           Take all of the JsonObjects found in a JsonObject, wrap them in JsonSimple objects, then add to a Java Map and return.
 String toString()
          Return the String representation of this object's JSON
 String toString(boolean pretty)
          Return the String representation of this object's JSON, optionally formatted for human readability.
 org.json.simple.JSONArray writeArray(Object... path)
           Walk down the JSON nodes specified by the path and retrieve the target array, writing each node that doesn't exist along the way.
 JsonObject writeObject(Object... path)
           Walk down the JSON nodes specified by the path and retrieve the target object, writing each node that doesn't exist along the way.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonSimple

public JsonSimple()
Creates an empty JSON object

Throws:
IOException - if there was an error during creation

JsonSimple

public JsonSimple(File jsonFile)
           throws IOException
Creates a JSON object from the specified file

Parameters:
jsonFile - a JSON file
Throws:
IOException - if there was an error parsing or reading the file

JsonSimple

public JsonSimple(InputStream jsonIn)
           throws IOException
Creates a JSON object from the specified input stream

Parameters:
jsonIn - a JSON stream
Throws:
IOException - if there was an error parsing or reading the stream

JsonSimple

public JsonSimple(String jsonString)
           throws IOException
Creates a JSON object from the specified string

Parameters:
jsonIn - a JSON string
Throws:
IOException - if there was an error parsing the string

JsonSimple

public JsonSimple(JsonObject newJsonObject)
Wrap a JsonObject in this class

Parameters:
newJsonObject - : The JsonObject to wrap
Method Detail

setPropertySubstitution

public void setPropertySubstitution(boolean newFlag)

Set method for behaviour flag on substituting system properties during string retrieval.

If set to true (default) strings of the form "{$system.property}" will be substituted for matching system properties during retrieval.

Parameters:
newFlag - : The new flag value to set for this behaviour

getJsonObject

public JsonObject getJsonObject()
Return the JsonObject holding this object's JSON representation

Returns:
JsonObject : The JSON representation

getArray

public org.json.simple.JSONArray getArray(Object... path)
Walk down the JSON nodes specified by the path and retrieve the target JSONArray.

Parameters:
path - : Variable length array of path segments
Returns:
JSONArray : The target node, or NULL if path invalid or not an array

getObject

public JsonObject getObject(Object... path)
Walk down the JSON nodes specified by the path and retrieve the target JsonObject.

Parameters:
path - : Variable length array of path segments
Returns:
JsonObject : The target node, or NULL if path invalid or not an object

getPath

public Object getPath(Object... path)
Walk down the JSON nodes specified by the path and retrieve the target.

Parameters:
path - : Variable length array of path segments
Returns:
Object : The target node, or NULL if invalid

getBoolean

public Boolean getBoolean(Boolean defaultValue,
                          Object... path)
Retrieve the Boolean value on the given path. IMPORTANT: The default value only applies if the path is not found. If a string on the path is found it will be considered false unless the value is 'true' (ignoring case). This is the default behaviour of the Boolean.parseBoolean() method.

Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
Boolean : The Boolean value found on the given path, or null if no default provided

getInteger

public Integer getInteger(Integer defaultValue,
                          Object... path)
Retrieve the Integer value on the given path.

Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
Integer : The Integer value found on the given path, or null if no default provided

getString

public String getString(String defaultValue,
                        Object... path)
Retrieve the String value on the given path.

Parameters:
defaultValue - : The fallback value to use if the path is invalid or not found
path - : An array of indeterminate length to use as the path
Returns:
String : The String value found on the given path, or null if no default provided

getStringList

public List<String> getStringList(Object... path)

Retrieve a list of Strings found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve Strings found on the provided node, and the node must be a JSONArray.

Parameters:
path - : An array of indeterminate length to use as the path
Returns:
List : A list of Strings, null if the node is not found

getJsonSimpleList

public List<JsonSimple> getJsonSimpleList(Object... path)

Retrieve a list of JsonSimple objects found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve valid JsonObjects found on the provided node, and wrap them in JsonSimple objects.

Other objects found on that path will be ignored, and if the path itself is not a JSONArray or not found, the function will return NULL.

Parameters:
path - : An array of indeterminate length to use as the path
Returns:
List : A list of JSONSimple objects, or null

getJsonSimpleMap

public Map<String,JsonSimple> getJsonSimpleMap(Object... path)

Retrieve a map of JsonSimple objects found on the given path. Note that this is a utility function, and not designed for data traversal. It will only retrieve valid JsonObjects found on the provided node, and wrap them in JsonSimple objects.

Other objects found on that path will be ignored, and if the path itself is not a JsonObject or not found, the function will return NULL.

Parameters:
path - : An array of indeterminate length to use as the path
Returns:
Map : A map of JSONSimple objects, or null

search

public List<Object> search(String node)

Search through the JSON for any nodes (at any depth) matching the requested name and return them. The returned List will be of type Object and require type interrogation for detailed use, but will be implemented as a LinkedList to preserve order.

Parameters:
node - : The node name we are looking for
Returns:
List : A list of matching Objects from the data

writeArray

public org.json.simple.JSONArray writeArray(Object... path)

Walk down the JSON nodes specified by the path and retrieve the target array, writing each node that doesn't exist along the way.

Note, when addressing path segments that are array indices you can use '-1' to indicate that the next object should always be created fresh and appended to the array.

Array indices that are standard (ie. not -1) integers will be read as normal, but return a null value from the method if they are not found. This method will not attempt to write to a non-existing index of an array.

Parameters:
path - : Variable length array of path segments
Returns:
JSONArray : The target node, or NULL if path is invalid

writeObject

public JsonObject writeObject(Object... path)

Walk down the JSON nodes specified by the path and retrieve the target object, writing each node that doesn't exist along the way.

Note, when addressing path segments that are array indices you can use '-1' to indicate that the next object should always be created fresh and appended to the array.

Array indices that are standard (ie. not -1) integers will be read as normal, but return a null value from the method if they are not found. This method will not attempt to write to a non-existing index of an array.

Parameters:
path - : Variable length array of path segments
Returns:
Object : The target node, or NULL if path is invalid

toString

public String toString()
Return the String representation of this object's JSON

Overrides:
toString in class Object
Returns:
String : The JSON String

toString

public String toString(boolean pretty)
Return the String representation of this object's JSON, optionally formatted for human readability.

Returns:
String : The JSON String

getStringList

public static List<String> getStringList(JsonObject json,
                                         String field)
Get a list of strings found on the specified node

Parameters:
json - object to retrieve from
field - The field that has the list
Returns:
List The resulting list

getStringList

public static List<String> getStringList(org.json.simple.JSONArray json)
Get a list of strings from the provided JSONArray

Parameters:
json - Array to retrieve strings from
Returns:
List The resulting list

toJavaList

public static List<JsonSimple> toJavaList(org.json.simple.JSONArray array)

Take all of the JsonObjects found in a JSONArray, wrap them in JsonSimple objects, then add to a Java list and return.

All entries found that are not JsonObjects are ignored.

Returns:
String : The JSON String

toJavaMap

public static Map<String,JsonSimple> toJavaMap(JsonObject object)

Take all of the JsonObjects found in a JsonObject, wrap them in JsonSimple objects, then add to a Java Map and return.

All entries found that are not JsonObjects are ignored.

Returns:
String : The JSON String

fromJavaMap

public static Map<String,JsonObject> fromJavaMap(Map<String,JsonSimple> from)

Take all of the JsonSimple objects in the given Map and return a Map having replace them all with their base JsonObjects.

Useful in when combining stacks of JSON objects.

Returns:
String : The JSON String


Copyright © 2009-2012. All Rights Reserved.