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() or parserBuilder().

The type of node can be determined using "is" methods like isNumber() and isString(). Once the type is determined, the value of the node can be extracted via the "as" methods, like asNumber() and asString().

  • Method Details

    • parser

      static JsonNodeParser parser()
      Create a JsonNodeParser for generating a JsonNode from a JSON document.
    • parserBuilder

      static JsonNodeParser.Builder parserBuilder()
      Create a JsonNodeParser.Builder for generating a JsonNode 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:
    • isString

      default boolean isString()
      Returns true if this node represents a JSON string: https://datatracker.ietf.org/doc/html/rfc8259#section-7
      See Also:
    • isBoolean

      default boolean isBoolean()
      Returns true if this node represents a JSON boolean: https://datatracker.ietf.org/doc/html/rfc8259#section-3
      See Also:
    • 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:
    • isObject

      default boolean isObject()
      Returns true if this node represents a JSON object: https://datatracker.ietf.org/doc/html/rfc8259#section-4
      See Also:
    • 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 via JsonNodeParser.Builder.jsonFactory(JsonFactory).

      See Also:
    • asNumber

      String asNumber()
      When isNumber() is true, this returns the number associated with this node. This will throw an exception if isNumber() is false.
      See Also:
    • asString

      String asString()
      When isString(), is true, this returns the string associated with this node. This will throw an exception if isString() ()} is false.
    • asBoolean

      boolean asBoolean()
      When isBoolean() is true, this returns the boolean associated with this node. This will throw an exception if isBoolean() is false.
    • asArray

      List<JsonNode> asArray()
      When isArray() is true, this returns the array associated with this node. This will throw an exception if isArray() is false.
    • asObject

      Map<String,JsonNode> asObject()
      When isObject() is true, this returns the object associated with this node. This will throw an exception if isObject() is false.
    • asEmbeddedObject

      Object asEmbeddedObject()
      When isEmbeddedObject() is true, this returns the embedded object associated with this node. This will throw an exception if isEmbeddedObject() is false.
      See Also:
    • visit

      <T> T visit(JsonNodeVisitor<T> visitor)
      Visit this node using the provided visitor.
    • text

      String text()
      When isString(), isBoolean(), or isNumber() 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)
      When isObject() is true, this will return the result of Optional.ofNullable(asObject().get(child)). If this is any other type, this will return Optional.empty().
    • index

      default Optional<JsonNode> index(int child)
      When isArray() is true, this will return the result of asArray().get(child) if child is within bounds. If this is any other type or the child is out of bounds, this will return Optional.empty().