Class Jackson2Adapter

java.lang.Object
com.apicatalog.tree.io.Jackson2Adapter
All Implemented Interfaces:
NodeAdapter

public class Jackson2Adapter extends Object implements NodeAdapter
Jackson2Adapter is a NodeAdapter implementation that bridges between a generic tree model and the Jackson 2 JsonNode API.

It provides type inspection, value extraction, and traversal utilities for working with JSON content represented as Jackson nodes. Keys are represented as String values, while values may be any supported JsonNode subtype. Supported node types include:

This adapter is implemented as a stateless singleton; use instance() to obtain the shared instance.

  • Field Details

  • Constructor Details

    • Jackson2Adapter

      public Jackson2Adapter()
  • Method Details

    • instance

      public static final Jackson2Adapter instance()
    • isNode

      public boolean isNode(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the given object can be processed by this adapter.

      Returns true if the adapter is capable of handling the node type; false otherwise.

      Specified by:
      isNode in interface NodeAdapter
      Parameters:
      node - the object to check
      Returns:
      true if the node can be adapted by this adapter, false otherwise
    • keyTypes

      public Set<NodeType> keyTypes()
      Description copied from interface: NodeAdapter
      Returns the set of node types that are supported as keys in map nodes for this adapter.
      Specified by:
      keyTypes in interface NodeAdapter
      Returns:
      a set of supported key node types
    • nodeTypes

      public Set<NodeType> nodeTypes()
      Description copied from interface: NodeAdapter
      Returns the set of node types supported by this adapter.
      Specified by:
      nodeTypes in interface NodeAdapter
      Returns:
      a set of supported node types
    • type

      public NodeType type(Object node)
      Description copied from interface: NodeAdapter
      Returns the type of the given node.
      Specified by:
      type in interface NodeAdapter
      Parameters:
      node - the node to inspect
      Returns:
      the NodeType of the node
    • keys

      public Set<String> keys(Object node)
      Description copied from interface: NodeAdapter
      Returns the collection of property key nodes for a map node.
      Specified by:
      keys in interface NodeAdapter
      Parameters:
      node - the map node
      Returns:
      collection of property key nodes
    • property

      public com.fasterxml.jackson.databind.JsonNode property(Object key, Object node)
      Description copied from interface: NodeAdapter
      Returns the property value associated with the specified key node in a map node.
      Specified by:
      property in interface NodeAdapter
      Parameters:
      key - the property key node
      node - the map node containing the property
      Returns:
      the property value node corresponding to the property key
    • entries

      public Iterable<Map.Entry<?,?>> entries(Object node)
      Description copied from interface: NodeAdapter
      Returns all key-value pairs of the given map node as an Iterable.
      Specified by:
      entries in interface NodeAdapter
      Parameters:
      node - the map node
      Returns:
      iterable of key-value entries
    • entryStream

      public Stream<Map.Entry<?,?>> entryStream(Object node)
      Description copied from interface: NodeAdapter
      Returns all key-value pairs of the given map node as a Stream.
      Specified by:
      entryStream in interface NodeAdapter
      Parameters:
      node - the map node
      Returns:
      stream of key-value entries
    • elements

      public Collection<com.fasterxml.jackson.databind.JsonNode> elements(Object node)
      Description copied from interface: NodeAdapter
      Returns the child nodes of a collection node as an Iterable.
      Specified by:
      elements in interface NodeAdapter
      Parameters:
      node - the collection node
      Returns:
      iterable of child nodes
    • elementStream

      public Stream<com.fasterxml.jackson.databind.JsonNode> elementStream(Object node)
      Description copied from interface: NodeAdapter
      Returns the child nodes of a collection node as a Stream.
      Specified by:
      elementStream in interface NodeAdapter
      Parameters:
      node - the collection node
      Returns:
      stream of child nodes
    • stringValue

      public String stringValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the string value of the node.
      Specified by:
      stringValue in interface NodeAdapter
      Parameters:
      node - the string node
      Returns:
      string representation
    • intValue

      public int intValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the integer value of a number node.
      Specified by:
      intValue in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      integer value
    • longValue

      public long longValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the long value of a number node.
      Specified by:
      longValue in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      long value
    • bigIntegerValue

      public BigInteger bigIntegerValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the BigInteger value of a number node.
      Specified by:
      bigIntegerValue in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      BigInteger value
    • doubleValue

      public double doubleValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the double value of a number node.
      Specified by:
      doubleValue in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      double value
    • decimalValue

      public BigDecimal decimalValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the BigDecimal value of a number node.
      Specified by:
      decimalValue in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      BigDecimal value
    • binaryValue

      public byte[] binaryValue(Object node)
      Description copied from interface: NodeAdapter
      Returns the binary value of the node as a byte array.
      Specified by:
      binaryValue in interface NodeAdapter
      Parameters:
      node - the binary node
      Returns:
      byte array representation
    • asIterable

      public Collection<com.fasterxml.jackson.databind.JsonNode> asIterable(Object node)
      Description copied from interface: NodeAdapter
      Returns the node as an iterable.

      If the node is null, returns an empty iterable. If it is a single element, wraps it as a singleton iterable. Useful for iterating without type checks.

      Specified by:
      asIterable in interface NodeAdapter
      Parameters:
      node - the node to convert
      Returns:
      iterable of elements
    • asStream

      public Stream<com.fasterxml.jackson.databind.JsonNode> asStream(Object node)
      Description copied from interface: NodeAdapter
      Returns the node as a stream.
      Specified by:
      asStream in interface NodeAdapter
      Parameters:
      node - the node to convert
      Returns:
      stream of elements
    • isNull

      public boolean isNull(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a null value.
      Specified by:
      isNull in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is null, false otherwise
    • isBoolean

      public boolean isBoolean(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a boolean value.
      Specified by:
      isBoolean in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is a boolean, false otherwise
    • isMap

      public boolean isMap(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a map (object) structure.
      Specified by:
      isMap in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is a map, false otherwise
    • isCollection

      public boolean isCollection(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a collection.
      Specified by:
      isCollection in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is a collection, false otherwise
    • isSet

      public boolean isSet(Object node)
      Description copied from interface: NodeAdapter
      Contains only unique elements. Is unordered.
      Specified by:
      isSet in interface NodeAdapter
      Returns:
    • isList

      public boolean isList(Object node)
      Description copied from interface: NodeAdapter
      Preserves insertion order and can contain duplicates.
      Specified by:
      isList in interface NodeAdapter
      Returns:
    • isString

      public boolean isString(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a string value.
      Specified by:
      isString in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is a string, false otherwise
    • isNumber

      public boolean isNumber(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node represents a numeric value.
      Specified by:
      isNumber in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is numeric, false otherwise
    • isIntegral

      public boolean isIntegral(Object node)
      Description copied from interface: NodeAdapter
      Checks whether a number node represents an integral value.

      Returns false for floating-point or BigDecimal nodes.

      Specified by:
      isIntegral in interface NodeAdapter
      Parameters:
      node - the number node
      Returns:
      true if the node is integral, false otherwise
    • isBinary

      public boolean isBinary(Object node)
      Description copied from interface: NodeAdapter
      Checks whether the node contains binary data.
      Specified by:
      isBinary in interface NodeAdapter
      Parameters:
      node - the node to check
      Returns:
      true if the node is binary, false otherwise
    • isEmpty

      public boolean isEmpty(Object node)
      Description copied from interface: NodeAdapter
      Checks whether a map node has no entries or a collection node has no items.

      This method is intended for nodes of type NodeType.MAP or NodeType.COLLECTION.

      Specified by:
      isEmpty in interface NodeAdapter
      Parameters:
      node - the node to inspect
      Returns:
      true if the map has no entries or the collection has no items, false otherwise
    • size

      public int size(Object node)
      Description copied from interface: NodeAdapter
      Returns the number of entries in a map node or items in a collection node.

      Intended for nodes of type NodeType.MAP or NodeType.COLLECTION.

      Specified by:
      size in interface NodeAdapter
      Parameters:
      node - the node to inspect
      Returns:
      number of entries (for map) or items (for collection)
    • asString

      public String asString(Object node)
      Description copied from interface: NodeAdapter
      Returns the string representation of the node, converting other types as needed.
      Specified by:
      asString in interface NodeAdapter
      Parameters:
      node - the node to convert
      Returns:
      string representation
    • asDecimal

      public BigDecimal asDecimal(Object node)
      Description copied from interface: NodeAdapter
      Converts the given node to a BigDecimal, if possible.
      Specified by:
      asDecimal in interface NodeAdapter
      Parameters:
      node - the node to convert
      Returns:
      BigDecimal representation of the node