Top Description Methods
com.fasterxml.jackson.core

public Interface TreeNode

Imports
java.util.Iterator

Marker interface used to denote JSON Tree nodes, as far as the core package knows them (which is very little): mostly needed to allow ObjectCodec to have some level of interoperability. Most functionality is within JsonNode base class in mapper package.

Note that in Jackson 1.x JsonNode itself was part of core package: Jackson 2.x refactored this since conceptually Tree Model is part of mapper package, and so part visible to core package should be minimized.

Note

starting with Jackson 2.2, there is more functionality available via this class, and the intent was that this should form actual base for multiple alternative tree representations; for example, immutable trees could use different implementation than mutable trees.

Since
2.2

Method Summary

Modifier and TypeMethod and Description
public JsonToken

Returns:

JsonToken that is most closely associated with the node type
asToken
()

Method that can be used for efficient type detection when using stream abstraction for traversing nodes.

public TreeNode

Returns:

Node that matches given JSON Pointer, if any: if no match exists, will return a "missing" node (for which TreeNode#isMissingNode() returns true).
at
(JsonPointer
JsonPointer expression for descendant node to return
ptr
)

Method for locating node specified by given JSON pointer instances.

public TreeNode

Returns:

Node that matches given JSON Pointer, if any: if no match exists, will return a "missing" node (for which TreeNode#isMissingNode() returns true).
at
(String
Expression to compile as a JsonPointer instance
jsonPointerExpression
)

Convenience method that is functionally equivalent to:

   return at(JsonPointer.valueOf(jsonPointerExpression));
public Iterator<String>

Returns:

An iterator for traversing names of all fields this Object node has (if Object node); empty Iterator otherwise (never null).
fieldNames
()

Method for accessing names of all fields for this node, if (and only if) this node is an Object node.

public TreeNode

Returns:

Node that represent value of the specified field, if this node is an Object and has value for the specified field; null otherwise.
get
(String
Name of the field (of Object node) to access
fieldName
)

Method for accessing value of the specified field of an object node.

public TreeNode

Returns:

Node that represent value of the specified element, if this node is an array and has specified element; null otherwise.
get
(int
Index of the Array node element to access
index
)

Method for accessing value of the specified element of an array node.

public boolean

Returns:

True for Array nodes, false for everything else
isArray
()

Method that returns true if this node is an Array node, false otherwise.

public boolean

Returns:

True for Array and Object nodes, false otherwise
isContainerNode
()

Method that returns true for container nodes: Arrays and Objects.

public boolean

Returns:

True if this node represents a "missing" node
isMissingNode
()

Method that returns true for "virtual" nodes which represent missing entries constructed by path accessor methods when there is no actual node matching given criteria.

public boolean

Returns:

True for Object nodes, false for everything else
isObject
()

Method that returns true if this node is an Object node, false otherwise.

public boolean

Returns:

True if this node is considered a value node; something that represents either a scalar value or explicit null
isValueNode
()

Method that returns true for all value nodes: ones that are not containers, and that do not represent "missing" nodes in the path.

public JsonParser.NumberType

Returns:

Type of number contained, if any; or null if node does not contain numeric value.
numberType
()

If this node is a numeric type (as per JsonToken#isNumeric), returns native type that node uses to store the numeric value; otherwise returns null.

public TreeNode

Returns:

Node that represent value of the specified field, if this node is an object and has value for the specified field; otherwise "missing node" is returned.
path
(String
Name of the field (of Object node) to access
fieldName
)

Method for accessing value of the specified field of an object node.

public TreeNode

Returns:

Node that represent value of the specified element, if this node is an array and has specified element; otherwise "missing node" is returned.
path
(int
Index of the Array node element to access
index
)

Method for accessing value of the specified element of an array node.

public int

Returns:

For non-container nodes returns 0; for arrays number of contained elements, and for objects number of fields.
size
()

Method that returns number of child nodes this node contains: for Array nodes, number of child elements, for Object nodes, number of fields, and for all other nodes 0.

public JsonParser

Returns:

JsonParser that will stream over contents of this node
traverse
()

Method for constructing a JsonParser instance for iterating over contents of the tree that this node is root of.

public JsonParser

Returns:

JsonParser that will stream over contents of this node
traverse
(ObjectCodec
ObjectCodec to associate with parser constructed
codec
)

Same as traverse(), but additionally passes com.fasterxml.jackson.core.ObjectCodec to use if JsonParser#readValueAs(Class) is used (otherwise caller must call JsonParser#setCodec on response explicitly).

Method Detail

asTokenback to summary
public JsonToken asToken()

Method that can be used for efficient type detection when using stream abstraction for traversing nodes. Will return the first JsonToken that equivalent stream event would produce (for most nodes there is just one token but for structured/container types multiple)

Returns:JsonToken

JsonToken that is most closely associated with the node type

atback to summary
public TreeNode at(JsonPointer ptr)

Method for locating node specified by given JSON pointer instances. Method will never return null; if no matching node exists, will return a node for which TreeNode#isMissingNode() returns true.

Parameters
ptr:JsonPointer

JsonPointer expression for descendant node to return

Returns:TreeNode

Node that matches given JSON Pointer, if any: if no match exists, will return a "missing" node (for which TreeNode#isMissingNode() returns true).

Since
2.3
atback to summary
public TreeNode at(String jsonPointerExpression) throws IllegalArgumentException

Convenience method that is functionally equivalent to:

   return at(JsonPointer.valueOf(jsonPointerExpression));

Note that if the same expression is used often, it is preferable to construct JsonPointer instance once and reuse it: this method will not perform any caching of compiled expressions.

Parameters
jsonPointerExpression:String

Expression to compile as a JsonPointer instance

Returns:TreeNode

Node that matches given JSON Pointer, if any: if no match exists, will return a "missing" node (for which TreeNode#isMissingNode() returns true).

Since
2.3
fieldNamesback to summary
public Iterator<String> fieldNames()

Method for accessing names of all fields for this node, if (and only if) this node is an Object node. Number of field names accessible will be size.

Returns:Iterator<String>

An iterator for traversing names of all fields this Object node has (if Object node); empty Iterator otherwise (never null).

Since
2.2
getback to summary
public TreeNode get(String fieldName)

Method for accessing value of the specified field of an object node. If this node is not an object (or it does not have a value for specified field name), or if there is no field with such name, null is returned.

Note

handling of explicit null values may vary between implementations; some trees may retain explicit nulls, others not.

Parameters
fieldName:String

Name of the field (of Object node) to access

Returns:TreeNode

Node that represent value of the specified field, if this node is an Object and has value for the specified field; null otherwise.

Since
2.2
getback to summary
public TreeNode get(int index)

Method for accessing value of the specified element of an array node. For other nodes, null is returned.

For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), null is returned; no exception is thrown for any index.

Parameters
index:int

Index of the Array node element to access

Returns:TreeNode

Node that represent value of the specified element, if this node is an array and has specified element; null otherwise.

Since
2.2
isArrayback to summary
public boolean isArray()

Method that returns true if this node is an Array node, false otherwise. Note that if true is returned, isContainerNode must also return true.

Returns:boolean

True for Array nodes, false for everything else

Since
2.2
isContainerNodeback to summary
public boolean isContainerNode()

Method that returns true for container nodes: Arrays and Objects.

Note

one and only one of methods isValueNode, isContainerNode and isMissingNode ever returns true for any given node.

Returns:boolean

True for Array and Object nodes, false otherwise

Since
2.2
isMissingNodeback to summary
public boolean isMissingNode()

Method that returns true for "virtual" nodes which represent missing entries constructed by path accessor methods when there is no actual node matching given criteria.

Note

one and only one of methods isValueNode, isContainerNode and isMissingNode ever returns true for any given node.

Returns:boolean

True if this node represents a "missing" node

Since
2.2
isObjectback to summary
public boolean isObject()

Method that returns true if this node is an Object node, false otherwise. Note that if true is returned, isContainerNode must also return true.

Returns:boolean

True for Object nodes, false for everything else

Since
2.2
isValueNodeback to summary
public boolean isValueNode()

Method that returns true for all value nodes: ones that are not containers, and that do not represent "missing" nodes in the path. Such value nodes represent String, Number, Boolean and null values from JSON.

Note

one and only one of methods isValueNode, isContainerNode and isMissingNode ever returns true for any given node.

Returns:boolean

True if this node is considered a value node; something that represents either a scalar value or explicit null

Since
2.2
numberTypeback to summary
public JsonParser.NumberType numberType()

If this node is a numeric type (as per JsonToken#isNumeric), returns native type that node uses to store the numeric value; otherwise returns null.

Returns:JsonParser.NumberType

Type of number contained, if any; or null if node does not contain numeric value.

pathback to summary
public TreeNode path(String fieldName)

Method for accessing value of the specified field of an object node. For other nodes, a "missing node" (virtual node for which isMissingNode returns true) is returned.

Parameters
fieldName:String

Name of the field (of Object node) to access

Returns:TreeNode

Node that represent value of the specified field, if this node is an object and has value for the specified field; otherwise "missing node" is returned.

Since
2.2
pathback to summary
public TreeNode path(int index)

Method for accessing value of the specified element of an array node. For other nodes, a "missing node" (virtual node for which isMissingNode returns true) is returned.

For array nodes, index specifies exact location within array and allows for efficient iteration over child elements (underlying storage is guaranteed to be efficiently indexable, i.e. has random-access to elements). If index is less than 0, or equal-or-greater than node.size(), "missing node" is returned; no exception is thrown for any index.

Parameters
index:int

Index of the Array node element to access

Returns:TreeNode

Node that represent value of the specified element, if this node is an array and has specified element; otherwise "missing node" is returned.

Since
2.2
sizeback to summary
public int size()

Method that returns number of child nodes this node contains: for Array nodes, number of child elements, for Object nodes, number of fields, and for all other nodes 0.

Returns:int

For non-container nodes returns 0; for arrays number of contained elements, and for objects number of fields.

Since
2.2
traverseback to summary
public JsonParser traverse()

Method for constructing a JsonParser instance for iterating over contents of the tree that this node is root of. Functionally equivalent to first serializing tree using ObjectCodec and then re-parsing but more efficient.

Note

constructed parser instance will NOT initially point to a token, so before passing it to deserializers, it is typically necessary to advance it to the first available token by calling JsonParser#nextToken().

Also note that calling this method will NOT pass ObjectCodec reference, so data-binding callback methods like JsonParser#readValueAs(Class) will not work with calling JsonParser#setCodec). It is often better to call traverse(ObjectCodec) to pass the codec explicitly.

Returns:JsonParser

JsonParser that will stream over contents of this node

traverseback to summary
public JsonParser traverse(ObjectCodec codec)

Same as traverse(), but additionally passes com.fasterxml.jackson.core.ObjectCodec to use if JsonParser#readValueAs(Class) is used (otherwise caller must call JsonParser#setCodec on response explicitly).

Note

constructed parser instance will NOT initially point to a token, so before passing it to deserializers, it is typically necessary to advance it to the first available token by calling JsonParser#nextToken().

Parameters
codec:ObjectCodec

ObjectCodec to associate with parser constructed

Returns:JsonParser

JsonParser that will stream over contents of this node

Since
2.1