Package weka.core.json
Class JSONNode
java.lang.Object
javax.swing.tree.DefaultMutableTreeNode
weka.core.json.JSONNode
- All Implemented Interfaces:
Serializable
,Cloneable
,MutableTreeNode
,TreeNode
Container class for storing a
JSON data structure.
- Version:
- $Revision: 12111 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
-
Nested Class Summary
-
Field Summary
Fields inherited from class javax.swing.tree.DefaultMutableTreeNode
EMPTY_ENUMERATION
-
Constructor Summary
ConstructorDescriptionJSONNode()
Initializes the root container.Initializes the primitive container.Initializes the primitive container.Initializes the primitive container.Initializes the primitive container. -
Method Summary
Modifier and TypeMethodDescriptionAdds an array child to the object.addArrayElement
(Object value) Adds an array element child to the array.Adds a "null" child to the object.Adds a null array element child to the array.Adds an object child to the object.Add a key-value object child into the arrayaddPrimitive
(String name, Boolean value) Adds a key-value child to the object.addPrimitive
(String name, Double value) Adds a key-value child to the object.addPrimitive
(String name, Integer value) Adds a key-value child to the object.addPrimitive
(String name, String value) Adds a key-value child to the object.Returns the child with the given name.getName()
Returns the name of the node.Returns the type of the container.getValue()
Returns the stored value.Returns the stored value.boolean
Checks whether the node has a child with the given name.boolean
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
Returns whether the node stores a primitive value or a an array/object.static void
Only for testing.static JSONNode
Reads the JSON object from the given reader.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
-
Constructor Details
-
JSONNode
public JSONNode()Initializes the root container. -
JSONNode
Initializes the primitive container.- Parameters:
name
- the namevalue
- the primitive value
-
JSONNode
Initializes the primitive container.- Parameters:
name
- the namevalue
- the primitive value
-
JSONNode
Initializes the primitive container.- Parameters:
name
- the namevalue
- the primitive value
-
JSONNode
Initializes the primitive container.- Parameters:
name
- the namevalue
- the primitive value
-
-
Method Details
-
isAnonymous
public boolean isAnonymous()Checks whether the node is anonymous.- Returns:
- true if no name available
-
getName
Returns the name of the node.- Returns:
- the name, null for anonymous nodes
-
getValue
Returns the stored value.- Returns:
- the stored value, can be null
-
getValue
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
Returns the type of the container.- Returns:
- the type
-
addNull
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
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
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
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
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
Adds an array child to the object.- Parameters:
name
- the name of the pair- Returns:
- the new node, or null if none added
-
addNullArrayElement
Adds a null array element child to the array.- Returns:
- the new node, or null if none added
-
addObjectArrayElement
Add a key-value object child into the array- Returns:
- the newly added node
-
addArrayElement
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
Adds an object child to the object.- Parameters:
name
- the name of the pair- Returns:
- the new node, or null if none added
-
hasChild
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
Returns the child with the given name.- Parameters:
name
- the name of the child- Returns:
- the child if available, null otherwise
-
toString
Dumps the node structure into JSON format.- Parameters:
buffer
- the buffer to add the data to
-
toString
Returns a string representation of the node.- Overrides:
toString
in classDefaultMutableTreeNode
- Returns:
- the string representation
-
read
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
Only for testing. Generates a simple JSON object and displays it.- Parameters:
args
- ignored- Throws:
Exception
- if something goes wrong
-