@DefaultKey(value="json") @InvalidScope(value="session") public class JsonTool extends ImportSupport implements Iterable, Serializable
Tool which can parse a JSON file.
Usage:
Configuration parameters:
resource
=file or classpath resourcesource
=URLExample configuration:
<tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.generic.JsonTool" key="foo" resource="doc.xml"/> </toolbox> </tools>
ImportSupport.SafeClosingHttpURLConnectionReader
Modifier and Type | Field and Description |
---|---|
protected ImportSupport |
importSupport
ImportSupport utility which provides underlying i/o
|
RESOURCE_KEY, URL_KEY, VALID_SCHEME_CHARS
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
Constructor and Description |
---|
JsonTool() |
Modifier and Type | Method and Description |
---|---|
protected void |
configure(ValueParser values)
Looks for the "file" parameter and automatically uses
initJSON(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. |
JsonTool |
fetch(String url)
Reads and parses a remote or local URL
|
Object |
get(int index)
Get nth element from root json array.
|
Object |
get(String key)
Get a property from root object
|
protected void |
initializeImportSupport(ValueParser config)
ImportSupport initialization
|
protected void |
initJSON(Reader reader)
Initialize JSON content from a reader.
|
protected void |
initJSON(String json)
Initialize JSON content from a string.
|
Iterator |
iterator()
Get an iterator.
|
Iterator<String> |
keys()
Iterate keys of root object.
|
Set<String> |
keySet()
Get set of root object keys.
|
JsonTool |
parse(String json)
|
JsonTool |
read(String resource)
Reads and parses a local JSON resource file
|
Object |
root()
Get JSON root object.
|
int |
size()
Get size of root object or array.
|
String |
toString()
Convert JSON object or array into string
|
acquireLocalURLReader, acquireLocalURLString, acquireReader, acquireRemoteURLReader, acquireRemoteURLString, acquireString, getClasspathResource, getContentTypeAttribute, getFileResource, getProtocol, getResourceReader, getResourceString, isRemoteURL, setSafeMode
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
forEach, spliterator
protected transient ImportSupport importSupport
protected void initializeImportSupport(ValueParser config)
config
- configuration valuesprotected void configure(ValueParser values)
initJSON(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.configure
in class ImportSupport
values
- configuration valuesprotected void initJSON(String json)
json
- JSON stringprotected void initJSON(Reader reader)
reader
- JSON stream readerpublic JsonTool read(String resource)
resource
- resource namepublic JsonTool fetch(String url)
url
- resource URLpublic Object root()
public Object get(int index)
index
- npublic Object get(String key)
key
- property keypublic Iterator iterator()
public int size()
Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.