- 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 enumConfiguration 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,nullis returned.protected <T> T_reportRequiredViolation(String msgTemplate, Object... args) Helper method that throwsDatabindExceptionas 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 BigIntegerMethod 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 BigIntegerasBigInteger(BigInteger defaultValue) Method similar toasBigInteger(), but that will return specifieddefaultValueif 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 booleanMethod that will try to convert value of this node to a Javaboolean.abstract booleanasBoolean(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 BigDecimalMethod that will try to access value of this node as aBigDecimal: but if node value cannot be expressed exactly as aBigDecimal, aJsonNodeExceptionwill be thrown.abstract BigDecimalasDecimal(BigDecimal defaultValue) Method similar toasDecimal(), but that will returndefaultValueif 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 doubleasDouble()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 doubleasDouble(double defaultValue) Method similar toasDouble(), but that will returndefaultValueif this node cannot be coerced todouble.abstract OptionalDoubleMethod similar toasDouble(), but that will return (OptionalDouble.empty()) if this node cannot be coerced todouble.abstract floatasFloat()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 floatasFloat(float defaultValue) Method similar toasFloat(), but that will returndefaultValueif this node cannot be coerced tofloat.Method similar toasFloat(), but that will return (Optional.empty()) if this node cannot be coerced tofloat.abstract intasInt()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 inintrange).abstract intasInt(int defaultValue) Method similar tointValue(), but that will return specifieddefaultValueif this node cannot be converted toint.abstract OptionalIntasIntOpt()Method similar toasInt(), but that will return (OptionalInt.empty()) if this node cannot be coerced toint.abstract longasLong()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 inlongrange).abstract longasLong(long defaultValue) Method similar toasLong(), but that will return specifieddefaultValueif this node cannot be coerced tolong(instead of throwing an exception).abstract OptionalLongMethod similar toasLong(), but that will return (OptionalLong.empty()) if this node cannot be coerced tolong.abstract shortasShort()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 inshortrange).abstract shortasShort(short defaultValue) Method similar toshortValue(), but that will return specifieddefaultValueif this node cannot be converted toshort.Method similar toasShort(), but that will return (Optional.empty()) if this node cannot be coerced toshort.abstract StringasString()Method that will try to convert value of this node to aString.abstract StringSimilar 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 StringasText()Deprecated.Deprecated.UseasString(String)instead.final JsonNodeConvenience method that is functionally equivalent to:final JsonNodeat(JsonPointer ptr) Method for locating node specified by given JSON pointer instances.abstract BigIntegerMethod that will try to access value of this node as aBigInteger, but if node value cannot be expressed exactly as aBigInteger, aJsonNodeExceptionwill be thrown.abstract BigIntegerbigIntegerValue(BigInteger defaultValue) Method similar tobigIntegerValue(), but that will return specifieddefaultValueif 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, aJsonNodeExceptionwill be thrown.abstract booleanMethod that will try to access value of this node as a Javabooleanwhich works if (and only if) node contains JSON boolean value: if not, aJsonNodeExceptionwill be thrown.abstract booleanbooleanValue(boolean defaultValue) Method similar tobooleanValue(), but that will return specifieddefaultValueif 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.booleanMethod that can be used to check whether contained value is numeric (returns true forisNumber()) and can be converted without loss to integral number (specifically,BigIntegerbut potentially others, seecanConvertToInt()andcanConvertToInt()).booleanMethod 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,intand if it is a floating-point number, it does not have fractional part).booleanMethod 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,longand if it is a floating-point number, it does not have fractional part).booleanMethod 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,shortand if it is a floating-point number, it does not have fractional part).abstract BigDecimalMethod that will try to access value of this node as a JavaBigDecimal: but if node value cannot be expressed exactly as aBigDecimal, aJsonNodeExceptionwill be thrown.abstract BigDecimaldecimalValue(BigDecimal defaultValue) Method similar todecimalValue(), but that will returndefaultValueif 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 JsonNodedeepCopy()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 doubleMethod that will try to access value of this node as adouble: but if node value cannot be expressed exactly as adouble, aJsonNodeExceptionwill be thrown.abstract doubledoubleValue(double defaultValue) Method similar todoubleValue(), but that will return specifieddefaultValueif this node cannot be converted todouble.abstract OptionalDoubleMethod similar todoubleValue(), but that will return emptyOptionalLong(OptionalDouble.empty()) if this node cannot be converted to Javadouble.abstract booleanEquality for node objects is defined as full (deep) value equality.booleanequals(Comparator<JsonNode> comparator, JsonNode other) Entry method for invoking customizable comparison, using passed-inComparatorobject.abstract JsonNodefindParent(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 JsonNodeMethod similar tofindValue(java.lang.String), but that will return a "missing node" instead of null if no field is found.abstract JsonNodeMethod 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 floatMethod that will try to access value of this node as a Javafloat: but if node value cannot be expressed exactly as afloat, aJsonNodeExceptionwill be thrown.abstract floatfloatValue(float defaultValue) Method similar tofloatValue(), but that will return specifieddefaultValueif 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.voidforEachEntry(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 JsonNodeget(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 JsonNodeTypeReturn the type of this nodebooleanhas(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.booleanMethod that allows checking whether this node is JSON Object node and contains value for specified property.booleanhasNonNull(int index) Method that is similar tohas(int), but that will returnfalsefor explicitly added nulls.booleanhasNonNull(String propertyName) Method that is similar tohas(String), but that will returnfalsefor explicitly added nulls.abstract intintValue()Method that will try to access value of this node as a Javaint: but if node value cannot be expressed exactly as anint, aJsonNodeExceptionwill be thrown.abstract intintValue(int defaultValue) Method similar tointValue(), but that will return specifieddefaultValueif this node cannot be converted to Javaint.abstract OptionalIntMethod similar tointValue(), but that will return emptyOptionalInt(OptionalInt.empty()) if this node cannot be converted to Javaint.booleanisArray()booleanbooleanfinal booleanisBinary()Method that can be used to check if this node represents binary data (Base64 encoded).final booleanMethod that can be used to check if this node was created from JSON boolean value (literals "true" and "false").booleanbooleanisDouble()booleanisEmpty()Convenience method that is functionally same as:booleanisFloat()booleanbooleanisInt()Method that can be used to check whether contained value is a number represented as Javaint.booleanbooleanisLong()Method that can be used to check whether contained value is a number represented as Javalong.booleanfinal booleanisNull()Method that can be used to check if this node was created from JSON literal null value.final booleanisNumber()booleanisObject()final booleanisPojo()Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean".booleanisShort()Method that can be used to check whether contained value is a number represented as Javashort.final booleanisString()Method that checks whether this node represents JSON String value.booleanDeprecated.UseisString()instead.final booleaniterator()Implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs.abstract longMethod that will try to access value of this node as a Javalong: but if node value cannot be expressed exactly as along, aJsonNodeExceptionwill be thrown.abstract longlongValue(long defaultValue) Method similar tolongValue(), but that will return specifieddefaultValueif this node cannot be converted tolong.abstract OptionalLongMethod similar tolongValue(), but that will return emptyOptionalLong(OptionalLong.empty()) if this node cannot be converted to Javalong.abstract NumberMethod that will try to access value of this node asNumberthat accurately represents its value, if (and only if) this is a number node (returnstrueforisNumber()).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 JsonNodepath(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 JsonNodeThis 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 ofObjectNodesimilar 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 thatthisnode is NOT so-called "missing node": that is, one for whichisMissingNode()returnstrue.abstract JsonNoderequired(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 JsonNodeMethod 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 specifiedJsonPointerstarting fromthisnode (but note that value may be explicit JSON null value).final JsonNoderequiredAt(JsonPointer path) Method is functionally equivalent toat(path).required()and can be used to check that there is an actual value node at specifiedJsonPointerstarting fromthisnode (but note that value may be explicit JSON null value).<T extends JsonNode>
TMethod that may be called to verify thatthisnode is neither so-called "missing node" (that is, one for whichisMissingNode()returnstrue) nor "null node" (one for whichisNull()returnstrue).abstract shortMethod 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, aJsonNodeExceptionwill be thrown.abstract shortshortValue(short defaultValue) Method similar toshortValue(), but that will return specifieddefaultValueif 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.intsize()final Spliterator<JsonNode>abstract StringMethod that will try to access value of this node as a JavaStringwhich works if (and only if) node contains JSON String ornullvalue: if not, aJsonNodeExceptionwill be thrown.abstract StringstringValue(String defaultValue) Method similar tostringValue(), but that will return specifieddefaultValueif 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 (NOTE: JSON null is not considered a String here)final StringDeprecated.UseasString()instead.Alternative totoString()that will serialize this node using Jackson default pretty-printer.abstract StringtoString()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 anArrayNodeorObjectNode.Short-cut equivalent to:withArray(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Short-cut equivalent to:final ArrayNodewithArray(JsonPointer ptr) Same aswithArray(JsonPointer, OverwriteMode, boolean)but with defaults ofOvewriteMode#NULLS(overwrite mode) andtrueforpreferIndex.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 whetherexprOrPropertyis a validJsonPointerexpression or not (valid expression is either empty String""or starts with leading slash/character).final ObjectNodewithObject(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Short-cut equivalent to:final ObjectNodewithObject(JsonPointer ptr) Same aswithObject(JsonPointer, OverwriteMode, boolean)but with defaults ofOvewriteMode#NULLS(overwrite mode) andtrueforpreferIndex(that is, will try to considerJsonPointersegments 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
isEmptyMethods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface tools.jackson.databind.JacksonSerializable
serialize, serializeWithTypeMethods 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() == 0for all node types. -
isValueNode
public final boolean isValueNode()- Specified by:
isValueNodein interfaceTreeNode
-
isContainer
public boolean isContainer()- Specified by:
isContainerin interfaceTreeNode
-
isMissingNode
public boolean isMissingNode()- Specified by:
isMissingNodein 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!), aNullNodewill 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!), aNullNodewill 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!), aNullNodewill 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, emptyOptionalis returned.NOTE: if the property value has been explicitly set as
null(which is different from removal!), an Optional containingNullNodewill 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:
propertyNamesin 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:
atin 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
JsonPointerinstance once and reuse it: this method will not perform any caching of compiled expressions.- Specified by:
atin interfaceTreeNode- Parameters:
jsonPtrExpr- Expression to compile as aJsonPointerinstance- 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,nullis returned.- Parameters:
ptr- Path expression to use- Returns:
- Either matching
JsonNodefor the first step of path ornullif no match (including case that this node is not a container)
-
getNodeType
Return the type of this node- Returns:
- the node type as a
JsonNodeTypeenum 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,shortand 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,intand 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,longand 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,BigIntegerbut 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 ofdoubleandfloatwill returnfalseas they cannot 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 aJsonNodewrapped in Java'sOptional. All nodes except ofMissingNodewill return non-emptyOptional;MissingNodewill 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 JavaStringwhich works if (and only if) node contains JSON String ornullvalue: if not, aJsonNodeExceptionwill be thrown. In case of JSONnull, Javanullis returned.NOTE: for more conversions, use
asString()instead.NOTE: in Jackson 2.x, this method was named
textValue().- Returns:
Stringvalue this node represents (if JSON String),nullfor JSONnull- Throws:
JsonNodeException- if node value is not a JSON String or Null value
-
stringValue
Method similar tostringValue(), but that will return specifieddefaultValueif this node does not contain a JSON String. This default value case includes JSONnull.- Parameters:
defaultValue- Value to return if this node does not contain a JSON String.- Returns:
- Java
Stringvalue this node represents (if JSON String);defaultValueotherwise -- only returnsnullifdefaultValueisnull
-
stringValueOpt
Method similar tostringValue(), but that will returnOptional.empty()if this node does not contain a JSON String (NOTE: JSON null is not considered a String here)- Returns:
Optional<String>value (if node represents JSON String);Optional.empty()otherwise (including for JSON null)
-
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); JSONnulls map to empty String. Other values (including structured types like Objects and Arrays, and "missing" value) will result in aJsonNodeExceptionbeing thrown.NOTE: this is NOT same as
toString()in that result isNOT VALID ENCODED JSON
for all nodes (although is for some, likeNumberNodes andBooleanNodes).- 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, aJsonNodeExceptionwill 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 Javabooleanwhich works if (and only if) node contains JSON boolean value: if not, aJsonNodeExceptionwill be thrown.NOTE: for more lenient conversions, use
asBoolean()- Returns:
booleanvalue 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 specifieddefaultValueif this node does not contain a JSON boolean.- Parameters:
defaultValue- Value to return if this node does not contain a JSON boolean.- Returns:
- Java
booleanvalue this node represents (if JSON boolean);defaultValueotherwise
-
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;nullmaps to false and Strings 'true' and 'false' map to corresponding values. Other values (including structured types like Objects and Arrays) will result in aJsonNodeExceptionbeing 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 asNumberthat accurately represents its value, if (and only if) this is a number node (returnstrueforisNumber()). If this node is NOT a number node, aJsonNodeExceptionwill 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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Integer values that fit in Java 16-bit signed
shortrange - JSON Floating-point values that fit in Java 16-bit signed
shortrange AND do not have fractional part.
- Returns:
Shortvalue 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 specifieddefaultValueif this node cannot be converted to Javashort.- Parameters:
defaultValue- Value to return if this node cannot be converted to Javashort- Returns:
- Java
shortvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
shortValueOpt
Method similar toshortValue(), but that will return emptyOptional<Short>(Optional.empty()) if this node cannot be converted to Javashort.- Returns:
- Java
shortvalue 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 inshortrange). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0)) - POJO nodes that contain Number values
- Returns:
shortvalue 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 specifieddefaultValueif this node cannot be converted toshort.- Parameters:
defaultValue- Value to return if this node cannot be converted toshort- Returns:
shortvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Integer values that fit in Java 32-bit signed
intrange - JSON Floating-point values that fit in Java 32-bit signed
intrange AND do not have fractional part.
NOTE: for more lenient conversions, use
asInt()- Returns:
Intvalue 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 specifieddefaultValueif this node cannot be converted to Javaint.- Parameters:
defaultValue- Value to return if this node cannot be converted to Javaint- Returns:
- Java
intvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
intValueOpt
Method similar tointValue(), but that will return emptyOptionalInt(OptionalInt.empty()) if this node cannot be converted to Javaint.- Returns:
- Java
intvalue 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 inintrange). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0)) - POJO nodes that contain Number values
- Returns:
intvalue 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 specifieddefaultValueif this node cannot be converted toint.- Parameters:
defaultValue- Value to return if this node cannot be converted toint- Returns:
intvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
asIntOpt
Method similar toasInt(), but that will return (OptionalInt.empty()) if this node cannot be coerced toint.- Returns:
OptionalIntvalue 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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Integer values that fit in Java 64-bit signed
longrange - JSON Floating-point values that fit in Java 64-bit signed
longrange AND do not have fractional part.
NOTE: for more lenient conversions, use
asLong()- Returns:
Longvalue 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 specifieddefaultValueif this node cannot be converted tolong.- Parameters:
defaultValue- Value to return if this node cannot be converted tolong- Returns:
longvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
longValueOpt
Method similar tolongValue(), but that will return emptyOptionalLong(OptionalLong.empty()) if this node cannot be converted to Javalong.- Returns:
- Java
longvalue 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 inlongrange). - JSON Strings that represent JSON Numbers ("stringified" numbers)
- JSON Null (converted to
0)) - POJO nodes that contain Number values
- Returns:
longvalue 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 specifieddefaultValueif this node cannot be coerced tolong(instead of throwing an exception).- Parameters:
defaultValue- Value to return if this node cannot be coerced tolong- Returns:
longvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
asLongOpt
Method similar toasLong(), but that will return (OptionalLong.empty()) if this node cannot be coerced tolong.- Returns:
OptionalLongvalue 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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Integer values
- JSON Floating-point values that do not have fractional part.
- Returns:
BigIntegervalue 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 specifieddefaultValueif this node cannot be converted to JavaBigInteger.- Parameters:
defaultValue- Value to return if this node cannot be converted to JavaBigInteger- Returns:
- Java
BigIntegervalue this node represents, if possible to accurately represent;defaultValueotherwise
-
bigIntegerValueOpt
Method similar tobigIntegerValue(), but that will return empty (Optional.empty()) if this node cannot be converted to JavaBigInteger.- Returns:
- Java
BigIntegervalue 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:
BigIntegervalue this node represents, if possible to accurately convert;defaultValueotherwise
- JSON Floating-point numbers with fractions (ones without fractions
are ok for
-
asBigInteger
Method similar toasBigInteger(), but that will return specifieddefaultValueif this node cannot be converted toBigInteger.- Parameters:
defaultValue- Value to return if this node cannot be converted toBigInteger- Returns:
BigIntegervalue this node represents, if possible to accurately convert;defaultValueotherwise
-
asBigIntegerOpt
Method similar tobigIntegerValue(), but that will return empty (Optional.empty()) if this node cannot be converted to JavaBigInteger.- Returns:
BigIntegervalue 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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Floating-point values that fit in Java 32-bit
doublerange - JSON Integer values that fit in Java 32-bit
doublerange
- Returns:
Floatvalue 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 specifieddefaultValueif this node cannot be converted tofloat.- Parameters:
defaultValue- Value to return if this node cannot be converted tofloat- Returns:
floatvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
floatValueOpt
Method similar tofloatValue(), but that will return emptyOptional<Float>(Optional.empty()) if this node cannot be converted to Javafloat.- Returns:
- Java
floatvalue 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:
floatvalue 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 returndefaultValueif this node cannot be coerced tofloat.- Returns:
floatvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
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, aJsonNodeExceptionwill be thrown. Access works for following cases:- JSON Floating-point values that fit in Java 64-bit
doublerange - JSON Integer values that fit in Java 64-bit
doublerange
NOTE: for more lenient conversions, use
asDouble()- Returns:
Doublevalue 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 specifieddefaultValueif this node cannot be converted todouble.- Parameters:
defaultValue- Value to return if this node cannot be converted todouble- Returns:
doublevalue this node represents, if possible to accurately represent;defaultValueotherwise
-
doubleValueOpt
Method similar todoubleValue(), but that will return emptyOptionalLong(OptionalDouble.empty()) if this node cannot be converted to Javadouble.- Returns:
- Java
doublevalue 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:
doublevalue 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 returndefaultValueif this node cannot be coerced todouble.- Returns:
doublevalue this node represents, if possible to accurately represent;defaultValueotherwise
-
asDoubleOpt
Method similar toasDouble(), but that will return (OptionalDouble.empty()) if this node cannot be coerced todouble.- Returns:
OptionalDoublevalue 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, aJsonNodeExceptionwill be thrown. Access works for following cases:- All JSON Number values
NOTE: for more lenient conversions, use
asDecimal()- Returns:
BigDecimalvalue 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 returndefaultValueif this node cannot be coerced to JavaBigDecimal.- Returns:
BigDecimalvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
decimalValueOpt
Method similar todecimalValue(), but that will return emptyOptional(Optional.empty()) if this node cannot be coerced toBigDecimal.- Returns:
- Java
BigDecimalvalue 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, aJsonNodeExceptionwill 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:
BigDecimalvalue this node represents, if possible to accurately represent- Throws:
JsonNodeException- if node value cannot be converted toBigDecimal
-
asDecimal
Method similar toasDecimal(), but that will returndefaultValueif this node cannot be coerced to JavaBigDecimal.- Returns:
BigDecimalvalue this node represents, if possible to accurately represent;defaultValueotherwise
-
asDecimalOpt
Method similar toasDecimal(), but that will return emptyOptional(Optional.empty()) if this node cannot be coerced toBigDecimal.- Returns:
- Java
BigDecimalvalue this node represents, asOptional<BigDecimal>, if possible to accurately represent;Optional.empty()otherwise
-
require
Method that may be called to verify thatthisnode is NOT so-called "missing node": that is, one for whichisMissingNode()returnstrue. If not missing node,thisis returned to allow chaining; otherwise exception is thrown.- Returns:
thisnode to allow chaining- Throws:
IllegalArgumentException- if this node is "missing node"
-
requireNonNull
Method that may be called to verify thatthisnode 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,thisis returned to allow chaining; otherwise exception is thrown.- Returns:
thisnode 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; otherwiseIllegalArgumentExceptionis 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; otherwiseIllegalArgumentExceptionis 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 specifiedJsonPointerstarting fromthisnode (but note that value may be explicit JSON null value). If such value node exists it is returned; otherwiseIllegalArgumentExceptionis thrown.- Parameters:
pathExpr-JsonPointerexpression (as String) to use for finding value node- Returns:
- Matching value node for given expression
- Throws:
IllegalArgumentException- if no value node exists at givenJSON Pointerpath
-
requiredAt
Method is functionally equivalent toat(path).required()and can be used to check that there is an actual value node at specifiedJsonPointerstarting fromthisnode (but note that value may be explicit JSON null value). If such value node exists it is returned; otherwiseIllegalArgumentExceptionis thrown.- Parameters:
path-JsonPointerexpression to use for finding value node- Returns:
- Matching value node for given expression
- Throws:
IllegalArgumentException- if no value node exists at givenJSON Pointerpath
-
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
nullvalues are added, this method will returntruefor 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
truefor 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 returnfalsefor 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 returnfalsefor 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:
spliteratorin 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 ofObjectNodesimilar to howMap.entrySet()works; for other node types will return emptySet.- Returns:
- Set of properties, if this node is an
ObjectNode(isObject()returnstrue); emptySetotherwise.
-
valueStream
Returns a stream of all value nodes of this Node, iff this node is anArrayNodeorObjectNode. In case ofObjectnode, 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 whetherexprOrPropertyis a validJsonPointerexpression 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 validJsonPointerexpression, value is taken as a literal Object property name and calls is alias forwithObjectProperty(exprOrProperty);
- Parameters:
exprOrProperty-JsonPointerexpression to use (if valid as one), or, if not (no leading "/"), property name to match.- Returns:
ObjectNodefound 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) andtrueforpreferIndex(that is, will try to considerJsonPointersegments index if at all possible and only secondarily as property name- Parameters:
ptr-JsonPointerthat indicates path to use for Object value to return (potentially creating as necessary)- Returns:
ObjectNodefound 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 withJsonPointerof/a/bwould returnObjectNode{ "c" : 13 }In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointerof/a/xan emptyObjectNodewould 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 existingJsonNodethrough which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteModepassed in. For example, with above document and expression of/a/b/c, conversion is allowed if passingOverwriteMode.SCALARSorOvewriteMode.ALL, and resulting document would look like:{ "a" : { "b" : { "c" : { } }, "x" : { } } }but if different modes (NONEorNULLS) is passed, an exception is thrown instead.- Parameters:
ptr- Pointer that indicates path to use forObjectNodevalue to return (potentially creating one as necessary)overwriteMode- Defines which node types may be converted in case of incompatibleJsonPointerexpression: if conversion not allowed,UnsupportedOperationExceptionis 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:
ObjectNodefound 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 onObjectNodeand will either use an existingObjectNodethat 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 haveObjectNodeas value- Returns:
ObjectNodevalue 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) andtrueforpreferIndex. -
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 withJsonPointerof/a/bwould returnArray[ 1, 2 ]
In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointerof/a/xan emptyArrayNodewould 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 existingJsonNodethrough which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteModepassed in. For example, with above document and expression of/a/b/0, conversion is allowed if passingOverwriteMode.SCALARSorOvewriteMode.ALL, and resulting document would look like:{ "a" : { "b" : [ [ ], 2 ], "x" : [ ] } }but if different modes (NONEorNULLS) is passed, an exception is thrown instead.- Parameters:
ptr- Pointer that indicates path to use forArrayNodevalue to return (potentially creating it as necessary)overwriteMode- Defines which node types may be converted in case of incompatibleJsonPointerexpression: 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:
ArrayNodefound 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 onObjectNodeand will either use an existingArrayNodethat 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-inComparatorobject. Nodes will handle traversal of structured types (arrays, objects), but defer to comparator for scalar value comparisons. If a "natural"Comparatoris 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, withthisas the first argument, andotheras the second argument.- Parameters:
comparator- Object called to compare two scalarJsonNodeinstances, 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 useObjectMapperorObjectWriterto 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 throwsDatabindExceptionas a result of violating "required-constraint" for this node (forrequired(java.lang.String)or related methods). -
_reportUnsupportedOperation
-
asString()instead.