public interface TreeNode
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.
Modifier and Type | Method and Description |
---|---|
JsonToken |
asToken()
Method that can be used for efficient type detection
when using stream abstraction for traversing nodes.
|
TreeNode |
at(JsonPointer ptr)
Method for locating node specified by given JSON pointer instances.
|
TreeNode |
at(String jsonPointerExpression)
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. |
Iterator<String> |
fieldNames()
Method for accessing names of all fields for this node, if (and only if)
this node is an Object node.
|
TreeNode |
get(int index)
Method for accessing value of the specified element of
an array node.
|
TreeNode |
get(String fieldName)
Method for accessing value of the specified field of
an object node.
|
boolean |
isArray()
Method that returns true if this node is an Array node, false
otherwise.
|
boolean |
isContainerNode()
Method that returns true for container nodes: Arrays and Objects.
|
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.
|
boolean |
isObject()
Method that returns true if this node is an Object node, false
otherwise.
|
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.
|
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. |
TreeNode |
path(int index)
Method for accessing value of the specified element of
an array node.
|
TreeNode |
path(String fieldName)
Method for accessing value of the specified field of
an object node.
|
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.
|
JsonParser |
traverse()
Method for constructing a
JsonParser instance for
iterating over contents of the tree that this node is root of. |
JsonParser |
traverse(ObjectCodec codec)
Same as
traverse() , but additionally passes ObjectCodec
to use if JsonParser.readValueAs(Class) is used (otherwise caller must call
JsonParser.setCodec(com.fasterxml.jackson.core.ObjectCodec) on response explicitly). |
JsonToken asToken()
JsonToken
that equivalent
stream event would produce (for most nodes there is just
one token but for structured/container types multiple)JsonToken
that is most closely associated with the node typeJsonParser.NumberType numberType()
JsonToken.isNumeric()
),
returns native type that node uses to store the numeric value;
otherwise returns null.int size()
boolean isValueNode()
Note: one and only one of methods isValueNode()
,
isContainerNode()
and isMissingNode()
ever
returns true for any given node.
null
boolean isContainerNode()
Note: one and only one of methods isValueNode()
,
isContainerNode()
and isMissingNode()
ever
returns true for any given node.
True
for Array and Object nodes, false
otherwiseboolean isMissingNode()
Note: one and only one of methods isValueNode()
,
isContainerNode()
and isMissingNode()
ever
returns true for any given node.
True
if this node represents a "missing" nodeboolean isArray()
isContainerNode()
must also return true.True
for Array nodes, false
for everything elseboolean isObject()
isContainerNode()
must also return true.True
for Object nodes, false
for everything elseTreeNode get(String fieldName)
NOTE: handling of explicit null values may vary between implementations; some trees may retain explicit nulls, others not.
fieldName
- Name of the field (of Object node) to accessnull
otherwise.TreeNode get(int index)
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.
index
- Index of the Array node element to accessnull
otherwise.TreeNode path(String fieldName)
isMissingNode()
returns true) is returned.fieldName
- Name of the field (of Object node) to accessTreeNode path(int index)
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.
index
- Index of the Array node element to accessIterator<String> fieldNames()
size()
.Iterator
otherwise (never null
).TreeNode at(JsonPointer ptr)
isMissingNode()
returns true.ptr
- JsonPointer
expression for descendant node to returnisMissingNode()
returns true
).TreeNode at(String jsonPointerExpression) throws IllegalArgumentException
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.
jsonPointerExpression
- Expression to compile as a JsonPointer
instanceisMissingNode()
returns true
).IllegalArgumentException
JsonParser traverse()
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(com.fasterxml.jackson.core.ObjectCodec)
).
It is often better to call traverse(ObjectCodec)
to pass the codec explicitly.
JsonParser
that will stream over contents of this nodeJsonParser traverse(ObjectCodec codec)
traverse()
, but additionally passes ObjectCodec
to use if JsonParser.readValueAs(Class)
is used (otherwise caller must call
JsonParser.setCodec(com.fasterxml.jackson.core.ObjectCodec)
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()
.
codec
- ObjectCodec
to associate with parser constructedJsonParser
that will stream over contents of this nodeCopyright © 2008–2024 FasterXML. All rights reserved.