Package weka.core.json

# Class JSONNode

java.lang.Object
javax.swing.tree.DefaultMutableTreeNode
weka.core.json.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:
  - Serialized Form

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static enum`
  `JSONNode.NodeType`
  The type of a node.

- ## Field Summary

  ### Fields inherited from class javax.swing.tree.DefaultMutableTreeNode

  `EMPTY_ENUMERATION`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JSONNode()`
  Initializes the root container.
  `JSONNode(String name, Boolean value)`
  Initializes the primitive container.
  `JSONNode(String name, Double value)`
  Initializes the primitive container.
  `JSONNode(String name, Integer value)`
  Initializes the primitive container.
  `JSONNode(String name, String value)`
  Initializes the primitive container.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `JSONNode`
  `addArray(String name)`
  Adds an array child to the object.
  `JSONNode`
  `addArrayElement(Object value)`
  Adds an array element child to the array.
  `JSONNode`
  `addNull(String name)`
  Adds a "null" child to the object.
  `JSONNode`
  `addNullArrayElement()`
  Adds a null array element child to the array.
  `JSONNode`
  `addObject(String name)`
  Adds an object child to the object.
  `JSONNode`
  `addObjectArrayElement()`
  Add a key-value object child into the array
  `JSONNode`
  `addPrimitive(String name, Boolean value)`
  Adds a key-value child to the object.
  `JSONNode`
  `addPrimitive(String name, Double value)`
  Adds a key-value child to the object.
  `JSONNode`
  `addPrimitive(String name, Integer value)`
  Adds a key-value child to the object.
  `JSONNode`
  `addPrimitive(String name, String value)`
  Adds a key-value child to the object.
  `JSONNode`
  `getChild(String name)`
  Returns the child with the given name.
  `String`
  `getName()`
  Returns the name of the node.
  `JSONNode.NodeType`
  `getNodeType()`
  Returns the type of the container.
  `Object`
  `getValue()`
  Returns the stored value.
  `Object`
  `getValue(Object defValue)`
  Returns the stored value.
  `boolean`
  `hasChild(String name)`
  Checks whether the node has a child with the given name.
  `boolean`
  `isAnonymous()`
  Checks whether the node is anonymous.
  `boolean`
  `isArray()`
  Returns wether the node is an array.
  `boolean`
  `isObject()`
  Returns wether the node is an object.
  `boolean`
  `isPrimitive()`
  Returns whether the node stores a primitive value or a an array/object.
  `static void`
  `main(String[] args)`
  Only for testing.
  `static JSONNode`
  `read(Reader reader)`
  Reads the JSON object from the given reader.
  `String`
  `toString()`
  Returns a string representation of the node.
  `void`
  `toString(StringBuffer buffer)`
  Dumps the node structure into JSON format.

  ### Methods inherited from class javax.swing.tree.DefaultMutableTreeNode

  `add, breadthFirstEnumeration, children, clone, depthFirstEnumeration, getAllowsChildren, getChildAfter, getChildAt, getChildBefore, getChildCount, getDepth, getFirstChild, getFirstLeaf, getIndex, getLastChild, getLastLeaf, getLeafCount, getLevel, getNextLeaf, getNextNode, getNextSibling, getParent, getPath, getPreviousLeaf, getPreviousNode, getPreviousSibling, getRoot, getSharedAncestor, getSiblingCount, getUserObject, getUserObjectPath, insert, isLeaf, isNodeAncestor, isNodeChild, isNodeDescendant, isNodeRelated, isNodeSibling, isRoot, pathFromAncestorEnumeration, postorderEnumeration, preorderEnumeration, remove, remove, removeAllChildren, removeFromParent, setAllowsChildren, setParent, setUserObject`

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

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

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