org.apache.velocity.tools.generic

## 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(value="json")
     @InvalidScope(value="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 resource
- `source`=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
`protected ImportSupport` `importSupport` ImportSupport 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 and Description
`JsonTool()`

### Method Summary

All Methods Instance Methods Concrete Methods
`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)` Parses the given JSON string and uses the resulting `Document` as the root `Node`.
`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

### 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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`

### Field Detail

#### importSupport

    protected transient ImportSupport importSupport

ImportSupport utility which provides underlying i/o

### Constructor Detail

#### JsonTool

    public JsonTool()

### 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 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.

Overrides:
  `configure` in class `ImportSupport`
Parameters:
  `values` - configuration values

#### initJSON

    protected void initJSON(String json)

Initialize JSON content from a string.

Parameters:
  `json` - JSON string

#### initJSON

    protected void initJSON(Reader reader)

Initialize JSON content from a reader.

Parameters:
  `reader` - JSON stream reader

#### parse

    public JsonTool parse(String json)

Parses the given JSON string and uses the resulting `Document` as the root `Node`.

Parameters:
  `json` - JSON string
Returns:
  new JsonTool

#### read

    public JsonTool read(String resource)

Reads and parses a local JSON resource file

Parameters:
  `resource` - resource name
Returns:
  new JsonTool

#### fetch

    public JsonTool fetch(String url)

Reads and parses a remote or local URL

Parameters:
  `url` - resource URL
Returns:
  new JSonTool

#### root

    public Object root()

Get JSON root object.

Returns:
  root object or array

#### get

    public 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 Object get(String key)

Get a property from root object

Parameters:
  `key` - property key
Returns:
  property value, or null

#### keys

    public Iterator<String> keys()

Iterate keys of root object.

Returns:
  iterator

#### keySet

    public Set<String> keySet()

Get set of root object keys.

Returns:
  keys set

#### iterator

    public 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:
  `iterator` in interface `Iterable`
Returns:
  iterator

#### size

    public int size()

Get size of root object or array.

Returns:
  size

#### toString

    public String toString()

Convert JSON object or array into string

Overrides:
  `toString` in class `Object`
Returns:
  JSON representation of the root object or array