Class BaseJsonNode
- All Implemented Interfaces:
TreeNode
,JsonSerializable
,Serializable
,Iterable<JsonNode>
- Direct Known Subclasses:
ContainerNode
,ValueNode
JsonNode
implementations.
The main addition here is that we declare that sub-classes must
implement JsonSerializable
.
This simplifies object mapping aspects a bit, as no external serializers are needed.
Since 2.10, all implements have been Serializable
.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.JsonNode
JsonNode.OverwriteMode
Nested classes/interfaces inherited from interface com.fasterxml.jackson.databind.JsonSerializable
JsonSerializable.Base
-
Method Summary
Modifier and TypeMethodDescriptionabstract JsonToken
asToken()
Method that can be used for efficient type detection when using stream abstraction for traversing nodes.final JsonNode
Method similar toJsonNode.findValue(java.lang.String)
, but that will return a "missing node" instead of null if no field is found.abstract int
hashCode()
Returns code that identifies type of underlying numeric value, if (and only if) node is a number node.required
(int index) Method is functionally equivalent topath(index).required()
and can be used to check that this node is anArrayNode
(that is, represents JSON Array value) and has value for specifiedindex
(but note that value may be explicit JSON null value).Method is functionally equivalent topath(fieldName).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 keyfieldName
(but note that value may be explicit JSON null value).abstract void
serialize
(JsonGenerator g, SerializerProvider ctxt) Method called to serialize node instances using given generator.abstract void
serializeWithType
(JsonGenerator g, SerializerProvider ctxt, TypeSerializer typeSer) Type information is needed, even if JsonNode instances are "plain" JSON, since they may be mixed with other types.Alternative toJsonNode.toString()
that will serialize this node using Jackson default pretty-printer.toString()
Method that will produce (as of Jackson 2.10) valid JSON using default settings of databind, as String.traverse()
Method for constructing aJsonParser
instance for iterating over contents of the tree that this node is root of.traverse
(ObjectCodec codec) Same asTreeNode.traverse()
, but additionally passesObjectCodec
to use ifJsonParser.readValueAs(Class)
is used (otherwise caller must callJsonParser.setCodec(com.fasterxml.jackson.core.ObjectCodec)
on response explicitly).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.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.Methods inherited from class com.fasterxml.jackson.databind.JsonNode
asBoolean, asBoolean, asDouble, asDouble, asInt, asInt, asLong, asLong, asText, asText, at, at, bigIntegerValue, binaryValue, booleanValue, canConvertToExactIntegral, canConvertToInt, canConvertToLong, decimalValue, deepCopy, doubleValue, elements, equals, equals, fieldNames, fields, findParent, findParents, findParents, findValue, findValues, findValues, findValuesAsText, findValuesAsText, floatValue, get, get, getNodeType, has, has, hasNonNull, hasNonNull, intValue, isArray, isBigDecimal, isBigInteger, isBinary, isBoolean, isContainerNode, isDouble, isEmpty, isFloat, isFloatingPointNumber, isInt, isIntegralNumber, isLong, isMissingNode, isNull, isNumber, isObject, isPojo, isShort, isTextual, isValueNode, iterator, longValue, numberValue, path, path, properties, require, requiredAt, requiredAt, requireNonNull, shortValue, size, textValue, with, withArray, withArray, withArray, withArrayProperty, withObject, withObject, withObject, withObjectProperty
Methods inherited from class com.fasterxml.jackson.databind.JsonSerializable.Base
isEmpty
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
findPath
Description copied from class:JsonNode
Method similar toJsonNode.findValue(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 whichJsonNode.isMissingNode()
returns true; and all value access methods return empty or missing value. -
hashCode
public abstract int hashCode() -
required
Description copied from class:JsonNode
Method is functionally equivalent topath(fieldName).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 keyfieldName
(but note that value may be explicit JSON null value). If this node is Object Node and has value for specified property, matching value is returned; otherwiseIllegalArgumentException
is thrown. -
required
Description copied from class:JsonNode
Method is functionally equivalent topath(index).required()
and can be used to check that this node is anArrayNode
(that is, represents JSON Array value) and has value for specifiedindex
(but note that value may be explicit JSON null value). If this node is Array Node and has value for specified index, value at index is returned; otherwiseIllegalArgumentException
is thrown. -
traverse
Description copied from interface:TreeNode
Method for constructing aJsonParser
instance for iterating over contents of the tree that this node is root of. Functionally equivalent to first serializing tree usingObjectCodec
and then re-parsing but more efficient.NOTE: constructed parser instance will NOT initially point to a token, so before passing it to deserializers, it is typically necessary to advance it to the first available token by calling
JsonParser.nextToken()
.Also note that calling this method will NOT pass
ObjectCodec
reference, so data-binding callback methods likeJsonParser.readValueAs(Class)
will not work with callingJsonParser.setCodec(com.fasterxml.jackson.core.ObjectCodec)
). It is often better to callTreeNode.traverse(ObjectCodec)
to pass the codec explicitly.- Specified by:
traverse
in interfaceTreeNode
- Returns:
JsonParser
that will stream over contents of this node
-
traverse
Description copied from interface:TreeNode
Same asTreeNode.traverse()
, but additionally passesObjectCodec
to use ifJsonParser.readValueAs(Class)
is used (otherwise caller must callJsonParser.setCodec(com.fasterxml.jackson.core.ObjectCodec)
on response explicitly).NOTE: constructed parser instance will NOT initially point to a token, so before passing it to deserializers, it is typically necessary to advance it to the first available token by calling
JsonParser.nextToken()
.- Specified by:
traverse
in interfaceTreeNode
- Parameters:
codec
-ObjectCodec
to associate with parser constructed- Returns:
JsonParser
that will stream over contents of this node
-
asToken
Method that can be used for efficient type detection when using stream abstraction for traversing nodes. Will return the firstJsonToken
that equivalent stream event would produce (for most nodes there is just one token but for structured/container types multiple) -
numberType
Returns code that identifies type of underlying numeric value, if (and only if) node is a number node.- Specified by:
numberType
in interfaceTreeNode
- Returns:
- Type of number contained, if any; or null if node does not contain numeric value.
-
withObject
public ObjectNode withObject(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Description copied from class:JsonNode
Method that can be called on Object or Array nodes, to access a Object-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it. For example, on document{ "a" : { "b" : { "c" : 13 } } }
calling method withJsonPointer
of/a/b
would returnObjectNode
{ "c" : 13 }
In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointer
of/a/x
an emptyObjectNode
would be returned and the document would look like:{ "a" : { "b" : { "c" : 13 }, "x" : { } } }
Finally, if the path is incompatible with the document -- there is an existingJsonNode
through which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteMode
passed in. For example, with above document and expression of/a/b/c
, conversion is allowed if passingOverwriteMode.SCALARS
orOvewriteMode.ALL
, and resulting document would look like:{ "a" : { "b" : { "c" : { } }, "x" : { } } }
but if different modes (NONE
orNULLS
) is passed, an exception is thrown instead.- Overrides:
withObject
in classJsonNode
- Parameters:
ptr
- Pointer that indicates path to use forObjectNode
value to return (potentially creating one as necessary)overwriteMode
- Defines which node types may be converted in case of incompatibleJsonPointer
expression: if conversion not allowed,UnsupportedOperationException
is thrown.preferIndex
- When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like3
), whether to construct anArrayNode
(true
) orObjectNode
(false
). In latter case matching property with quoted number (like"3"
) is used within Object.- Returns:
ObjectNode
found or created
-
withArray
public ArrayNode withArray(JsonPointer ptr, JsonNode.OverwriteMode overwriteMode, boolean preferIndex) Description copied from class:JsonNode
Method that can be called on Object or Array nodes, to access a Array-valued node pointed to by givenJsonPointer
, if such a node exists: or if not, an attempt is made to create one and return it. For example, on document{ "a" : { "b" : [ 1, 2 ] } }
calling method withJsonPointer
of/a/b
would returnArray
[ 1, 2 ]
In cases where path leads to "missing" nodes, a path is created. So, for example, on above document, and
JsonPointer
of/a/x
an emptyArrayNode
would be returned and the document would look like:{ "a" : { "b" : [ 1, 2 ], "x" : [ ] } }
Finally, if the path is incompatible with the document -- there is an existingJsonNode
through which expression cannot go -- a replacement is attempted if (and only if) conversion is allowed as peroverwriteMode
passed in. For example, with above document and expression of/a/b/0
, conversion is allowed if passingOverwriteMode.SCALARS
orOvewriteMode.ALL
, and resulting document would look like:{ "a" : { "b" : [ [ ], 2 ], "x" : [ ] } }
but if different modes (NONE
orNULLS
) is passed, an exception is thrown instead.- Overrides:
withArray
in classJsonNode
- Parameters:
ptr
- Pointer that indicates path to use forArrayNode
value to return (potentially creating it as necessary)overwriteMode
- Defines which node types may be converted in case of incompatibleJsonPointer
expression: if conversion not allowed, an exception is thrown.preferIndex
- When creating a path (for empty or replacement), and path contains segment that may be an array index (simple integer number like3
), whether to construct anArrayNode
(true
) orObjectNode
(false
). In latter case matching property with quoted number (like"3"
) is used within Object.- Returns:
ArrayNode
found or created
-
serialize
Method called to serialize node instances using given generator.- Specified by:
serialize
in interfaceJsonSerializable
- Throws:
IOException
-
serializeWithType
public abstract void serializeWithType(JsonGenerator g, SerializerProvider ctxt, TypeSerializer typeSer) throws IOException Type information is needed, even if JsonNode instances are "plain" JSON, since they may be mixed with other types.- Specified by:
serializeWithType
in interfaceJsonSerializable
- Throws:
IOException
-
toString
Description copied from class:JsonNode
Method that will produce (as of Jackson 2.10) valid JSON using default settings of databind, as String. If you want other kinds of JSON output (or output formatted using one of other Jackson-supported data formats) make sure to useObjectMapper
orObjectWriter
to serialize an instance, for example:String json = objectMapper.writeValueAsString(rootNode);
Note: method defined as abstract to ensure all implementation classes explicitly implement method, instead of relying on
Object.toString()
definition. -
toPrettyString
Description copied from class:JsonNode
Alternative toJsonNode.toString()
that will serialize this node using Jackson default pretty-printer.- Overrides:
toPrettyString
in classJsonNode
-