- All Implemented Interfaces:
Iterable<JsonNode>
,TreeNode
,JacksonSerializable
- Direct Known Subclasses:
BaseJsonNode
As a general design rule, most accessors ("getters") are included
in this base class, to allow for traversing structure without
type casts. Most mutators, however, need to be accessed through
specific sub-classes (such as ObjectNode
and ArrayNode
).
This seems sensible because proper type
information is generally available when building or modifying
trees, but less often when reading a tree (newly built from
parsed JSON content).
Actual concrete sub-classes can be found from package
tools.jackson.databind.node
.
Note that it is possible to "read" from nodes, using
method TreeNode.traverse(tools.jackson.core.ObjectReadContext)
, which will result in
a JsonParser
being constructed. This can be used for (relatively)
efficient conversations between different representations; and it is what
core databind uses for methods like ObjectMapper.treeToValue(TreeNode, Class)
and ObjectMapper.treeAsTokens(TreeNode)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Configuration setting used withwithObject(JsonPointer)
method overrides, to indicate which overwrites are acceptable if the path pointer indicates has incompatible nodes (for example, instead of Object node a Null node is encountered).Nested classes/interfaces inherited from interface tools.jackson.databind.JacksonSerializable
JacksonSerializable.Base
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract JsonNode
_at
(JsonPointer ptr) Helper method used by other methods for traversing the next step of given path expression, and returning matching value node if any: if no match,null
is returned.protected <T> T
_reportRequiredViolation
(String msgTemplate, Object... args) Helper method that throwsDatabindException
as a result of violating "required-constraint" for this node (forrequired(java.lang.String)
or related methods).protected <T> T
_reportUnsupportedOperation
(String msgTemplate, Object... args) protected <T extends JsonNode>
T_this()
abstract BigInteger
Method similar tobigIntegerValue()
but in addition to coercing Number values (same asbigIntegerValue()
), will also try to coerce a couple of additional types (or cases): JSON Floating-point numbers with fractions (ones without fractions are ok forbigIntegerValue()
) will be truncated to integer value (like withBigDecimal.toBigInteger()
)) JSON Strings that represent JSON Numbers ("stringified" numbers) JSON Null (converted to0
)) POJO nodes that contain Number valuesabstract BigInteger
asBigInteger
(BigInteger defaultValue) Method similar toasBigInteger()
, but that will return specifieddefaultValue
if this node cannot be converted toBigInteger
.abstract Optional<BigInteger>
Method similar tobigIntegerValue()
, but that will return empty (Optional.empty()
) if this node cannot be converted to JavaBigInteger
.abstract boolean
Method that will try to convert value of this node to a Javaboolean
.abstract boolean
asBoolean
(boolean defaultValue) Similar toasBoolean()
, but instead of throwing an exception for non-coercible values, will return specified default value.Similar toasBoolean()
, but instead of throwing an exception for non-coercible values, will returnOptional.empty()
.abstract BigDecimal
Method that will try to access value of this node as aBigDecimal
: but if node value cannot be expressed exactly as aBigDecimal
, aJsonNodeException
will be thrown.abstract BigDecimal
asDecimal
(BigDecimal defaultValue) Method similar toasDecimal()
, but that will returndefaultValue
if this node cannot be coerced to JavaBigDecimal
.abstract Optional<BigDecimal>
Method similar toasDecimal()
, but that will return emptyOptional
(Optional.empty()
) if this node cannot be coerced toBigDecimal
.abstract double
asDouble()
Method similar todoubleValue()
but in addition to coercing Number values will also try coerce couple of additional types: JSON String that represents JSON Numbers ("stringified" numbers) JSON Null (converted to0.0d
) POJO nodes that contain Number valuesabstract double
asDouble
(double defaultValue) Method similar toasDouble()
, but that will returndefaultValue
if this node cannot be coerced todouble
.abstract OptionalDouble
Method similar toasDouble()
, but that will return (OptionalDouble.empty()
) if this node cannot be coerced todouble
.abstract float
asFloat()
Method similar tofloatValue()
but in addition to coercing Number values will also try coerce couple of additional types: JSON String that represents JSON Numbers ("stringified" numbers) JSON Null (converted to0.0f
) POJO nodes that contain Number valuesabstract float
asFloat
(float defaultValue) Method similar toasFloat()
, but that will returndefaultValue
if this node cannot be coerced tofloat
.Method similar toasFloat()
, but that will return (Optional.empty()
) if this node cannot be coerced tofloat
.abstract int
asInt()
Method similar tointValue()
but in addition to coercing Number values (same asintValue()
), will also try to coerce a couple of additional types (or cases): JSON Floating-point numbers with fractions (ones without fractions are ok forintValue()
) will be truncated toint
(if (and only if) they fit inint
range).abstract int
asInt
(int defaultValue) Method similar tointValue()
, but that will return specifieddefaultValue
if this node cannot be converted toint
.abstract OptionalInt
asIntOpt()
Method similar toasInt()
, but that will return (OptionalInt.empty()
) if this node cannot be coerced toint
.abstract long
asLong()
Method similar tolongValue()
but in addition to coercing Number values (same aslongValue()
), will also try to coerce a couple of additional types (or cases): JSON Floating-point numbers with fractions (ones without fractions are ok forlongValue()
) will be truncated tolong
(if (and only if) they fit inlong
range).abstract long
asLong
(long defaultValue) Method similar toasLong()
, but that will return specifieddefaultValue
if this node cannot be coerced tolong
(instead of throwing an exception).abstract OptionalLong
Method similar toasLong()
, but that will return (OptionalLong.empty()
) if this node cannot be coerced tolong
.abstract short
asShort()
Method similar toshortValue()
but in addition to coercing Number values (same asshortValue()
), will also try to coerce a couple of additional types (or cases): JSON Floating-point numbers with fractions (ones without fractions are ok forshortValue()
) will be truncated toshort
(if (and only if) they fit inshort
range).abstract short
asShort
(short defaultValue) Method similar toshortValue()
, but that will return specifieddefaultValue
if this node cannot be converted toshort
.Method similar toasShort()
, but that will return (Optional.empty()
) if this node cannot be coerced toshort
.abstract String
asString()
Method that will try to convert value of this node to aString
.abstract String
Similar toasString()
, but instead of throwing an exception for non-coercible values, will return specified default value.Similar toasString()
, but instead of throwing an exception for non-coercible values, will returnOptional.empty()
.final String
asText()
Deprecated.Deprecated.UseasString(String)
instead.final JsonNode
Convenience method that is functionally equivalent to:final JsonNode
at
(JsonPointer ptr) Method for locating node specified by given JSON pointer instances.abstract BigInteger
Method that will try to access value of this node as aBigInteger
, but if node value cannot be expressed exactly as aBigInteger
, aJsonNodeException
will be thrown.abstract BigInteger
bigIntegerValue
(BigInteger defaultValue) Method similar tobigIntegerValue()
, but that will return specifieddefaultValue
if this node cannot be converted to JavaBigInteger
.abstract Optional<BigInteger>
Method similar tobigIntegerValue()
, but that will return empty (Optional.empty()
) if this node cannot be converted to JavaBigInteger
.abstract byte[]
Method that will try to access value of this node as binary value (Javabyte[]
) which works if (and only if) node contains binary value (for JSON, Base64-encoded String, for other formats native binary value): if not, aJsonNodeException
will be thrown.abstract boolean
Method that will try to access value of this node as a Javaboolean
which works if (and only if) node contains JSON boolean value: if not, aJsonNodeException
will be thrown.abstract boolean
booleanValue
(boolean defaultValue) Method similar tobooleanValue()
, but that will return specifieddefaultValue
if this node does not contain a JSON boolean.Method similar tobooleanValue()
, but that will returnOptional.empty()
if this node does not contain a JSON boolean.boolean
Method that can be used to check whether contained value is numeric (returns true forisNumber()
) and can be converted without loss to integral number (specifically,BigInteger
but potentially others, seecanConvertToInt()
andcanConvertToInt()
).boolean
Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss toint
(that is, its value fits in Java's 32-bit signed integer type,int
and if it is a floating-point number, it does not have fractional part).boolean
Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss tolong
(that is, its value fits in Java's 64-bit signed integer type,long
and if it is a floating-point number, it does not have fractional part).boolean
Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss toshort
(that is, its value fits in Java's 16-bit signed integer type,short
and if it is a floating-point number, it does not have fractional part).abstract BigDecimal
Method that will try to access value of this node as a JavaBigDecimal
: but if node value cannot be expressed exactly as aBigDecimal
, aJsonNodeException
will be thrown.abstract BigDecimal
decimalValue
(BigDecimal defaultValue) Method similar todecimalValue()
, but that will returndefaultValue
if this node cannot be coerced to JavaBigDecimal
.abstract Optional<BigDecimal>
Method similar todecimalValue()
, but that will return emptyOptional
(Optional.empty()
) if this node cannot be coerced toBigDecimal
.abstract JsonNode
deepCopy()
Method that can be called to get a node that is guaranteed not to allow changing of this node through mutators on this node or any of its children.abstract double
Method that will try to access value of this node as adouble
: but if node value cannot be expressed exactly as adouble
, aJsonNodeException
will be thrown.abstract double
doubleValue
(double defaultValue) Method similar todoubleValue()
, but that will return specifieddefaultValue
if this node cannot be converted todouble
.abstract OptionalDouble
Method similar todoubleValue()
, but that will return emptyOptionalLong
(OptionalDouble.empty()
) if this node cannot be converted to Javadouble
.abstract boolean
Equality for node objects is defined as full (deep) value equality.boolean
equals
(Comparator<JsonNode> comparator, JsonNode other) Entry method for invoking customizable comparison, using passed-inComparator
object.abstract JsonNode
findParent
(String propertyName) Method for finding a JSON Object that contains specified field, within this node or its descendants.findParents
(String propertyName) Method for finding a JSON Object that contains specified field, within this node or its descendants.findParents
(String propertyName, List<JsonNode> foundSoFar) abstract JsonNode
Method similar tofindValue(java.lang.String)
, but that will return a "missing node" instead of null if no field is found.abstract JsonNode
Method for finding the first JSON Object field with specified name in this node or its child nodes, and returning value it has.findValues
(String propertyName) Method for finding JSON Object fields with specified name -- both immediate child values and descendants -- and returning found ones as aList
.findValues
(String propertyName, List<JsonNode> foundSoFar) findValuesAsString
(String propertyName) Similar tofindValues(java.lang.String)
, but will additionally convert values into Strings, callingasText()
.findValuesAsString
(String propertyName, List<String> foundSoFar) abstract float
Method that will try to access value of this node as a Javafloat
: but if node value cannot be expressed exactly as afloat
, aJsonNodeException
will be thrown.abstract float
floatValue
(float defaultValue) Method similar tofloatValue()
, but that will return specifieddefaultValue
if this node cannot be converted tofloat
.Method similar tofloatValue()
, but that will return emptyOptional<Float>
(Optional.empty()
) if this node cannot be converted to Javafloat
.void
forEachEntry
(BiConsumer<? super String, ? super JsonNode> action) If this node is anObjectNode
, performs the given action for each property (key, value pair) until all entries have been processed or the action throws an exception.abstract JsonNode
get
(int index) Method for accessing value of the specified element of an array node.Method for accessing value of the specified field of an object node.abstract JsonNodeType
Return the type of this nodeboolean
has
(int index) Method that allows checking whether this node is JSON Array node and contains a value for specified index If this is the case (including case of specified indexing having null as value), returns true; otherwise returns false.boolean
Method that allows checking whether this node is JSON Object node and contains value for specified property.boolean
hasNonNull
(int index) Method that is similar tohas(int)
, but that will returnfalse
for explicitly added nulls.boolean
hasNonNull
(String propertyName) Method that is similar tohas(String)
, but that will returnfalse
for explicitly added nulls.abstract int
intValue()
Method that will try to access value of this node as a Javaint
: but if node value cannot be expressed exactly as anint
, aJsonNodeException
will be thrown.abstract int
intValue
(int defaultValue) Method similar tointValue()
, but that will return specifieddefaultValue
if this node cannot be converted to Javaint
.abstract OptionalInt
Method similar tointValue()
, but that will return emptyOptionalInt
(OptionalInt.empty()
) if this node cannot be converted to Javaint
.boolean
isArray()
boolean
boolean
final boolean
isBinary()
Method that can be used to check if this node represents binary data (Base64 encoded).final boolean
Method that can be used to check if this node was created from JSON boolean value (literals "true" and "false").boolean
boolean
isDouble()
boolean
isEmpty()
Convenience method that is functionally same as:boolean
isFloat()
boolean
boolean
isInt()
Method that can be used to check whether contained value is a number represented as Javaint
.boolean
boolean
isLong()
Method that can be used to check whether contained value is a number represented as Javalong
.boolean
final boolean
isNull()
Method that can be used to check if this node was created from JSON literal null value.final boolean
isNumber()
boolean
isObject()
final boolean
isPojo()
Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean".boolean
isShort()
Method that can be used to check whether contained value is a number represented as Javashort
.final boolean
isString()
Method that checks whether this node represents JSON String value.boolean
Deprecated.UseisString()
instead.final boolean
iterator()
Implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs.abstract long
Method that will try to access value of this node as a Javalong
: but if node value cannot be expressed exactly as along
, aJsonNodeException
will be thrown.abstract long
longValue
(long defaultValue) Method similar tolongValue()
, but that will return specifieddefaultValue
if this node cannot be converted tolong
.abstract OptionalLong
Method similar tolongValue()
, but that will return emptyOptionalLong
(OptionalLong.empty()
) if this node cannot be converted to Javalong
.abstract Number
Method that will try to access value of this node asNumber
that accurately represents its value, if (and only if) this is a number node (returnstrue
forisNumber()
).optional
(int index) Method for accessing value of the specified element of an array node, wrapped in anOptional
.Method for accessing value of the specified field of an object node.abstract JsonNode
path
(int index) This method is similar toget(int)
, except that instead of returning null if no such element exists (due to index being out of range, or this node not being an array), a "missing node" (node that returns true forisMissingNode()
) will be returned.abstract JsonNode
This method is similar toget(String)
, except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a "missing node" (node that returns true forisMissingNode()
) will be returned.Accessor that will return properties ofObjectNode
similar to howMap.entrySet()
works; for other node types will return emptySet
.Returns a stream of all properties (key, value pairs) of this Node, iff this node is an anObjectNode
.<T extends JsonNode>
Trequire()
Method that may be called to verify thatthis
node is NOT so-called "missing node": that is, one for whichisMissingNode()
returnstrue
.abstract JsonNode
required
(int index) Method is functionally equivalent topath(index).required()
and can be used to check that this node is anArrayNode
(that is, represents JSON Array value) and has value for specifiedindex
(but note that value may be explicit JSON null value).abstract JsonNode
Method is functionally equivalent topath(propertyName).required()
and can be used to check that this node is anObjectNode
(that is, represents JSON Object value) and has value for specified property with keypropertyName
(but note that value may be explicit JSON null value).requiredAt
(String pathExpr) Method is functionally equivalent toat(pathExpr).required()
and can be used to check that there is an actual value node at specifiedJsonPointer
starting fromthis
node (but note that value may be explicit JSON null value).final JsonNode
requiredAt
(JsonPointer path) Method is functionally equivalent toat(path).required()
and can be used to check that there is an actual value node at specifiedJsonPointer
starting fromthis
node (but note that value may be explicit JSON null value).<T extends JsonNode>
TMethod that may be called to verify thatthis
node is neither so-called "missing node" (that is, one for whichisMissingNode()
returnstrue
) nor "null node" (one for whichisNull()
returnstrue
).abstract short
Method that will try to access value of this node as 16-bit signed integer value (Javashort
): but if node value cannot be expressed exactly as ashort
, aJsonNodeException
will be thrown.abstract short
shortValue
(short defaultValue) Method similar toshortValue()
, but that will return specifieddefaultValue
if this node cannot be converted to Javashort
.Method similar toshortValue()
, but that will return emptyOptional<Short>
(Optional.empty()
) if this node cannot be converted to Javashort
.int
size()
final Spliterator<JsonNode>
abstract String
Method that will try to access value of this node as a JavaString
which works if (and only if) node contains JSON String value: if not, aJsonNodeException
will be thrown.abstract String
stringValue
(String defaultValue) Method similar tostringValue()
, but that will return specifieddefaultValue
if this node does not contain a JSON String.Method similar tostringValue()
, but that will returnOptional.empty()
if this node does not contain a JSON String.final String
Deprecated.UseasString()
instead.Alternative totoString()
that will serialize this node using Jackson default pretty-printer.abstract String
toString()
Method that will produce (as of Jackson 2.10) valid JSON using default settings of databind, as String.values()
Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node.Returns a stream of all value nodes of this Node, iff this node is anArrayNode
orObjectNode
.Short-cut equivalent to:withArray
(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Short-cut equivalent to:final ArrayNode
withArray
(JsonPointer ptr) Same aswithArray(JsonPointer, OverwriteMode, boolean)
but with defaults ofOvewriteMode#NULLS
(overwrite mode) andtrue
forpreferIndex
.withArray
(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Method that can be called on Object or Array nodes, to access a Array-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it.withArrayProperty
(String propName) Method similar towithArray(JsonPointer, OverwriteMode, boolean)
-- basically short-cut to:withObject
(String exprOrProperty) Method that works in one of possible ways, depending on whetherexprOrProperty
is a validJsonPointer
expression or not (valid expression is either empty String""
or starts with leading slash/
character).final ObjectNode
withObject
(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Short-cut equivalent to:final ObjectNode
withObject
(JsonPointer ptr) Same aswithObject(JsonPointer, OverwriteMode, boolean)
but with defaults ofOvewriteMode#NULLS
(overwrite mode) andtrue
forpreferIndex
(that is, will try to considerJsonPointer
segments index if at all possible and only secondarily as property namewithObject
(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Method that can be called on Object or Array nodes, to access a Object-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it.withObjectProperty
(String propName) Method similar towithObject(JsonPointer, OverwriteMode, boolean)
-- basically short-cut to:Methods inherited from class tools.jackson.databind.JacksonSerializable.Base
isEmpty
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface tools.jackson.databind.JacksonSerializable
serialize, serializeWithType
Methods inherited from interface tools.jackson.core.TreeNode
asToken, isEmbeddedValue, numberType, traverse
-
Constructor Details
-
JsonNode
protected JsonNode()
-
-
Method Details
-
deepCopy
Method that can be called to get a node that is guaranteed not to allow changing of this node through mutators on this node or any of its children. This means it can either make a copy of this node (and all mutable children and grand children nodes), or node itself if it is immutable.Note: return type is guaranteed to have same type as the node method is called on; which is why method is declared with local generic type.
- Returns:
- Node that is either a copy of this node (and all non-leaf children); or, for immutable leaf nodes, node itself.
-
size
public int size() -
isEmpty
public boolean isEmpty()Convenience method that is functionally same as:size() == 0
for all node types. -
isValueNode
public final boolean isValueNode()- Specified by:
isValueNode
in interfaceTreeNode
-
isContainer
public boolean isContainer()- Specified by:
isContainer
in interfaceTreeNode
-
isMissingNode
public boolean isMissingNode()- Specified by:
isMissingNode
in interfaceTreeNode
-
isArray
public boolean isArray() -
isObject
public boolean isObject() -
get
Method for accessing value of the specified element of an array node. For other nodes, null is always 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.NOTE: if the element value has been explicitly set as
null
(which is different from removal!), aNullNode
will be returned, not null. -
get
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: if the property value has been explicitly set as
null
(which is different from removal!), aNullNode
will be returned, not null. -
optional
Method for accessing value of the specified element of an array node, wrapped in anOptional
. For other nodes, an empty Optional is always 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()
, an empty Optional is returned; no exception is thrown for any index.NOTE: if the element value has been explicitly set as
null
(which is different from removal!), aNullNode
will be returned wrapped in an Optional, not an empty Optional.- Returns:
- Optional containing the node that represents the value of the specified element, if this node is an array and has the specified element and otherwise, an empty Optional, never null.
-
optional
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, emptyOptional
is returned.NOTE: if the property value has been explicitly set as
null
(which is different from removal!), an Optional containingNullNode
will be returned, not null.- Returns:
- Optional that may contain value of the specified field, if this node is an object and has value for the specified field. Empty Optional otherwise never null.
-
path
This method is similar toget(String)
, except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a "missing node" (node that returns true forisMissingNode()
) will be returned. This allows for convenient and safe chained access via path calls. -
path
This method is similar toget(int)
, except that instead of returning null if no such element exists (due to index being out of range, or this node not being an array), a "missing node" (node that returns true forisMissingNode()
) will be returned. This allows for convenient and safe chained access via path calls. -
propertyNames
- Specified by:
propertyNames
in interfaceTreeNode
-
at
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 whichisMissingNode()
returns true.- Specified by:
at
in interfaceTreeNode
- Returns:
- Node that matches given JSON Pointer: if no match exists,
will return a node for which
isMissingNode()
returns true.
-
at
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.- Specified by:
at
in interfaceTreeNode
- Parameters:
jsonPtrExpr
- Expression to compile as aJsonPointer
instance- Returns:
- Node that matches given JSON Pointer: if no match exists,
will return a node for which
TreeNode.isMissingNode()
returns true.
-
_at
Helper method used by other methods for traversing the next step of given path expression, and returning matching value node if any: if no match,null
is returned.- Parameters:
ptr
- Path expression to use- Returns:
- Either matching
JsonNode
for the first step of path ornull
if no match (including case that this node is not a container)
-
getNodeType
Return the type of this node- Returns:
- the node type as a
JsonNodeType
enum value
-
isPojo
public final boolean isPojo()Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean". Returns true only for instances ofPOJONode
.- Returns:
- True if this node wraps a POJO
-
isNumber
public final boolean isNumber()- Returns:
- True if this node represents a numeric JSON value
-
isIntegralNumber
public boolean isIntegralNumber()- Returns:
- True if this node represents an integral (integer) numeric JSON value
-
isFloatingPointNumber
public boolean isFloatingPointNumber()- Returns:
- True if this node represents a non-integral numeric JSON value
-
isShort
public boolean isShort()Method that can be used to check whether contained value is a number represented as Javashort
. Note, however, that even if this method returns false, it is possible that conversion would be possible from other numeric types -- to check if this is possible, usecanConvertToInt()
instead.- Returns:
- True if the value contained by this node is stored as Java short
-
isInt
public boolean isInt()Method that can be used to check whether contained value is a number represented as Javaint
. Note, however, that even if this method returns false, it is possible that conversion would be possible from other numeric types -- to check if this is possible, usecanConvertToInt()
instead.- Returns:
- True if the value contained by this node is stored as Java int
-
isLong
public boolean isLong()Method that can be used to check whether contained value is a number represented as Javalong
. Note, however, that even if this method returns false, it is possible that conversion would be possible from other numeric types -- to check if this is possible, usecanConvertToLong()
instead.- Returns:
- True if the value contained by this node is stored as Java
long
-
isFloat
public boolean isFloat() -
isDouble
public boolean isDouble() -
isBigDecimal
public boolean isBigDecimal() -
isBigInteger
public boolean isBigInteger() -
isString
public final boolean isString()Method that checks whether this node represents JSON String value. -
isTextual
Deprecated.UseisString()
instead. -
isBoolean
public final boolean isBoolean()Method that can be used to check if this node was created from JSON boolean value (literals "true" and "false"). -
isNull
public final boolean isNull()Method that can be used to check if this node was created from JSON literal null value. -
isBinary
public final boolean isBinary()Method that can be used to check if this node represents binary data (Base64 encoded). Although this will be externally written as JSON String value,isString()
will return false if this method returns true.- Returns:
- True if this node represents base64 encoded binary data
-
canConvertToShort
public boolean canConvertToShort()Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss toshort
(that is, its value fits in Java's 16-bit signed integer type,short
and if it is a floating-point number, it does not have fractional part).NOTE: this method does not consider possible value type conversion from non-number types like JSON String into Number; so even if this method returns false, it is possible that
asShort()
could still succeed. -
canConvertToInt
public boolean canConvertToInt()Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss toint
(that is, its value fits in Java's 32-bit signed integer type,int
and if it is a floating-point number, it does not have fractional part).NOTE: this method does not consider possible value type conversion from non-number types like JSON String into Number; so even if this method returns false, it is possible that
asInt()
could still succeed. -
canConvertToLong
public boolean canConvertToLong()Method that can be used to check whether this node is a numeric node (isNumber()
would return true) AND can be converted without loss tolong
(that is, its value fits in Java's 64-bit signed integer type,long
and if it is a floating-point number, it does not have fractional part).NOTE: this method does not consider possible value type conversion from non-number types like JSON String into Number; so even if this method returns false, it is possible that
asLong()
could still succeed. -
canConvertToExactIntegral
public boolean canConvertToExactIntegral()Method that can be used to check whether contained value is numeric (returns true forisNumber()
) and can be converted without loss to integral number (specifically,BigInteger
but potentially others, seecanConvertToInt()
andcanConvertToInt()
). Latter part allows floating-point numbers (for whichisFloatingPointNumber()
returnstrue
) that do not have fractional part. Note that "not-a-number" values ofdouble
andfloat
will returnfalse
as they can not be converted to matching integral representations.- Returns:
- True if the value is an actual number with no fractional part; false for non-numeric types, NaN representations of floating-point numbers, and floating-point numbers with fractional part.
-
asOptional
Method that will return aJsonNode
wrapped in Java'sOptional
. All nodes except ofMissingNode
will return non-emptyOptional
;MissingNode
will return emptyOptional
.- Returns:
Optional<JsonNode>
containing this node, orOptional.empty()
if this is aMissingNode
.
-
stringValue
Method that will try to access value of this node as a JavaString
which works if (and only if) node contains JSON String value: if not, aJsonNodeException
will be thrown.NOTE: for more lenient conversions, use
asString()
NOTE: in Jackson 2.x, was
textValue()
.- Returns:
String
value this node represents (if JSON String)- Throws:
JsonNodeException
- if node value is not a JSON String value
-
stringValue
Method similar tostringValue()
, but that will return specifieddefaultValue
if this node does not contain a JSON String.- Parameters:
defaultValue
- Value to return if this node does not contain a JSON String.- Returns:
- Java
String
value this node represents (if JSON String);defaultValue
otherwise
-
stringValueOpt
Method similar tostringValue()
, but that will returnOptional.empty()
if this node does not contain a JSON String.- Returns:
Optional<String>
value (if node represents JSON String);Optional.empty()
otherwise
-
textValue
Deprecated.UseasString()
instead. -
asString
Method that will try to convert value of this node to aString
. JSON Strings map naturally; other scalars map to their string representation (including Binary data as Base64 encoded String); JSONnull
s map to empty String. Other values (including structured types like Objects and Arrays, and "missing" value) will result in aJsonNodeException
being thrown.NOTE: this is NOT same as
toString()
in that result isNOT VALID ENCODED JSON
for all nodes (although is for some, likeNumberNode
s andBooleanNode
s).- Returns:
- String representation of this node, if coercible; exception otherwise
- Throws:
JsonNodeException
- if node cannot be coerced to aString
-
asString
Similar toasString()
, but instead of throwing an exception for non-coercible values, will return specified default value. -
asStringOpt
Similar toasString()
, but instead of throwing an exception for non-coercible values, will returnOptional.empty()
. -
asText
Deprecated.UseasString()
instead. -
asText
Deprecated.UseasString(String)
instead. -
binaryValue
public abstract byte[] binaryValue()Method that will try to access value of this node as binary value (Javabyte[]
) which works if (and only if) node contains binary value (for JSON, Base64-encoded String, for other formats native binary value): if not, aJsonNodeException
will be thrown. To check if this method can be used, you may callisBinary()
.- Returns:
- Binary value this node represents (if node contains binary value)
- Throws:
JsonNodeException
- if node does not contain a Binary value (a
-
booleanValue
public abstract boolean booleanValue()Method that will try to access value of this node as a Javaboolean
which works if (and only if) node contains JSON boolean value: if not, aJsonNodeException
will be thrown.NOTE: for more lenient conversions, use
asBoolean()
- Returns:
boolean
value this node represents (if JSON boolean)- Throws:
JsonNodeException
- if node does not represent a JSON boolean value
-
booleanValue
public abstract boolean booleanValue(boolean defaultValue) Method similar tobooleanValue()
, but that will return specifieddefaultValue
if this node does not contain a JSON boolean.- Parameters:
defaultValue
- Value to return if this node does not contain a JSON boolean.- Returns:
- Java
boolean
value this node represents (if JSON boolean);defaultValue
otherwise
-
booleanValueOpt
Method similar tobooleanValue()
, but that will returnOptional.empty()
if this node does not contain a JSON boolean.- Returns:
Optional<Boolean>
value (if node represents JSON boolean);Optional.empty()
otherwise
-
asBoolean
public abstract boolean asBoolean()Method that will try to convert value of this node to a Javaboolean
. JSON Booleans map naturally; Integer numbers other than 0 map to true, and 0 maps to false;null
maps to false and Strings 'true' and 'false' map to corresponding values. Other values (including structured types like Objects and Arrays) will result in aJsonNodeException
being thrown.- Returns:
- Boolean value this node represents, if coercible; exception otherwise
- Throws:
JsonNodeException
- if node cannot be coerced to a Javaboolean
-
asBoolean
public abstract boolean asBoolean(boolean defaultValue) Similar toasBoolean()
, but instead of throwing an exception for non-coercible values, will return specified default value. -
asBooleanOpt
Similar toasBoolean()
, but instead of throwing an exception for non-coercible values, will returnOptional.empty()
. -
numberValue
Method that will try to access value of this node asNumber
that accurately represents its value, if (and only if) this is a number node (returnstrue
forisNumber()
). If this node is NOT a number node, aJsonNodeException
will be thrown.- Returns:
- Number value this node contains, if numeric node
-
shortValue
public abstract short shortValue()Method that will try to access value of this node as 16-bit signed integer value (Javashort
): but if node value cannot be expressed exactly as ashort
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Integer values that fit in Java 16-bit signed
short
range - JSON Floating-point values that fit in Java 16-bit signed
short
range AND do not have fractional part.
- Returns:
Short
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to Javashort
- JSON Integer values that fit in Java 16-bit signed
-
shortValue
public abstract short shortValue(short defaultValue) Method similar toshortValue()
, but that will return specifieddefaultValue
if this node cannot be converted to Javashort
.- Parameters:
defaultValue
- Value to return if this node cannot be converted to Javashort
- Returns:
- Java
short
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
shortValueOpt
Method similar toshortValue()
, but that will return emptyOptional<Short>
(Optional.empty()
) if this node cannot be converted to Javashort
.- Returns:
- Java
short
value this node represents, asOptional<Short>
, if possible to accurately represent;Optional.empty()
otherwise
-
asShort
public abstract short asShort()Method similar toshortValue()
but in addition to coercing Number values (same asshortValue()
), will also try to coerce a couple of additional types (or cases):- JSON Floating-point numbers with fractions (ones without fractions
are ok for
shortValue()
) will be truncated toshort
(if (and only if) they fit inshort
range). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0
)) - POJO nodes that contain Number values
- Returns:
short
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted toshort
- JSON Floating-point numbers with fractions (ones without fractions
are ok for
-
asShort
public abstract short asShort(short defaultValue) Method similar toshortValue()
, but that will return specifieddefaultValue
if this node cannot be converted toshort
.- Parameters:
defaultValue
- Value to return if this node cannot be converted toshort
- Returns:
short
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asShortOpt
Method similar toasShort()
, but that will return (Optional.empty()
) if this node cannot be coerced toshort
.- Returns:
Optional<Short>
value this node represents, if possible to accurately represent;Optional.empty()
otherwise
-
intValue
public abstract int intValue()Method that will try to access value of this node as a Javaint
: but if node value cannot be expressed exactly as anint
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Integer values that fit in Java 32-bit signed
int
range - JSON Floating-point values that fit in Java 32-bit signed
int
range AND do not have fractional part.
NOTE: for more lenient conversions, use
asInt()
- Returns:
Int
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to Javaint
- JSON Integer values that fit in Java 32-bit signed
-
intValue
public abstract int intValue(int defaultValue) Method similar tointValue()
, but that will return specifieddefaultValue
if this node cannot be converted to Javaint
.- Parameters:
defaultValue
- Value to return if this node cannot be converted to Javaint
- Returns:
- Java
int
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
intValueOpt
Method similar tointValue()
, but that will return emptyOptionalInt
(OptionalInt.empty()
) if this node cannot be converted to Javaint
.- Returns:
- Java
int
value this node represents, asOptionalInt
, if possible to accurately represent;OptionalInt.empty()
otherwise
-
asInt
public abstract int asInt()Method similar tointValue()
but in addition to coercing Number values (same asintValue()
), will also try to coerce a couple of additional types (or cases):- JSON Floating-point numbers with fractions (ones without fractions
are ok for
intValue()
) will be truncated toint
(if (and only if) they fit inint
range). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0
)) - POJO nodes that contain Number values
- Returns:
int
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted toint
- JSON Floating-point numbers with fractions (ones without fractions
are ok for
-
asInt
public abstract int asInt(int defaultValue) Method similar tointValue()
, but that will return specifieddefaultValue
if this node cannot be converted toint
.- Parameters:
defaultValue
- Value to return if this node cannot be converted toint
- Returns:
int
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asIntOpt
Method similar toasInt()
, but that will return (OptionalInt.empty()
) if this node cannot be coerced toint
.- Returns:
OptionalInt
value this node represents, if possible to accurately represent;OptionalInt.empty()
otherwise
-
longValue
public abstract long longValue()Method that will try to access value of this node as a Javalong
: but if node value cannot be expressed exactly as along
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Integer values that fit in Java 64-bit signed
long
range - JSON Floating-point values that fit in Java 64-bit signed
long
range AND do not have fractional part.
NOTE: for more lenient conversions, use
asLong()
- Returns:
Long
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to Javalong
- JSON Integer values that fit in Java 64-bit signed
-
longValue
public abstract long longValue(long defaultValue) Method similar tolongValue()
, but that will return specifieddefaultValue
if this node cannot be converted tolong
.- Parameters:
defaultValue
- Value to return if this node cannot be converted tolong
- Returns:
long
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
longValueOpt
Method similar tolongValue()
, but that will return emptyOptionalLong
(OptionalLong.empty()
) if this node cannot be converted to Javalong
.- Returns:
- Java
long
value this node represents, asOptionalLong
, if possible to accurately represent;OptionalLong.empty()
otherwise
-
asLong
public abstract long asLong()Method similar tolongValue()
but in addition to coercing Number values (same aslongValue()
), will also try to coerce a couple of additional types (or cases):- JSON Floating-point numbers with fractions (ones without fractions
are ok for
longValue()
) will be truncated tolong
(if (and only if) they fit inlong
range). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0
)) - POJO nodes that contain Number values
- Returns:
long
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted tolong
- JSON Floating-point numbers with fractions (ones without fractions
are ok for
-
asLong
public abstract long asLong(long defaultValue) Method similar toasLong()
, but that will return specifieddefaultValue
if this node cannot be coerced tolong
(instead of throwing an exception).- Parameters:
defaultValue
- Value to return if this node cannot be coerced tolong
- Returns:
long
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asLongOpt
Method similar toasLong()
, but that will return (OptionalLong.empty()
) if this node cannot be coerced tolong
.- Returns:
OptionalLong
value this node represents (or can be coerced to),OptionalLong.empty()
otherwise
-
bigIntegerValue
Method that will try to access value of this node as aBigInteger
, but if node value cannot be expressed exactly as aBigInteger
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Integer values
- JSON Floating-point values that do not have fractional part.
- Returns:
BigInteger
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to JavaBigInteger
-
bigIntegerValue
Method similar tobigIntegerValue()
, but that will return specifieddefaultValue
if this node cannot be converted to JavaBigInteger
.- Parameters:
defaultValue
- Value to return if this node cannot be converted to JavaBigInteger
- Returns:
- Java
BigInteger
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
bigIntegerValueOpt
Method similar tobigIntegerValue()
, but that will return empty (Optional.empty()
) if this node cannot be converted to JavaBigInteger
.- Returns:
- Java
BigInteger
value this node represents, asOptional<BigInteger>
, if possible to accurately represent;Optional.empty()
otherwise
-
asBigInteger
Method similar tobigIntegerValue()
but in addition to coercing Number values (same asbigIntegerValue()
), will also try to coerce a couple of additional types (or cases):- JSON Floating-point numbers with fractions (ones without fractions
are ok for
bigIntegerValue()
) will be truncated to integer value (like withBigDecimal.toBigInteger()
)) - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0
)) - POJO nodes that contain Number values
- Returns:
BigInteger
value this node represents, if possible to accurately convert;defaultValue
otherwise
- JSON Floating-point numbers with fractions (ones without fractions
are ok for
-
asBigInteger
Method similar toasBigInteger()
, but that will return specifieddefaultValue
if this node cannot be converted toBigInteger
.- Parameters:
defaultValue
- Value to return if this node cannot be converted toBigInteger
- Returns:
BigInteger
value this node represents, if possible to accurately convert;defaultValue
otherwise
-
asBigIntegerOpt
Method similar tobigIntegerValue()
, but that will return empty (Optional.empty()
) if this node cannot be converted to JavaBigInteger
.- Returns:
BigInteger
value this node represents, asOptional<BigInteger>
, if possible to accurately represent;Optional.empty()
otherwise.
-
floatValue
public abstract float floatValue()Method that will try to access value of this node as a Javafloat
: but if node value cannot be expressed exactly as afloat
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Floating-point values that fit in Java 32-bit
double
range - JSON Integer values that fit in Java 32-bit
double
range
- Returns:
Float
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to Javafloat
- JSON Floating-point values that fit in Java 32-bit
-
floatValue
public abstract float floatValue(float defaultValue) Method similar tofloatValue()
, but that will return specifieddefaultValue
if this node cannot be converted tofloat
.- Parameters:
defaultValue
- Value to return if this node cannot be converted tofloat
- Returns:
float
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
floatValueOpt
Method similar tofloatValue()
, but that will return emptyOptional<Float>
(Optional.empty()
) if this node cannot be converted to Javafloat
.- Returns:
- Java
float
value this node represents, asOptional<Float>
, if possible to accurately represent;Optional.empty()
otherwise
-
asFloat
public abstract float asFloat()Method similar tofloatValue()
but in addition to coercing Number values will also try coerce couple of additional types:- JSON String that represents JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0.0f
) - POJO nodes that contain Number values
- Returns:
float
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted tofloat
-
asFloat
public abstract float asFloat(float defaultValue) Method similar toasFloat()
, but that will returndefaultValue
if this node cannot be coerced tofloat
.- Returns:
float
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asFloatOpt
Method similar toasFloat()
, but that will return (Optional.empty()
) if this node cannot be coerced tofloat
.- Returns:
Optional<Float>
value this node represents, if possible to accurately represent;Optional.empty()
otherwise
-
doubleValue
public abstract double doubleValue()Method that will try to access value of this node as adouble
: but if node value cannot be expressed exactly as adouble
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Floating-point values that fit in Java 64-bit
double
range - JSON Integer values that fit in Java 64-bit
double
range
NOTE: for more lenient conversions, use
asDouble()
- Returns:
Double
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted todouble
- JSON Floating-point values that fit in Java 64-bit
-
doubleValue
public abstract double doubleValue(double defaultValue) Method similar todoubleValue()
, but that will return specifieddefaultValue
if this node cannot be converted todouble
.- Parameters:
defaultValue
- Value to return if this node cannot be converted todouble
- Returns:
double
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
doubleValueOpt
Method similar todoubleValue()
, but that will return emptyOptionalLong
(OptionalDouble.empty()
) if this node cannot be converted to Javadouble
.- Returns:
- Java
double
value this node represents, asOptionalDouble
, if possible to accurately represent;OptionalDouble.empty()
otherwise
-
asDouble
public abstract double asDouble()Method similar todoubleValue()
but in addition to coercing Number values will also try coerce couple of additional types:- JSON String that represents JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0.0d
) - POJO nodes that contain Number values
- Returns:
double
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted todouble
-
asDouble
public abstract double asDouble(double defaultValue) Method similar toasDouble()
, but that will returndefaultValue
if this node cannot be coerced todouble
.- Returns:
double
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asDoubleOpt
Method similar toasDouble()
, but that will return (OptionalDouble.empty()
) if this node cannot be coerced todouble
.- Returns:
OptionalDouble
value this node represents, if possible to accurately represent;OptionalDouble.empty()
otherwise
-
decimalValue
Method that will try to access value of this node as a JavaBigDecimal
: but if node value cannot be expressed exactly as aBigDecimal
, aJsonNodeException
will be thrown. Access works for following cases:- All JSON Number values
NOTE: for more lenient conversions, use
asDecimal()
- Returns:
BigDecimal
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted to JavaBigDecimal
-
decimalValue
Method similar todecimalValue()
, but that will returndefaultValue
if this node cannot be coerced to JavaBigDecimal
.- Returns:
BigDecimal
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
decimalValueOpt
Method similar todecimalValue()
, but that will return emptyOptional
(Optional.empty()
) if this node cannot be coerced toBigDecimal
.- Returns:
- Java
BigDecimal
value this node represents, asOptional<BigDecimal>
, if possible to accurately represent;Optional.empty()
otherwise
-
asDecimal
Method that will try to access value of this node as aBigDecimal
: but if node value cannot be expressed exactly as aBigDecimal
, aJsonNodeException
will be thrown. Access works for following cases:- JSON Floating-point values (but not "NaN" or "Infinity")
- JSON Integer values
- JSON String that represents JSON Numbers ("stringified" numbers)
- JSON Null (converted to
BigDecimal.ZERO
)) - POJO nodes that contain Number values
- Returns:
BigDecimal
value this node represents, if possible to accurately represent- Throws:
JsonNodeException
- if node value cannot be converted toBigDecimal
-
asDecimal
Method similar toasDecimal()
, but that will returndefaultValue
if this node cannot be coerced to JavaBigDecimal
.- Returns:
BigDecimal
value this node represents, if possible to accurately represent;defaultValue
otherwise
-
asDecimalOpt
Method similar toasDecimal()
, but that will return emptyOptional
(Optional.empty()
) if this node cannot be coerced toBigDecimal
.- Returns:
- Java
BigDecimal
value this node represents, asOptional<BigDecimal>
, if possible to accurately represent;Optional.empty()
otherwise
-
require
Method that may be called to verify thatthis
node is NOT so-called "missing node": that is, one for whichisMissingNode()
returnstrue
. If not missing node,this
is returned to allow chaining; otherwise exception is thrown.- Returns:
this
node to allow chaining- Throws:
IllegalArgumentException
- if this node is "missing node"
-
requireNonNull
Method that may be called to verify thatthis
node is neither so-called "missing node" (that is, one for whichisMissingNode()
returnstrue
) nor "null node" (one for whichisNull()
returnstrue
). If non-null non-missing node,this
is returned to allow chaining; otherwise exception is thrown.- Returns:
this
node to allow chaining- Throws:
IllegalArgumentException
- if this node is either "missing node" or "null node"
-
required
Method is functionally equivalent topath(propertyName).required()
and can be used to check that this node is anObjectNode
(that is, represents JSON Object value) and has value for specified property with keypropertyName
(but note that value may be explicit JSON null value). If this node is Object Node and has value for specified property, matching value is returned; otherwiseIllegalArgumentException
is thrown.- Parameters:
propertyName
- Name of property to access- Returns:
- Value of the specified property of this Object node
- Throws:
IllegalArgumentException
- if this node is not an Object node or if it does not have value for specified property
-
required
Method is functionally equivalent topath(index).required()
and can be used to check that this node is anArrayNode
(that is, represents JSON Array value) and has value for specifiedindex
(but note that value may be explicit JSON null value). If this node is Array Node and has value for specified index, value at index is returned; otherwiseIllegalArgumentException
is thrown.- Parameters:
index
- Index of the value of this Array node to access- Returns:
- Value at specified index of this Array node
- Throws:
IllegalArgumentException
- if this node is not an Array node or if it does not have value for specified index
-
requiredAt
Method is functionally equivalent toat(pathExpr).required()
and can be used to check that there is an actual value node at specifiedJsonPointer
starting fromthis
node (but note that value may be explicit JSON null value). If such value node exists it is returned; otherwiseIllegalArgumentException
is thrown.- Parameters:
pathExpr
-JsonPointer
expression (as String) to use for finding value node- Returns:
- Matching value node for given expression
- Throws:
IllegalArgumentException
- if no value node exists at givenJSON Pointer
path
-
requiredAt
Method is functionally equivalent toat(path).required()
and can be used to check that there is an actual value node at specifiedJsonPointer
starting fromthis
node (but note that value may be explicit JSON null value). If such value node exists it is returned; otherwiseIllegalArgumentException
is thrown.- Parameters:
path
-JsonPointer
expression to use for finding value node- Returns:
- Matching value node for given expression
- Throws:
IllegalArgumentException
- if no value node exists at givenJSON Pointer
path
-
has
Method that allows checking whether this node is JSON Object node and contains value for specified property. If this is the case (including properties with explicit null values), returns true; otherwise returns false.This method is equivalent to:
node.get(propertyName) != null
(since return value of get() is node, not value node contains)NOTE: when explicit
null
values are added, this method will returntrue
for such properties.- Parameters:
propertyName
- Name of element to check- Returns:
- True if this node is a JSON Object node, and has a property entry with specified name (with any value, including null value)
-
has
public boolean has(int index) Method that allows checking whether this node is JSON Array node and contains a value for specified index If this is the case (including case of specified indexing having null as value), returns true; otherwise returns false.Note: array element indexes are 0-based.
This method is equivalent to:
node.get(index) != null
NOTE: this method will return
true
for explicitly added null values.- Parameters:
index
- Index to check- Returns:
- True if this node is a JSON Object node, and has a property entry with specified name (with any value, including null value)
-
hasNonNull
Method that is similar tohas(String)
, but that will returnfalse
for explicitly added nulls.This method is functionally equivalent to:
node.get(propertyName) != null && !node.get(propertyName).isNull()
-
hasNonNull
public boolean hasNonNull(int index) Method that is similar tohas(int)
, but that will returnfalse
for explicitly added nulls.This method is equivalent to:
node.get(index) != null && !node.get(index).isNull()
-
iterator
Implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs. -
spliterator
- Specified by:
spliterator
in interfaceIterable<JsonNode>
-
values
Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node. In case of Object node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator. -
properties
Accessor that will return properties ofObjectNode
similar to howMap.entrySet()
works; for other node types will return emptySet
.- Returns:
- Set of properties, if this node is an
ObjectNode
(isObject()
returnstrue
); emptySet
otherwise.
-
valueStream
Returns a stream of all value nodes of this Node, iff this node is anArrayNode
orObjectNode
. In case ofObject
node, property names (keys) are not included, only values. For other types of nodes, returns empty stream. -
propertyStream
Returns a stream of all properties (key, value pairs) of this Node, iff this node is an anObjectNode
. For other types of nodes, returns empty stream. -
forEachEntry
If this node is anObjectNode
, performs the given action for each property (key, value pair) until all entries have been processed or the action throws an exception. Exceptions thrown by the action are relayed to the caller. For other node types, no action is performed.Actions are performed in the order of properties, same as order returned by method
properties()
. This is generally the document order of properties in JSON object.- Parameters:
action
- Action to perform for each entry
-
findValue
Method for finding the first JSON Object field with specified name in this node or its child nodes, and returning value it has. If no matching field is found in this node or its descendants, returns null.Note that traversal is done in document order (that is, order in which nodes are iterated if using
values()
)- Parameters:
propertyName
- Name of field to look for- Returns:
- Value of first matching node found, if any; null if none
-
findValues
Method for finding JSON Object fields with specified name -- both immediate child values and descendants -- and returning found ones as aList
. Note that sub-tree search ends when matching field is found, so possible children of result nodes are not included. If no matching fields are found in this node or its descendants, returns an empty List.- Parameters:
propertyName
- Name of field to look for
-
findValuesAsString
Similar tofindValues(java.lang.String)
, but will additionally convert values into Strings, callingasText()
. -
findPath
Method similar tofindValue(java.lang.String)
, but that will return a "missing node" instead of null if no field is found. Missing node is a specific kind of node for whichisMissingNode()
returns true; and all value access methods return empty or missing value.- Parameters:
propertyName
- Name of field to look for- Returns:
- Value of first matching node found; or if not found, a "missing node" (non-null instance that has no value)
-
findParent
Method for finding a JSON Object that contains specified field, within this node or its descendants. If no matching field is found in this node or its descendants, returns null.- Parameters:
propertyName
- Name of field to look for- Returns:
- Value of first matching node found, if any; null if none
-
findParents
Method for finding a JSON Object that contains specified field, within this node or its descendants. If no matching field is found in this node or its descendants, returns null.- Parameters:
propertyName
- Name of field to look for- Returns:
- Value of first matching node found, if any; null if none
-
findValues
-
findValuesAsString
-
findParents
-
withObject
Method that works in one of possible ways, depending on whetherexprOrProperty
is a validJsonPointer
expression or not (valid expression is either empty String""
or starts with leading slash/
character). If it is, works as a short-cut to:withObject(JsonPointer.compile(exprOrProperty));
If it is NOT a validJsonPointer
expression, value is taken as a literal Object property name and calls is alias forwithObjectProperty(exprOrProperty);
- Parameters:
exprOrProperty
-JsonPointer
expression to use (if valid as one), or, if not (no leading "/"), property name to match.- Returns:
ObjectNode
found or created
-
withObject
public final ObjectNode withObject(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Short-cut equivalent to:withObject(JsonPointer.compile(expr), overwriteMode, preferIndex);
-
withObject
Same aswithObject(JsonPointer, OverwriteMode, boolean)
but with defaults ofOvewriteMode#NULLS
(overwrite mode) andtrue
forpreferIndex
(that is, will try to considerJsonPointer
segments index if at all possible and only secondarily as property name- Parameters:
ptr
-JsonPointer
that indicates path to use for Object value to return (potentially creating as necessary)- Returns:
ObjectNode
found or created
-
withObject
public ObjectNode withObject(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Method that can be called on Object or Array nodes, to access a Object-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it. For example, on document{ "a" : { "b" : { "c" : 13 } } }
calling method withJsonPointer
of/a/b
would returnObjectNode
{ "c" : 13 }
In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointer
of/a/x
an emptyObjectNode
would be returned and the document would look like:{ "a" : { "b" : { "c" : 13 }, "x" : { } } }
Finally, if the path is incompatible with the document -- there is an existingJsonNode
through which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteMode
passed in. For example, with above document and expression of/a/b/c
, conversion is allowed if passingOverwriteMode.SCALARS
orOvewriteMode.ALL
, and resulting document would look like:{ "a" : { "b" : { "c" : { } }, "x" : { } } }
but if different modes (NONE
orNULLS
) is passed, an exception is thrown instead.- Parameters:
ptr
- Pointer that indicates path to use forObjectNode
value to return (potentially creating one as necessary)overwriteMode
- Defines which node types may be converted in case of incompatibleJsonPointer
expression: if conversion not allowed,UnsupportedOperationException
is thrown.preferIndex
- When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like3
), whether to construct anArrayNode
(true
) orObjectNode
(false
). In latter case matching property with quoted number (like"3"
) is used within Object.- Returns:
ObjectNode
found or created- Throws:
JsonNodeException
- if a conversion would be needed for givenJsonPointer
, document, but was not allowed for the type encountered
-
withObjectProperty
Method similar towithObject(JsonPointer, OverwriteMode, boolean)
-- basically short-cut to:withObject(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
that is, only matches immediate property onObjectNode
and will either use an existingObjectNode
that is value of the property, or create one if no value or value isNullNode
.
Will fail with an exception if:- Node method called on is NOT
ObjectNode
- Property has an existing value that is NOT
NullNode
(explicitnull
)
- Parameters:
propName
- Name of property that has or will haveObjectNode
as value- Returns:
ObjectNode
value of given property (existing or created)
- Node method called on is NOT
-
withArray
Short-cut equivalent to:withArray(JsonPointer.compile(expr), overwriteMode, preferIndex);
-
withArray
Short-cut equivalent to:withArray(JsonPointer.compile(expr), overwriteMode, preferIndex);
-
withArray
Same aswithArray(JsonPointer, OverwriteMode, boolean)
but with defaults ofOvewriteMode#NULLS
(overwrite mode) andtrue
forpreferIndex
. -
withArray
public ArrayNode withArray(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Method that can be called on Object or Array nodes, to access a Array-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it. For example, on document{ "a" : { "b" : [ 1, 2 ] } }
calling method withJsonPointer
of/a/b
would returnArray
[ 1, 2 ]
In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointer
of/a/x
an emptyArrayNode
would be returned and the document would look like:{ "a" : { "b" : [ 1, 2 ], "x" : [ ] } }
Finally, if the path is incompatible with the document -- there is an existingJsonNode
through which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteMode
passed in. For example, with above document and expression of/a/b/0
, conversion is allowed if passingOverwriteMode.SCALARS
orOvewriteMode.ALL
, and resulting document would look like:{ "a" : { "b" : [ [ ], 2 ], "x" : [ ] } }
but if different modes (NONE
orNULLS
) is passed, an exception is thrown instead.- Parameters:
ptr
- Pointer that indicates path to use forArrayNode
value to return (potentially creating it as necessary)overwriteMode
- Defines which node types may be converted in case of incompatibleJsonPointer
expression: if conversion not allowed, an exception is thrown.preferIndex
- When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like3
), whether to construct anArrayNode
(true
) orObjectNode
(false
). In latter case matching property with quoted number (like"3"
) is used within Object.- Returns:
ArrayNode
found or created- Throws:
JsonNodeException
- if a conversion would be needed for givenJsonPointer
, document, but was not allowed for the type encountered
-
withArrayProperty
Method similar towithArray(JsonPointer, OverwriteMode, boolean)
-- basically short-cut to:withArray(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
that is, only matches immediate property onObjectNode
and will either use an existingArrayNode
that is value of the property, or create one if no value or value isNullNode
.
Will fail with an exception if:- Node method called on is NOT
ObjectNode
- Property has an existing value that is NOT
NullNode
(explicitnull
)
- Node method called on is NOT
-
equals
Entry method for invoking customizable comparison, using passed-inComparator
object. Nodes will handle traversal of structured types (arrays, objects), but defer to comparator for scalar value comparisons. If a "natural"Comparator
is passed -- one that simply callsequals()
on one of arguments, passing the other -- implementation is the same as directly callingequals()
on node.Default implementation simply delegates to passed in
comparator
, withthis
as the first argument, andother
as the second argument.- Parameters:
comparator
- Object called to compare two scalarJsonNode
instances, and return either 0 (are equals) or non-zero (not equal)
-
toString
Method that will produce (as of Jackson 2.10) valid JSON using default settings of databind, as String. If you want other kinds of JSON output (or output formatted using one of other Jackson-supported data formats) make sure to useObjectMapper
orObjectWriter
to serialize an instance, for example:String json = objectMapper.writeValueAsString(rootNode);
Note: method defined as abstract to ensure all implementation classes explicitly implement method, instead of relying on
Object.toString()
definition. -
toPrettyString
Alternative totoString()
that will serialize this node using Jackson default pretty-printer. -
equals
Equality for node objects is defined as full (deep) value equality. This means that it is possible to compare complete JSON trees for equality by comparing equality of root nodes.Note: marked as abstract to ensure all implementation classes define it properly and not rely on definition from
Object
. -
_this
-
_reportRequiredViolation
Helper method that throwsDatabindException
as a result of violating "required-constraint" for this node (forrequired(java.lang.String)
or related methods). -
_reportUnsupportedOperation
-
asString()
instead.