Package weka.core.json
Class JSONNode
- java.lang.Object
-
- javax.swing.tree.DefaultMutableTreeNode
-
- 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.DefaultMutableTreeNodeContainer 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 classJSONNode.NodeTypeThe type of a node.
-
Constructor Summary
Constructors Constructor Description 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description JSONNodeaddArray(java.lang.String name)Adds an array child to the object.JSONNodeaddArrayElement(java.lang.Object value)Adds an array element child to the array.JSONNodeaddNull(java.lang.String name)Adds a "null" child to the object.JSONNodeaddNullArrayElement()Adds a null array element child to the array.JSONNodeaddObject(java.lang.String name)Adds an object child to the object.JSONNodeaddObjectArrayElement()Add a key-value object child into the arrayJSONNodeaddPrimitive(java.lang.String name, java.lang.Boolean value)Adds a key-value child to the object.JSONNodeaddPrimitive(java.lang.String name, java.lang.Double value)Adds a key-value child to the object.JSONNodeaddPrimitive(java.lang.String name, java.lang.Integer value)Adds a key-value child to the object.JSONNodeaddPrimitive(java.lang.String name, java.lang.String value)Adds a key-value child to the object.JSONNodegetChild(java.lang.String name)Returns the child with the given name.java.lang.StringgetName()Returns the name of the node.JSONNode.NodeTypegetNodeType()Returns the type of the container.java.lang.ObjectgetValue()Returns the stored value.java.lang.ObjectgetValue(java.lang.Object defValue)Returns the stored value.booleanhasChild(java.lang.String name)Checks whether the node has a child with the given name.booleanisAnonymous()Checks whether the node is anonymous.booleanisArray()Returns wether the node is an array.booleanisObject()Returns wether the node is an object.booleanisPrimitive()Returns whether the node stores a primitive value or a an array/object.static voidmain(java.lang.String[] args)Only for testing.static JSONNoderead(java.io.Reader reader)Reads the JSON object from the given reader.java.lang.StringtoString()Returns a string representation of the node.voidtoString(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
-
-
-
-
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 namevalue- the primitive value
-
JSONNode
public JSONNode(java.lang.String name, java.lang.Integer value)Initializes the primitive container.- Parameters:
name- the namevalue- the primitive value
-
JSONNode
public JSONNode(java.lang.String name, java.lang.Double value)Initializes the primitive container.- Parameters:
name- the namevalue- the primitive value
-
JSONNode
public JSONNode(java.lang.String name, java.lang.String value)Initializes the primitive container.- Parameters:
name- the namevalue- 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 pairvalue- 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 pairvalue- 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 pairvalue- 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 pairvalue- 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
-
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(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:
toStringin classjavax.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.ExceptionOnly for testing. Generates a simple JSON object and displays it.- Parameters:
args- ignored- Throws:
java.lang.Exception- if something goes wrong
-
-