Interface JsonNode
-
- All Known Implementing Classes:
ArrayJsonNode
,BooleanJsonNode
,EmbeddedObjectJsonNode
,NullJsonNode
,NumberJsonNode
,ObjectJsonNode
,StringJsonNode
public interface JsonNode
A node in a JSON document. Either a number, string, boolean, array, object or null. Also can be an embedded object, which is a non-standard type used in JSON extensions, like CBOR.Created from a JSON document via
parser()
orparserBuilder()
.The type of node can be determined using "is" methods like
isNumber()
andisString()
. Once the type is determined, the value of the node can be extracted via the "as" methods, likeasNumber()
andasString()
.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description List<JsonNode>
asArray()
WhenisArray()
is true, this returns the array associated with this node.boolean
asBoolean()
WhenisBoolean()
is true, this returns the boolean associated with this node.Object
asEmbeddedObject()
WhenisEmbeddedObject()
is true, this returns the embedded object associated with this node.String
asNumber()
WhenisNumber()
is true, this returns the number associated with this node.Map<String,JsonNode>
asObject()
WhenisObject()
is true, this returns the object associated with this node.String
asString()
WhenisString()
, is true, this returns the string associated with this node.static JsonNode
emptyObjectNode()
Return an empty object node.default Optional<JsonNode>
field(String child)
WhenisObject()
is true, this will return the result ofOptional.ofNullable(asObject().get(child))
.default Optional<JsonNode>
index(int child)
WhenisArray()
is true, this will return the result ofasArray().get(child)
if child is within bounds.default boolean
isArray()
Returns true if this node represents a JSON array: https://datatracker.ietf.org/doc/html/rfc8259#section-5default boolean
isBoolean()
Returns true if this node represents a JSON boolean: https://datatracker.ietf.org/doc/html/rfc8259#section-3default boolean
isEmbeddedObject()
Returns true if this node represents a JSON "embedded object".default boolean
isNull()
Returns true if this node represents a JSON null: https://datatracker.ietf.org/doc/html/rfc8259#section-3default boolean
isNumber()
Returns true if this node represents a JSON number: https://datatracker.ietf.org/doc/html/rfc8259#section-6default boolean
isObject()
Returns true if this node represents a JSON object: https://datatracker.ietf.org/doc/html/rfc8259#section-4default boolean
isString()
Returns true if this node represents a JSON string: https://datatracker.ietf.org/doc/html/rfc8259#section-7static JsonNodeParser
parser()
Create aJsonNodeParser
for generating aJsonNode
from a JSON document.static JsonNodeParser.Builder
parserBuilder()
Create aJsonNodeParser.Builder
for generating aJsonNode
from a JSON document.String
text()
WhenisString()
,isBoolean()
, orisNumber()
is true, this will return the value of this node as a textual string.<T> T
visit(JsonNodeVisitor<T> visitor)
Visit this node using the provided visitor.
-
-
-
Method Detail
-
parser
static JsonNodeParser parser()
Create aJsonNodeParser
for generating aJsonNode
from a JSON document.
-
parserBuilder
static JsonNodeParser.Builder parserBuilder()
Create aJsonNodeParser.Builder
for generating aJsonNode
from a JSON document.
-
emptyObjectNode
static JsonNode emptyObjectNode()
Return an empty object node.
-
isNumber
default boolean isNumber()
Returns true if this node represents a JSON number: https://datatracker.ietf.org/doc/html/rfc8259#section-6- See Also:
asNumber()
-
isString
default boolean isString()
Returns true if this node represents a JSON string: https://datatracker.ietf.org/doc/html/rfc8259#section-7- See Also:
asString()
-
isBoolean
default boolean isBoolean()
Returns true if this node represents a JSON boolean: https://datatracker.ietf.org/doc/html/rfc8259#section-3- See Also:
asBoolean()
-
isNull
default boolean isNull()
Returns true if this node represents a JSON null: https://datatracker.ietf.org/doc/html/rfc8259#section-3
-
isArray
default boolean isArray()
Returns true if this node represents a JSON array: https://datatracker.ietf.org/doc/html/rfc8259#section-5- See Also:
asArray()
-
isObject
default boolean isObject()
Returns true if this node represents a JSON object: https://datatracker.ietf.org/doc/html/rfc8259#section-4- See Also:
asObject()
-
isEmbeddedObject
default boolean isEmbeddedObject()
Returns true if this node represents a JSON "embedded object". This non-standard type is associated with JSON extensions, like CBOR or ION. It allows additional data types to be embedded in a JSON document, like a timestamp or a raw byte array.Users who are only concerned with handling JSON can ignore this field. It will only be present when using a custom
JsonFactory
viaJsonNodeParser.Builder.jsonFactory(JsonFactory)
.- See Also:
asEmbeddedObject()
-
asNumber
String asNumber()
WhenisNumber()
is true, this returns the number associated with this node. This will throw an exception ifisNumber()
is false.- See Also:
text()
-
asString
String asString()
WhenisString()
, is true, this returns the string associated with this node. This will throw an exception ifisString()
()} is false.
-
asBoolean
boolean asBoolean()
WhenisBoolean()
is true, this returns the boolean associated with this node. This will throw an exception ifisBoolean()
is false.
-
asObject
Map<String,JsonNode> asObject()
WhenisObject()
is true, this returns the object associated with this node. This will throw an exception ifisObject()
is false.
-
asEmbeddedObject
Object asEmbeddedObject()
WhenisEmbeddedObject()
is true, this returns the embedded object associated with this node. This will throw an exception ifisEmbeddedObject()
is false.- See Also:
isEmbeddedObject()
-
visit
<T> T visit(JsonNodeVisitor<T> visitor)
Visit this node using the provided visitor.
-
text
String text()
WhenisString()
,isBoolean()
, orisNumber()
is true, this will return the value of this node as a textual string. If this is any other type, this will return null.
-
field
default Optional<JsonNode> field(String child)
WhenisObject()
is true, this will return the result ofOptional.ofNullable(asObject().get(child))
. If this is any other type, this will returnOptional.empty()
.
-
index
default Optional<JsonNode> index(int child)
WhenisArray()
is true, this will return the result ofasArray().get(child)
if child is within bounds. If this is any other type or the child is out of bounds, this will returnOptional.empty()
.
-
-