Class JsonTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.ImportSupport
-
- org.apache.velocity.tools.generic.JsonTool
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable
@DefaultKey("json") @InvalidScope("session") public class JsonTool extends ImportSupport implements java.lang.Iterable, java.io.Serializable
Tool which can parse a JSON file.
Usage:
- $json.parse(JSON string)
- $json.read(file or classpath resource)
- $json.fetch(URL)
Configuration parameters:
resource=file or classpath resourcesource=URL
Example configuration:
<tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.generic.JsonTool" key="foo" resource="doc.xml"/> </toolbox> </tools>- Since:
- VelocityTools 3.0
- Version:
- $Id:$
- Author:
- Claude Brisson
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.velocity.tools.generic.ImportSupport
ImportSupport.SafeClosingHttpURLConnectionReader
-
-
Field Summary
Fields Modifier and Type Field Description protected ImportSupportimportSupportImportSupport utility which provides underlying i/o-
Fields inherited from class org.apache.velocity.tools.generic.ImportSupport
RESOURCE_KEY, URL_KEY, VALID_SCHEME_CHARS
-
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
-
Constructor Summary
Constructors Constructor Description JsonTool()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidconfigure(ValueParser values)Looks for the "file" parameter and automatically usesinitJSON(String)to parse the file (searched in filesystem current path and classpath) and set the resulting JSON object as the root node for this instance.JsonToolfetch(java.lang.String url)Reads and parses a remote or local URLjava.lang.Objectget(int index)Get nth element from root json array.java.lang.Objectget(java.lang.String key)Get a property from root objectprotected voidinitializeImportSupport(ValueParser config)ImportSupport initializationprotected voidinitJSON(java.io.Reader reader)Initialize JSON content from a reader.protected voidinitJSON(java.lang.String json)Initialize JSON content from a string.java.util.Iteratoriterator()Get an iterator.java.util.Iterator<java.lang.String>keys()Iterate keys of root object.java.util.Set<java.lang.String>keySet()Get set of root object keys.JsonToolparse(java.lang.String json)Parses the given JSON string and uses the resultingDocumentas the rootNode.JsonToolread(java.lang.String resource)Reads and parses a local JSON resource filejava.lang.Objectroot()Get JSON root object.intsize()Get size of root object or array.java.lang.StringtoString()Convert JSON object or array into string-
Methods inherited from class org.apache.velocity.tools.generic.ImportSupport
acquireLocalURLReader, acquireLocalURLString, acquireReader, acquireRemoteURLReader, acquireRemoteURLString, acquireString, getClasspathResource, getContentTypeAttribute, getFileResource, getProtocol, getResourceReader, getResourceString, isRemoteURL, setSafeMode
-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig
-
-
-
-
Field Detail
-
importSupport
protected transient ImportSupport importSupport
ImportSupport utility which provides underlying i/o
-
-
Method Detail
-
initializeImportSupport
protected void initializeImportSupport(ValueParser config)
ImportSupport initialization- Parameters:
config- configuration values
-
configure
protected void configure(ValueParser values)
Looks for the "file" parameter and automatically usesinitJSON(String)to parse the file (searched in filesystem current path and classpath) and set the resulting JSON object as the root node for this instance.- Overrides:
configurein classImportSupport- Parameters:
values- configuration values
-
initJSON
protected void initJSON(java.lang.String json)
Initialize JSON content from a string.- Parameters:
json- JSON string
-
initJSON
protected void initJSON(java.io.Reader reader)
Initialize JSON content from a reader.- Parameters:
reader- JSON stream reader
-
parse
public JsonTool parse(java.lang.String json)
Parses the given JSON string and uses the resultingDocumentas the rootNode.- Parameters:
json- JSON string- Returns:
- new JsonTool
-
read
public JsonTool read(java.lang.String resource)
Reads and parses a local JSON resource file- Parameters:
resource- resource name- Returns:
- new JsonTool
-
fetch
public JsonTool fetch(java.lang.String url)
Reads and parses a remote or local URL- Parameters:
url- resource URL- Returns:
- new JSonTool
-
root
public java.lang.Object root()
Get JSON root object.- Returns:
- root object or array
-
get
public java.lang.Object get(int index)
Get nth element from root json array.- Parameters:
index- n- Returns:
- nth element, or null if root object is null or not an array
-
get
public java.lang.Object get(java.lang.String key)
Get a property from root object- Parameters:
key- property key- Returns:
- property value, or null
-
keys
public java.util.Iterator<java.lang.String> keys()
Iterate keys of root object.- Returns:
- iterator
-
keySet
public java.util.Set<java.lang.String> keySet()
Get set of root object keys.- Returns:
- keys set
-
iterator
public java.util.Iterator iterator()
Get an iterator. For a root object, returns an iterator over key names. For a root array, returns an iterator over contained objects.- Specified by:
iteratorin interfacejava.lang.Iterable- Returns:
- iterator
-
size
public int size()
Get size of root object or array.- Returns:
- size
-
toString
public java.lang.String toString()
Convert JSON object or array into string- Overrides:
toStringin classjava.lang.Object- Returns:
- JSON representation of the root object or array
-
-