Class 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 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:
  • Field Details

    • importSupport

      protected transient ImportSupport importSupport
      ImportSupport utility which provides underlying i/o
  • Constructor Details

    • JsonTool

      public JsonTool()
  • Method Details

    • 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