weka.core.json
Class JSONNode

java.lang.Object
  extended by javax.swing.tree.DefaultMutableTreeNode
      extended by weka.core.json.JSONNode
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, javax.swing.tree.MutableTreeNode, javax.swing.tree.TreeNode

public class JSONNode
extends javax.swing.tree.DefaultMutableTreeNode

Container class for storing a JSON data structure.

Version:
$Revision: 5786 $
Author:
FracPete (fracpete at waikato dot ac dot nz)
See Also:
Serialized Form

Nested Class Summary
static class JSONNode.NodeType
          The type of a node.
 
Field Summary
 
Fields inherited from class javax.swing.tree.DefaultMutableTreeNode
EMPTY_ENUMERATION
 
Constructor Summary
JSONNode()
          Initializes the root container.
JSONNode(java.lang.String name, java.lang.Boolean value)
          Initializes the primitive container.
JSONNode(java.lang.String name, java.lang.Double value)
          Initializes the primitive container.
JSONNode(java.lang.String name, java.lang.Integer value)
          Initializes the primitive container.
JSONNode(java.lang.String name, java.lang.String value)
          Initializes the primitive container.
 
Method Summary
 JSONNode addArray(java.lang.String name)
          Adds an array child to the object.
 JSONNode addArrayElement(java.lang.Object value)
          Adds an array element child to the array.
 JSONNode addNull(java.lang.String name)
          Adds a "null" child to the object.
 JSONNode addObject(java.lang.String name)
          Adds an object child to the object.
 JSONNode addPrimitive(java.lang.String name, java.lang.Boolean value)
          Adds a key-value child to the object.
 JSONNode addPrimitive(java.lang.String name, java.lang.Double value)
          Adds a key-value child to the object.
 JSONNode addPrimitive(java.lang.String name, java.lang.Integer value)
          Adds a key-value child to the object.
 JSONNode addPrimitive(java.lang.String name, java.lang.String value)
          Adds a key-value child to the object.
 JSONNode getChild(java.lang.String name)
          Returns the child with the given name.
 java.lang.String getName()
          Returns the name of the node.
 JSONNode.NodeType getNodeType()
          Returns the type of the container.
 java.lang.Object getValue()
          Returns the stored value.
 java.lang.Object getValue(java.lang.Object defValue)
          Returns the stored value.
 boolean hasChild(java.lang.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(java.lang.String[] args)
          Only for testing.
static JSONNode read(java.io.Reader reader)
          Reads the JSON object from the given reader.
 java.lang.String toString()
          Returns a string representation of the node.
 void toString(java.lang.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 Detail

JSONNode

public JSONNode()
Initializes the root container.


JSONNode

public JSONNode(java.lang.String name,
                java.lang.Boolean value)
Initializes the primitive container.

Parameters:
name - the name
value - the primitive value

JSONNode

public JSONNode(java.lang.String name,
                java.lang.Integer value)
Initializes the primitive container.

Parameters:
name - the name
value - the primitive value

JSONNode

public JSONNode(java.lang.String name,
                java.lang.Double value)
Initializes the primitive container.

Parameters:
name - the name
value - the primitive value

JSONNode

public JSONNode(java.lang.String name,
                java.lang.String value)
Initializes the primitive container.

Parameters:
name - the name
value - the primitive value
Method Detail

isAnonymous

public boolean isAnonymous()
Checks whether the node is anonymous.

Returns:
true if no name available

getName

public java.lang.String getName()
Returns the name of the node.

Returns:
the name, null for anonymous nodes

getValue

public java.lang.Object getValue()
Returns the stored value.

Returns:
the stored value, can be null

getValue

public java.lang.Object getValue(java.lang.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(java.lang.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(java.lang.String name,
                             java.lang.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(java.lang.String name,
                             java.lang.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(java.lang.String name,
                             java.lang.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(java.lang.String name,
                             java.lang.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(java.lang.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

addArrayElement

public JSONNode addArrayElement(java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.StringBuffer buffer)
Dumps the node structure into JSON format.

Parameters:
buffer - the buffer to add the data to

toString

public java.lang.String toString()
Returns a string representation of the node.

Overrides:
toString in class javax.swing.tree.DefaultMutableTreeNode
Returns:
the string representation

read

public static JSONNode read(java.io.Reader reader)
                     throws java.lang.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:
java.lang.Exception - if parsing fails

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Only for testing. Generates a simple JSON object and displays it.

Parameters:
args - ignored
Throws:
java.lang.Exception - if something goes wrong