Top Description Inners Constructors Methods
com.fasterxml.jackson.databind

public abstract Class JsonNode

extends Base
implements TreeNode, Iterable<JsonNode>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, com.fasterxml.jackson.core.TreeNode
Known Direct Subclasses
com.fasterxml.jackson.databind.node.BaseJsonNode
Imports
java.io.IOException, java.math.BigDecimal, .BigInteger, java.util.*, com.fasterxml.jackson.core.*, com.fasterxml.jackson.databind.node.ArrayNode, .JsonNodeType, .MissingNode, .ObjectNode, com.fasterxml.jackson.databind.util.ClassUtil

Base class for all JSON nodes, which form the basis of JSON Tree Model that Jackson implements. One way to think of these nodes is to consider them similar to DOM nodes in XML DOM trees.

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 com.fasterxml.jackson.databind.node.

Note that it is possible to "read" from nodes, using method TreeNode#traverse(ObjectCodec), 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 and Inner Type Summary

Modifier and TypeClass and Description
public static enum
JsonNode.OverwriteMode

Configuration setting used with JsonNode#withObject(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).

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected abstract JsonNode

Returns:

Either matching JsonNode for the first step of path or null if no match (including case that this node is not a container)
_at
(JsonPointer
Path expression to use
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 throws IllegalArgumentException as a result of violating "required-constraint" for this node (for required or related methods).

protected <T extends JsonNode> T
public boolean
asBoolean()

Method that will try to convert value of this node to a Java boolean.

public boolean
asBoolean(boolean defaultValue)

Method that will try to convert value of this node to a Java boolean.

public double
asDouble()

Method that will try to convert value of this node to a Java double.

public double
asDouble(double defaultValue)

Method that will try to convert value of this node to a Java double.

public int
asInt()

Method that will try to convert value of this node to a Java int.

public int
asInt(int defaultValue)

Method that will try to convert value of this node to a Java int.

public long
asLong()

Method that will try to convert value of this node to a Java long.

public long
asLong(long defaultValue)

Method that will try to convert value of this node to a Java long.

public abstract String
asText()

Method that will return a valid String representation of the container value, if the node is a value node (method isValueNode returns true), otherwise empty String.

public String

Returns:

The text value of this node, or defaultValue if the text value is absent.
asText
(String
The default value to return if this node's text value is absent.
defaultValue
)

Returns the text value of this node or the provided defaultValue if this node does not have a text value.

public final JsonNode

Returns:

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

Implements com.fasterxml.jackson.core.TreeNode.at.

Method for locating node specified by given JSON pointer instances.

public final JsonNode

Returns:

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

Implements com.fasterxml.jackson.core.TreeNode.at.

Convenience method that is functionally equivalent to:

   return at(JsonPointer.valueOf(jsonPointerExpression));
public BigInteger

Returns:

BigInteger value this node contains, if numeric node; BigInteger.ZERO for non-number nodes.
bigIntegerValue
()

Returns integer value for this node (as BigInteger), if and only if this node is numeric (isNumber returns true).

public byte[]

Returns:

Binary data this node contains, iff it is a binary node; null otherwise
binaryValue
()

Method to use for accessing binary content of binary nodes (nodes for which isBinary returns true); or for Text Nodes (ones for which textValue returns non-null value), to read decoded base64 data.

public boolean

Returns:

Boolean value this node contains, if any; otherwise always false
booleanValue
()

Method to use for accessing JSON boolean values (value literals 'true' and 'false').

public boolean

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.
canConvertToExactIntegral
()

Method that can be used to check whether contained value is numeric (returns true for isNumber()) and can be losslessly converted to integral number (specifically, BigInteger but potentially others, see canConvertToInt and canConvertToInt).

public boolean
canConvertToInt()

Method that can be used to check whether this node is a numeric node (isNumber would return true) AND its value fits within Java's 32-bit signed integer type, int.

public boolean
canConvertToLong()

Method that can be used to check whether this node is a numeric node (isNumber would return true) AND its value fits within Java's 64-bit signed integer type, long.

public BigDecimal

Returns:

BigDecimal value this node contains, if numeric node; BigDecimal.ZERO for non-number nodes.
decimalValue
()

Returns floating point value for this node (as BigDecimal), if and only if this node is numeric (isNumber returns true).

public abstract <T extends JsonNode> T

Returns:

Node that is either a copy of this node (and all non-leaf children); or, for immutable leaf nodes, node itself.
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.

public double

Returns:

64-bit double value this node contains, if any; 0.0 for non-number nodes.
doubleValue
()

Returns 64-bit floating point (double) value for this node, if and only if this node is numeric (isNumber returns true).

public Iterator<JsonNode>
elements()

Method for accessing all value nodes of this Node, iff this node is a JSON Array or Object node.

public boolean
equals(Comparator<JsonNode>
Object called to compare two scalar JsonNode instances, and return either 0 (are equals) or non-zero (not equal)
comparator
,
JsonNode other)

Entry method for invoking customizable comparison, using passed-in Comparator object.

public abstract boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Equality for node objects is defined as full (deep) value equality.

public Iterator<String>
fieldNames()

Implements com.fasterxml.jackson.core.TreeNode.fieldNames.

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

public Iterator<Map.Entry<String, JsonNode>>

Returns:

Iterator that can be used to traverse all key/value pairs for object nodes; empty iterator (no contents) for other types
fields
()

public abstract JsonNode

Returns:

Value of first matching node found, if any; null if none
findParent
(String
Name of field to look for
fieldName
)

Method for finding a JSON Object that contains specified field, within this node or its descendants.

public final List<JsonNode>

Returns:

Value of first matching node found, if any; null if none
findParents
(String
Name of field to look for
fieldName
)

Method for finding a JSON Object that contains specified field, within this node or its descendants.

public abstract List<JsonNode>
findParents(String fieldName, List<JsonNode> foundSoFar)

public abstract JsonNode

Returns:

Value of first matching node found; or if not found, a "missing node" (non-null instance that has no value)
findPath
(String
Name of field to look for
fieldName
)

Method similar to findValue, but that will return a "missing node" instead of null if no field is found.

public abstract JsonNode

Returns:

Value of first matching node found, if any; null if none
findValue
(String
Name of field to look for
fieldName
)

Method for finding the first JSON Object field with specified name in this node or its child nodes, and returning value it has.

public final List<JsonNode>
findValues(String
Name of field to look for
fieldName
)

Method for finding JSON Object fields with specified name -- both immediate child values and descendants -- and returning found ones as a List.

public abstract List<JsonNode>
findValues(String fieldName, List<JsonNode> foundSoFar)

public final List<String>
findValuesAsText(String fieldName)

Similar to findValues, but will additionally convert values into Strings, calling asText.

public abstract List<String>
findValuesAsText(String fieldName, List<String> foundSoFar)

public float

Returns:

32-bit float value this node contains, if any; 0.0 for non-number nodes.
floatValue
()

Returns 32-bit floating value for this node, if and only if this node is numeric (isNumber returns true).

public abstract JsonNode

Returns:

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

Redeclares com.fasterxml.jackson.core.TreeNode.get.

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

public JsonNode

Returns:

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

Implements com.fasterxml.jackson.core.TreeNode.get.

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

public abstract JsonNodeType

Returns:

the node type as a JsonNodeType enum value
getNodeType
()

Return the type of this node

public boolean

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
(String
Name of element to check
fieldName
)

Method that allows checking whether this node is JSON Object node and contains value for specified property.

public boolean

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
(int
Index to check
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.

public boolean
hasNonNull(String fieldName)

Method that is similar to has(String), but that will return false for explicitly added nulls.

public boolean
hasNonNull(int index)

Method that is similar to has(int), but that will return false for explicitly added nulls.

public int

Returns:

Integer value this node contains, if any; 0 for non-number nodes.
intValue
()

Returns integer value for this node, if and only if this node is numeric (isNumber returns true).

public boolean
isArray()

Implements com.fasterxml.jackson.core.TreeNode.isArray.

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

public boolean
public boolean
public final boolean

Returns:

True if this node represents base64 encoded binary data
isBinary
()

Method that can be used to check if this node represents binary data (Base64 encoded).

public final boolean
isBoolean()

Method that can be used to check if this node was created from JSON boolean value (literals "true" and "false").

public final boolean
isContainerNode()

Implements com.fasterxml.jackson.core.TreeNode.isContainerNode.

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

public boolean
public boolean
isEmpty()

Convenience method that is functionally same as:

    size() == 0
public boolean
public boolean

Returns:

True if this node represents a non-integral numeric JSON value
isFloatingPointNumber
()

public boolean

Returns:

True if the value contained by this node is stored as Java int
isInt
()

Method that can be used to check whether contained value is a number represented as Java int.

public boolean

Returns:

True if this node represents an integral (integer) numeric JSON value
isIntegralNumber
()

public boolean

Returns:

True if the value contained by this node is stored as Java long
isLong
()

Method that can be used to check whether contained value is a number represented as Java long.

public boolean
isMissingNode()

Implements com.fasterxml.jackson.core.TreeNode.isMissingNode.

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

public final boolean
isNull()

Method that can be used to check if this node was created from JSON literal null value.

public final boolean

Returns:

True if this node represents a numeric JSON value
isNumber
()

public boolean
isObject()

Implements com.fasterxml.jackson.core.TreeNode.isObject.

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

public final boolean

Returns:

True if this node wraps a POJO
isPojo
()

Method that can be used to check if the node is a wrapper for a POJO ("Plain Old Java Object" aka "bean".

public boolean

Returns:

True if the value contained by this node is stored as Java short
isShort
()

Method that can be used to check whether contained value is a number represented as Java short.

public final boolean
isTextual()

Method that checks whether this node represents basic JSON String value.

public final boolean
isValueNode()

Implements com.fasterxml.jackson.core.TreeNode.isValueNode.

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

public final Iterator<JsonNode>
iterator()

Implements java.lang.Iterable.iterator.

Same as calling elements; implemented so that convenience "for-each" loop can be used for looping over elements of JSON Array constructs.

public long

Returns:

Long value this node contains, if any; 0 for non-number nodes.
longValue
()

Returns 64-bit long value for this node, if and only if this node is numeric (isNumber returns true).

public Number

Returns:

Number value this node contains, if any (null for non-number nodes).
numberValue
()

Returns numeric value for this node, if and only if this node is numeric (isNumber returns true); otherwise returns null

public abstract JsonNode
path(String
Name of the field (of Object node) to access
fieldName
)

Redeclares com.fasterxml.jackson.core.TreeNode.path.

This method is similar to get(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 for isMissingNode) will be returned.

public abstract JsonNode
path(int
Index of the Array node element to access
index
)

Redeclares com.fasterxml.jackson.core.TreeNode.path.

This method is similar to get(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 for isMissingNode) will be returned.

public Set<Map.Entry<String, JsonNode>>

Returns:

Set of properties, if this node is an ObjectNode (JsonNode#isObject() returns true); empty java.util.Set otherwise.
properties
()

Accessor that will return properties of ObjectNode similar to how Map#entrySet() works; for other node types will return empty java.util.Set.

public <T extends JsonNode> T

Returns:

this node to allow chaining
require
()

Method that may be called to verify that this node is NOT so-called "missing node": that is, one for which isMissingNode() returns true.

public JsonNode

Returns:

Value of the specified property of this Object node
required
(String
Name of property to access
propertyName
)

Method is functionally equivalent to path(fieldName).required() and can be used to check that this node is an ObjectNode (that is, represents JSON Object value) and has value for specified property with key fieldName (but note that value may be explicit JSON null value).

public JsonNode

Returns:

Value at specified index of this Array node
required
(int
Index of the value of this Array node to access
index
)

Method is functionally equivalent to path(index).required() and can be used to check that this node is an ArrayNode (that is, represents JSON Array value) and has value for specified index (but note that value may be explicit JSON null value).

public JsonNode

Returns:

Matching value node for given expression
requiredAt
(String
JsonPointer expression (as String) to use for finding value node
pathExpr
)

Method is functionally equivalent to at(pathExpr).required() and can be used to check that there is an actual value node at specified JsonPointer starting from this node (but note that value may be explicit JSON null value).

public final JsonNode

Returns:

Matching value node for given expression
requiredAt
(final JsonPointer
JsonPointer expression to use for finding value node
path
)

Method is functionally equivalent to at(path).required() and can be used to check that there is an actual value node at specified JsonPointer starting from this node (but note that value may be explicit JSON null value).

public <T extends JsonNode> T

Returns:

this node to allow chaining
requireNonNull
()

Method that may be called to verify that this node is neither so-called "missing node" (that is, one for which isMissingNode() returns true) nor "null node" (one for which isNull() returns true).

public short

Returns:

Short value this node contains, if any; 0 for non-number nodes.
shortValue
()

Returns 16-bit short value for this node, if and only if this node is numeric (isNumber returns true).

public int
size()

Implements com.fasterxml.jackson.core.TreeNode.size.

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

public String

Returns:

Textual value this node contains, iff it is a textual JSON node (comes from JSON String value entry)
textValue
()

Method to use for accessing String values.

public String
toPrettyString()

Alternative to toString that will serialize this node using Jackson default pretty-printer.

public abstract String
toString()

Overrides java.lang.Object.toString.

Method that will produce (as of Jackson 2.10) valid JSON using default settings of databind, as String.

public <T extends JsonNode> T
with(String exprOrProperty)
Deprecated Since 2.14 use withObject(String) instead

Method that works in one of possible ways, depending on whether exprOrProperty is a valid JsonPointer expression or not (valid expression is either empty String "" or starts with leading slash / character).

public <T extends JsonNode> T

Returns:

ArrayNode found or created
withArray
(String
Either JsonPointer expression for full access (if valid pointer expression), or the name of property for the ArrayNode.
exprOrProperty
)

Method that works in one of possible ways, depending on whether exprOrProperty is a valid JsonPointer expression or not (valid expression is either empty String "" or starts with leading slash / character).

public ArrayNode
withArray(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex)

Short-cut equivalent to:

  withArray(JsonPointer.compile(expr), overwriteMode, preferIndex);
public final ArrayNode

Returns:

ArrayNode found or created
withArray
(JsonPointer
Pointer that indicates path to use for ArrayNode to return (potentially creating as necessary)
ptr
)

Same as withArray(JsonPointer, OverwriteMode, boolean) but with defaults of OvewriteMode#NULLS (overwrite mode) and true for preferIndex.

public ArrayNode

Returns:

ArrayNode found or created
withArray
(JsonPointer
Pointer that indicates path to use for ArrayNode value to return (potentially creating it as necessary)
ptr
,
JsonNode.OverwriteMode
Defines which node types may be converted in case of incompatible JsonPointer expression: if conversion not allowed, an exception is thrown.
overwriteMode
,
boolean
When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like 3), whether to construct an ArrayNode (true) or ObjectNode (false). In latter case matching property with quoted number (like "3") is used within Object.
preferIndex
)

Method that can be called on Object or Array nodes, to access a Array-valued node pointed to by given JsonPointer, if such a node exists: or if not, an attempt is made to create one and return it.

public ArrayNode

Returns:

ArrayNode value of given property (existing or created)
withArrayProperty
(String
Name of property that has or will have ArrayNode as value
propName
)

Method similar to withArray(JsonPointer, OverwriteMode, boolean) -- basically short-cut to:

   withArray(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
public ObjectNode

Returns:

ObjectNode found or created
withObject
(String
JsonPointer expression to use (if valid as one), or, if not (no leading "/"), property name to match.
exprOrProperty
)

Method that works in one of possible ways, depending on whether exprOrProperty is a valid JsonPointer expression or not (valid expression is either empty String "" or starts with leading slash / character).

public final ObjectNode
withObject(String expr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex)

Short-cut equivalent to:

  withObject(JsonPointer.compile(expr), overwriteMode, preferIndex);
public final ObjectNode

Returns:

ObjectNode found or created
withObject
(JsonPointer
JsonPointer that indicates path to use for Object value to return (potentially creating as necessary)
ptr
)

Same as withObject(JsonPointer, OverwriteMode, boolean) but with defaults of OvewriteMode#NULLS (overwrite mode) and true for preferIndex (that is, will try to consider JsonPointer segments index if at all possible and only secondarily as property name

public ObjectNode

Returns:

ObjectNode found or created
withObject
(JsonPointer
Pointer that indicates path to use for ObjectNode value to return (potentially creating one as necessary)
ptr
,
JsonNode.OverwriteMode
Defines which node types may be converted in case of incompatible JsonPointer expression: if conversion not allowed, UnsupportedOperationException is thrown.
overwriteMode
,
boolean
When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like 3), whether to construct an ArrayNode (true) or ObjectNode (false). In latter case matching property with quoted number (like "3") is used within Object.
preferIndex
)

Method that can be called on Object or Array nodes, to access a Object-valued node pointed to by given JsonPointer, if such a node exists: or if not, an attempt is made to create one and return it.

public ObjectNode

Returns:

ObjectNode value of given property (existing or created)
withObjectProperty
(String
Name of property that has or will have ObjectNode as value
propName
)

Method similar to withObject(JsonPointer, OverwriteMode, boolean) -- basically short-cut to:

   withObject(JsonPointer.compile("/"+propName), OverwriteMode.NULLS, false);
Inherited from com.fasterxml.jackson.databind.JsonSerializable.Base:
isEmpty