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:
Serializable, Iterable
@DefaultKey("json")
@InvalidScope("session")
public class JsonTool
extends ImportSupport
implements Iterable, 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:
-
Nested Class Summary
Nested classes/interfaces inherited from class ImportSupport
ImportSupport.SafeClosingHttpURLConnectionReader -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ImportSupportImportSupport utility which provides underlying i/oFields inherited from class ImportSupport
RESOURCE_KEY, URL_KEY, VALID_SCHEME_CHARSFields inherited from class SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.Reads and parses a remote or local URLget(int index) Get nth element from root json array.Get a property from root objectprotected voidinitializeImportSupport(ValueParser config) ImportSupport initializationprotected voidInitialize JSON content from a reader.protected voidInitialize JSON content from a string.iterator()Get an iterator.keys()Iterate keys of root object.keySet()Get set of root object keys.Reads and parses a local JSON resource fileroot()Get JSON root object.intsize()Get size of root object or array.toString()Convert JSON object or array into stringMethods inherited from class ImportSupport
acquireLocalURLReader, acquireLocalURLString, acquireReader, acquireRemoteURLReader, acquireRemoteURLString, acquireString, getClasspathResource, getContentTypeAttribute, getFileResource, getProtocol, getResourceReader, getResourceString, isRemoteURL, setSafeModeMethods inherited from class SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfigMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Field Details
-
importSupport
ImportSupport utility which provides underlying i/o
-
-
Constructor Details
-
JsonTool
public JsonTool()
-
-
Method Details
-
initializeImportSupport
ImportSupport initialization- Parameters:
config- configuration values
-
configure
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
Initialize JSON content from a string.- Parameters:
json- JSON string
-
initJSON
Initialize JSON content from a reader.- Parameters:
reader- JSON stream reader
-
parse
-
read
-
fetch
-
root
-
get
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
-
keys
-
keySet
-
iterator
-
size
public int size()Get size of root object or array.- Returns:
- size
-
toString
-