Class JSONNode

All Implemented Interfaces:
Serializable, Cloneable, MutableTreeNode, TreeNode

public class JSONNode extends DefaultMutableTreeNode
Container class for storing a JSON data structure.
Version:
$Revision: 12111 $
Author:
FracPete (fracpete at waikato dot ac dot nz)
See Also:
  • Constructor Details

    • JSONNode

      public JSONNode()
      Initializes the root container.
    • JSONNode

      public JSONNode(String name, Boolean value)
      Initializes the primitive container.
      Parameters:
      name - the name
      value - the primitive value
    • JSONNode

      public JSONNode(String name, Integer value)
      Initializes the primitive container.
      Parameters:
      name - the name
      value - the primitive value
    • JSONNode

      public JSONNode(String name, Double value)
      Initializes the primitive container.
      Parameters:
      name - the name
      value - the primitive value
    • JSONNode

      public JSONNode(String name, String value)
      Initializes the primitive container.
      Parameters:
      name - the name
      value - the primitive value
  • Method Details

    • isAnonymous

      public boolean isAnonymous()
      Checks whether the node is anonymous.
      Returns:
      true if no name available
    • getName

      public String getName()
      Returns the name of the node.
      Returns:
      the name, null for anonymous nodes
    • getValue

      public Object getValue()
      Returns the stored value.
      Returns:
      the stored value, can be null
    • getValue

      public Object getValue(Object defValue)
      Returns the stored value.
      Parameters:
      defValue - the default value, if value is null
      Returns:
      the stored value, can be null
    • isPrimitive

      public boolean isPrimitive()
      Returns whether the node stores a primitive value or a an array/object.
      Returns:
      true if a primitive, false in case of an array/object
    • isArray

      public boolean isArray()
      Returns wether the node is an array.
      Returns:
      true if the node is array container
    • isObject

      public boolean isObject()
      Returns wether the node is an object.
      Returns:
      true if the node is object container
    • getNodeType

      public JSONNode.NodeType getNodeType()
      Returns the type of the container.
      Returns:
      the type
    • addNull

      public JSONNode addNull(String name)
      Adds a "null" child to the object.
      Parameters:
      name - the name of the null value
      Returns:
      the new node, or null if none added
    • addPrimitive

      public JSONNode addPrimitive(String name, Boolean value)
      Adds a key-value child to the object.
      Parameters:
      name - the name of the pair
      value - the value
      Returns:
      the new node, or null if none added
    • addPrimitive

      public JSONNode addPrimitive(String name, Integer value)
      Adds a key-value child to the object.
      Parameters:
      name - the name of the pair
      value - the value
      Returns:
      the new node, or null if none added
    • addPrimitive

      public JSONNode addPrimitive(String name, Double value)
      Adds a key-value child to the object.
      Parameters:
      name - the name of the pair
      value - the value
      Returns:
      the new node, or null if none added
    • addPrimitive

      public JSONNode addPrimitive(String name, String value)
      Adds a key-value child to the object.
      Parameters:
      name - the name of the pair
      value - the value
      Returns:
      the new node, or null if none added
    • addArray

      public JSONNode addArray(String name)
      Adds an array child to the object.
      Parameters:
      name - the name of the pair
      Returns:
      the new node, or null if none added
    • addNullArrayElement

      public JSONNode addNullArrayElement()
      Adds a null array element child to the array.
      Returns:
      the new node, or null if none added
    • addObjectArrayElement

      public JSONNode addObjectArrayElement()
      Add a key-value object child into the array
      Returns:
      the newly added node
    • addArrayElement

      public JSONNode addArrayElement(Object value)
      Adds an array element child to the array.
      Parameters:
      value - the value of the element array
      Returns:
      the new node, or null if none added
    • addObject

      public JSONNode addObject(String name)
      Adds an object child to the object.
      Parameters:
      name - the name of the pair
      Returns:
      the new node, or null if none added
    • hasChild

      public boolean hasChild(String name)
      Checks whether the node has a child with the given name.
      Parameters:
      name - the name of the child
      Returns:
      true if child with that name is available
    • getChild

      public JSONNode getChild(String name)
      Returns the child with the given name.
      Parameters:
      name - the name of the child
      Returns:
      the child if available, null otherwise
    • toString

      public void toString(StringBuffer buffer)
      Dumps the node structure into JSON format.
      Parameters:
      buffer - the buffer to add the data to
    • toString

      public String toString()
      Returns a string representation of the node.
      Overrides:
      toString in class DefaultMutableTreeNode
      Returns:
      the string representation
    • read

      public static JSONNode read(Reader reader) throws Exception
      Reads the JSON object from the given reader.
      Parameters:
      reader - the reader to read the JSON object from
      Returns:
      the generated JSON object
      Throws:
      Exception - if parsing fails
    • main

      public static void main(String[] args) throws Exception
      Only for testing. Generates a simple JSON object and displays it.
      Parameters:
      args - ignored
      Throws:
      Exception - if something goes wrong