com.googlecode.fascinator.common
Class JsonConfigHelper

java.lang.Object
  extended by com.googlecode.fascinator.common.JsonConfigHelper

public class JsonConfigHelper
extends Object

Helper class for working with JSON configuration. Uses the JXPath library to use XPath syntax to access JSON nodes.

Author:
Oliver Lucido

Constructor Summary
JsonConfigHelper()
          Creates an empty JSON configuration
JsonConfigHelper(File jsonFile)
          Creates a JSON configuration from the specified file
JsonConfigHelper(InputStream jsonIn)
          Creates a JSON configuration from the specified input stream
JsonConfigHelper(Map<String,Object> rootNode)
          Creates a JSON configuration from a map.
JsonConfigHelper(Reader jsonReader)
          Creates a JSON configuration from the specified reader
JsonConfigHelper(String jsonContent)
          Creates a JSON configuration from the specified string
 
Method Summary
 String get(String path)
          Gets the value of the specified node
 String get(String path, String defaultValue)
          Gets the value of the specified node, with a specified default if the not was not found
 List<JsonConfigHelper> getJsonList(String path)
          Get list of JsonConfigHelper of the specified node
 Map<String,JsonConfigHelper> getJsonMap(String path)
          Get the JSON Map of the specified node
 List<Object> getList(String path)
          Gets values of the specified node as a list.
 Map<String,Object> getMap(String path)
          Gets a map of the child nodes of the specified node
 Map<String,Object> getMapWithChild(String path)
          Gets a map of the child (and the 2nd level children) nodes of the specified node
 String getPlainText(String path, String defaultValue)
          Get the value of specified node, with a specified default if it's not found
 void move(String source, String dest)
          Move node from one path to another path in the JSON
 void moveAfter(String path, String refPath)
          Move node to a node after the specified node
 void moveBefore(String path, String refPath)
          Move node to a node before the specified node
 void removePath(String path)
          Remove specified path from json
 void set(String path, String value)
          Sets the value of the specified node.
 void setJsonList(String path, List<JsonConfigHelper> jsonList)
          Set Map on the specified path
 void setJsonMap(String path, Map<String,JsonConfigHelper> map)
          Set Map on the specified path
 void setMap(String path, Map<String,Object> map)
          Set map with its child
 void setMultiMap(String path, Map<String,Object> json)
          Set Multiple nested map on the specified path
 void store(Writer writer)
          Serialises the current state of the JSON configuration to the specified writer.
 void store(Writer writer, boolean pretty)
          Serialises the current state of the JSON configuration to the specified writer.
 String toString()
          Convert Json to String
 String toString(boolean pretty)
          Convert Json to String
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonConfigHelper

public JsonConfigHelper()
Creates an empty JSON configuration


JsonConfigHelper

public JsonConfigHelper(Map<String,Object> rootNode)
Creates a JSON configuration from a map. This is normally used to create an instance for a subNode returned from one of the get methods.

Parameters:
rootNode - a JSON structured map

JsonConfigHelper

public JsonConfigHelper(String jsonContent)
                 throws IOException
Creates a JSON configuration from the specified string

Parameters:
jsonContent - a JSON content string
Throws:
IOException - if there was an error parsing or reading the content

JsonConfigHelper

public JsonConfigHelper(File jsonFile)
                 throws IOException
Creates a JSON configuration from the specified file

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

JsonConfigHelper

public JsonConfigHelper(InputStream jsonIn)
                 throws IOException
Creates a JSON configuration from the specified input stream

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

JsonConfigHelper

public JsonConfigHelper(Reader jsonReader)
                 throws IOException
Creates a JSON configuration from the specified reader

Parameters:
jsonReader - a reader for a JSON file
Throws:
IOException - if there was an error parsing or reading the reader
Method Detail

get

public String get(String path)
Gets the value of the specified node

Parameters:
path - XPath to node
Returns:
node value or null if not found

get

public String get(String path,
                  String defaultValue)
Gets the value of the specified node, with a specified default if the not was not found

Parameters:
path - XPath to node
defaultValue - value to return if the node was not found
Returns:
node value or defaultValue if not found

getPlainText

public String getPlainText(String path,
                           String defaultValue)
Get the value of specified node, with a specified default if it's not found

Parameters:
path -
defaultValue -
Returns:
node value or default Value if not found WITHOUT string substitution

getList

public List<Object> getList(String path)
Gets values of the specified node as a list. Use this method for JSON arrays.

Parameters:
path - XPath to node
Returns:
value list, possibly empty

getMap

public Map<String,Object> getMap(String path)
Gets a map of the child nodes of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

getJsonList

public List<JsonConfigHelper> getJsonList(String path)
Get list of JsonConfigHelper of the specified node

Parameters:
path - XPath to node
Returns:
node list, possibly empty

getJsonMap

public Map<String,JsonConfigHelper> getJsonMap(String path)
Get the JSON Map of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

setMap

public void setMap(String path,
                   Map<String,Object> map)
Set map with its child

Parameters:
path - XPath to node
map - node Map

setMultiMap

public void setMultiMap(String path,
                        Map<String,Object> json)
Set Multiple nested map on the specified path

Parameters:
path - XPath to node
json - node Map

setJsonMap

public void setJsonMap(String path,
                       Map<String,JsonConfigHelper> map)
Set Map on the specified path

Parameters:
path - XPath to node
map - node Map

setJsonList

public void setJsonList(String path,
                        List<JsonConfigHelper> jsonList)
Set Map on the specified path

Parameters:
path - XPath to node
map - node Map

getMapWithChild

public Map<String,Object> getMapWithChild(String path)
Gets a map of the child (and the 2nd level children) nodes of the specified node

Parameters:
path - XPath to node
Returns:
node map, possibly empty

removePath

public void removePath(String path)
Remove specified path from json

Parameters:
path - to node

set

public void set(String path,
                String value)
Sets the value of the specified node. If the node doesn't exist it is created.

Parameters:
path - XPath to node
value - value to set

move

public void move(String source,
                 String dest)
Move node from one path to another path in the JSON

Parameters:
source - XPath to node
dest - XPath to node

moveBefore

public void moveBefore(String path,
                       String refPath)
Move node to a node before the specified node

Parameters:
path - XPath to node
refPath - XPath to node

moveAfter

public void moveAfter(String path,
                      String refPath)
Move node to a node after the specified node

Parameters:
path - XPath to node
refPath - XPath to node

store

public void store(Writer writer)
           throws IOException
Serialises the current state of the JSON configuration to the specified writer. By default this doesn't use a pretty printer.

Parameters:
writer - a writer
Throws:
IOException - if there was an error writing the configuration

store

public void store(Writer writer,
                  boolean pretty)
           throws IOException
Serialises the current state of the JSON configuration to the specified writer. The output can be set to be pretty printed if required.

Parameters:
writer - a writer
pretty - use pretty printer
Throws:
IOException - if there was an error writing the configuration

toString

public String toString()
Convert Json to String

Overrides:
toString in class Object
Returns:
Json configuration in String

toString

public String toString(boolean pretty)
Convert Json to String

Parameters:
pretty - state to format the layout of the Json configuration file
Returns:
Json configuration in String


Copyright © 2009-2013. All Rights Reserved.