Package fmpp.models

Class JSONNode

java.lang.Object
fmpp.models.JSONNode
All Implemented Interfaces:
freemarker.template.AdapterTemplateModel, freemarker.template.TemplateModel, freemarker.template.TemplateNodeModel, Serializable
Direct Known Subclasses:
JSONArrayNode, JSONBooleanNode, JSONNullNode, JSONNumberNode, JSONObjectNode, JSONStringNode

public abstract class JSONNode extends Object implements freemarker.template.TemplateNodeModel, freemarker.template.AdapterTemplateModel, Serializable
Node in a hierarchy of JSON values. See http://www.json.org/ for JSON types; each has its own subclass. JSON "object" and "array" values are the non-leafs in the tree.
See Also:
  • Field Summary

    Fields inherited from interface freemarker.template.TemplateModel

    NOTHING
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    JSONNode(JSONNode parentNode, String nodeName)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract String
    Returns the name of the node if it has no explicit name.
    final String
    Returns the same as TemplateNodeModel.getNodeType(), except when the node is the value in a key-value pair in a JSON object, in which case it returns the key value.
    final String
     
    final freemarker.template.TemplateNodeModel
    Returns the JSON "object" or JSON "array" that contains this value.
    protected static String
    Returns the FTL node name for a node that has otherwise no name.
    static JSONNode
    wrap(Object jsonPOJO)
    Wraps a List, a Map with string keys, a String, a Number or a Boolean into a JSONNode.
    protected static JSONNode
    wrap(Object obj, JSONNode parentNode, String nodeName, boolean wrapNullAsJSONNullNode)
     

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface freemarker.template.AdapterTemplateModel

    getAdaptedObject

    Methods inherited from interface freemarker.template.TemplateNodeModel

    getChildNodes, getNodeType
  • Constructor Details

    • JSONNode

      protected JSONNode(JSONNode parentNode, String nodeName)
      Parameters:
      parentNode - the JSON "object" or JSON "array" that contains this value.
      nodeName - null, unless this is the value in a key-value pair, in which case it's the key. When it's null, the actual node name will be getDefaultNodeName().
  • Method Details

    • nodeTypeToDefaultNodeName

      protected static String nodeTypeToDefaultNodeName(String nodeType)
      Returns the FTL node name for a node that has otherwise no name.
    • getDefaultNodeName

      protected abstract String getDefaultNodeName()
      Returns the name of the node if it has no explicit name. This is normally called by the JSONNode(JSONNode, String) constructor if its second argument is null.
      See Also:
    • getParentNode

      public final freemarker.template.TemplateNodeModel getParentNode() throws freemarker.template.TemplateModelException
      Returns the JSON "object" or JSON "array" that contains this value.
      Specified by:
      getParentNode in interface freemarker.template.TemplateNodeModel
      Throws:
      freemarker.template.TemplateModelException
    • getNodeName

      public final String getNodeName() throws freemarker.template.TemplateModelException
      Returns the same as TemplateNodeModel.getNodeType(), except when the node is the value in a key-value pair in a JSON object, in which case it returns the key value.
      Specified by:
      getNodeName in interface freemarker.template.TemplateNodeModel
      Throws:
      freemarker.template.TemplateModelException
    • getNodeNamespace

      public final String getNodeNamespace() throws freemarker.template.TemplateModelException
      Specified by:
      getNodeNamespace in interface freemarker.template.TemplateNodeModel
      Throws:
      freemarker.template.TemplateModelException
    • wrap

      public static JSONNode wrap(Object jsonPOJO) throws freemarker.template.TemplateModelException
      Wraps a List, a Map with string keys, a String, a Number or a Boolean into a JSONNode. The values in the List or Map must be also be one of the previously listed types. The resulting object is NOT thread safe. Also, the wrapped objects shouldn't be changed after the wrapping. The wrapping of the contained values is possibly lazy.
      Returns:
      The wrapped value; note the this will return null for JSON null values, not a JSONNullNode instance.
      Throws:
      freemarker.template.TemplateModelException - If obj can't be wrapped into JSON node.
    • wrap

      protected static JSONNode wrap(Object obj, JSONNode parentNode, String nodeName, boolean wrapNullAsJSONNullNode) throws freemarker.template.TemplateModelException
      Parameters:
      parentNode - Same as the similar parameter of JSONNode(JSONNode, String).
      nodeName - Same as the similar parameter of JSONNode(JSONNode, String).
      Throws:
      freemarker.template.TemplateModelException - If obj can't be wrapped into JSON node.