Class 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 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
    • 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​(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 resulting Document as the root Node.
        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:
        iterator in interface java.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:
        toString in class java.lang.Object
        Returns:
        JSON representation of the root object or array