Class Jackson2Adapter
- java.lang.Object
-
- com.apicatalog.tree.io.Jackson2Adapter
-
- All Implemented Interfaces:
NodeAdapter
public class Jackson2Adapter extends java.lang.Object implements NodeAdapter
Jackson2Adapteris aNodeAdapterimplementation that bridges between a generic tree model and the Jackson 2JsonNodeAPI.It provides type inspection, value extraction, and traversal utilities for working with JSON content represented as Jackson nodes. Keys are represented as
Stringvalues, while values may be any supportedJsonNodesubtype. Supported node types include:NodeType.STRINGNodeType.NUMBERNodeType.TRUENodeType.FALSENodeType.NULLNodeType.COLLECTION(arrays)NodeType.MAP(objects)NodeType.BINARY
This adapter is implemented as a stateless singleton; use
instance()to obtain the shared instance.
-
-
Constructor Summary
Constructors Constructor Description Jackson2Adapter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.math.BigDecimalasDecimal(java.lang.Object node)Converts the given node to aBigDecimal, if possible.java.util.Collection<com.fasterxml.jackson.databind.JsonNode>asIterable(java.lang.Object node)Returns the node as an iterable.java.util.stream.Stream<com.fasterxml.jackson.databind.JsonNode>asStream(java.lang.Object node)Returns the node as a stream.java.lang.StringasString(java.lang.Object node)Returns the string representation of the node, converting other types as needed.java.math.BigIntegerbigIntegerValue(java.lang.Object node)Returns theBigIntegervalue of a number node.byte[]binaryValue(java.lang.Object node)Returns the binary value of the node as a byte array.java.math.BigDecimaldecimalValue(java.lang.Object node)Returns theBigDecimalvalue of a number node.doubledoubleValue(java.lang.Object node)Returns the double value of a number node.java.util.Collection<com.fasterxml.jackson.databind.JsonNode>elements(java.lang.Object node)Returns the child nodes of a collection node as anIterable.java.util.stream.Stream<com.fasterxml.jackson.databind.JsonNode>elementStream(java.lang.Object node)Returns the child nodes of a collection node as aStream.java.lang.Iterable<java.util.Map.Entry<?,?>>entries(java.lang.Object node)Returns all key-value pairs of the given map node as anIterable.java.util.stream.Stream<java.util.Map.Entry<?,?>>entryStream(java.lang.Object node)Returns all key-value pairs of the given map node as aStream.static Jackson2Adapterinstance()intintValue(java.lang.Object node)Returns the integer value of a number node.booleanisBinary(java.lang.Object node)Checks whether the node contains binary data.booleanisBoolean(java.lang.Object node)Checks whether the node represents a boolean value.booleanisCollection(java.lang.Object node)Checks whether the node represents a collection.booleanisEmpty(java.lang.Object node)Checks whether a map node has no entries or a collection node has no items.booleanisIntegral(java.lang.Object node)Checks whether a number node represents an integral value.booleanisList(java.lang.Object node)Preserves insertion order and can contain duplicates.booleanisMap(java.lang.Object node)Checks whether the node represents a map (object) structure.booleanisNode(java.lang.Object node)Checks whether the given object can be processed by this adapter.booleanisNull(java.lang.Object node)Checks whether the node represents a null value.booleanisNumber(java.lang.Object node)Checks whether the node represents a numeric value.booleanisSet(java.lang.Object node)Contains only unique elements.booleanisString(java.lang.Object node)Checks whether the node represents a string value.java.util.Set<java.lang.String>keys(java.lang.Object node)Returns the collection of property key nodes for a map node.java.util.Set<NodeType>keyTypes()Returns the set of node types that are supported as keys in map nodes for this adapter.longlongValue(java.lang.Object node)Returns the long value of a number node.java.util.Set<NodeType>nodeTypes()Returns the set of node types supported by this adapter.com.fasterxml.jackson.databind.JsonNodeproperty(java.lang.Object key, java.lang.Object node)Returns the property value associated with the specified key node in a map node.intsize(java.lang.Object node)Returns the number of entries in a map node or items in a collection node.java.lang.StringstringValue(java.lang.Object node)Returns the string value of the node.NodeTypetype(java.lang.Object node)Returns the type of the given node.
-
-
-
Field Detail
-
VALUES
static final java.util.Set<NodeType> VALUES
-
KEYS
static final java.util.Set<NodeType> KEYS
-
INSTANCE
static final Jackson2Adapter INSTANCE
-
-
Method Detail
-
instance
public static final Jackson2Adapter instance()
-
isNode
public boolean isNode(java.lang.Object node)
Description copied from interface:NodeAdapterChecks 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:
isNodein interfaceNodeAdapter- Parameters:
node- the object to check- Returns:
- true if the node can be adapted by this adapter, false otherwise
-
keyTypes
public java.util.Set<NodeType> keyTypes()
Description copied from interface:NodeAdapterReturns the set of node types that are supported as keys in map nodes for this adapter.- Specified by:
keyTypesin interfaceNodeAdapter- Returns:
- a set of supported key node types
-
nodeTypes
public java.util.Set<NodeType> nodeTypes()
Description copied from interface:NodeAdapterReturns the set of node types supported by this adapter.- Specified by:
nodeTypesin interfaceNodeAdapter- Returns:
- a set of supported node types
-
type
public NodeType type(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the type of the given node.- Specified by:
typein interfaceNodeAdapter- Parameters:
node- the node to inspect- Returns:
- the
NodeTypeof the node
-
keys
public java.util.Set<java.lang.String> keys(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the collection of property key nodes for a map node.- Specified by:
keysin interfaceNodeAdapter- Parameters:
node- the map node- Returns:
- collection of property key nodes
-
property
public com.fasterxml.jackson.databind.JsonNode property(java.lang.Object key, java.lang.Object node)Description copied from interface:NodeAdapterReturns the property value associated with the specified key node in a map node.- Specified by:
propertyin interfaceNodeAdapter- Parameters:
key- the property key nodenode- the map node containing the property- Returns:
- the property value node corresponding to the property key
-
entries
public java.lang.Iterable<java.util.Map.Entry<?,?>> entries(java.lang.Object node)
Description copied from interface:NodeAdapterReturns all key-value pairs of the given map node as anIterable.- Specified by:
entriesin interfaceNodeAdapter- Parameters:
node- the map node- Returns:
- iterable of key-value entries
-
entryStream
public java.util.stream.Stream<java.util.Map.Entry<?,?>> entryStream(java.lang.Object node)
Description copied from interface:NodeAdapterReturns all key-value pairs of the given map node as aStream.- Specified by:
entryStreamin interfaceNodeAdapter- Parameters:
node- the map node- Returns:
- stream of key-value entries
-
elements
public java.util.Collection<com.fasterxml.jackson.databind.JsonNode> elements(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the child nodes of a collection node as anIterable.- Specified by:
elementsin interfaceNodeAdapter- Parameters:
node- the collection node- Returns:
- iterable of child nodes
-
elementStream
public java.util.stream.Stream<com.fasterxml.jackson.databind.JsonNode> elementStream(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the child nodes of a collection node as aStream.- Specified by:
elementStreamin interfaceNodeAdapter- Parameters:
node- the collection node- Returns:
- stream of child nodes
-
stringValue
public java.lang.String stringValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the string value of the node.- Specified by:
stringValuein interfaceNodeAdapter- Parameters:
node- the string node- Returns:
- string representation
-
intValue
public int intValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the integer value of a number node.- Specified by:
intValuein interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- integer value
-
longValue
public long longValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the long value of a number node.- Specified by:
longValuein interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- long value
-
bigIntegerValue
public java.math.BigInteger bigIntegerValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns theBigIntegervalue of a number node.- Specified by:
bigIntegerValuein interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- BigInteger value
-
doubleValue
public double doubleValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the double value of a number node.- Specified by:
doubleValuein interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- double value
-
decimalValue
public java.math.BigDecimal decimalValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns theBigDecimalvalue of a number node.- Specified by:
decimalValuein interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- BigDecimal value
-
binaryValue
public byte[] binaryValue(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the binary value of the node as a byte array.- Specified by:
binaryValuein interfaceNodeAdapter- Parameters:
node- the binary node- Returns:
- byte array representation
-
asIterable
public java.util.Collection<com.fasterxml.jackson.databind.JsonNode> asIterable(java.lang.Object node)
Description copied from interface:NodeAdapterReturns 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:
asIterablein interfaceNodeAdapter- Parameters:
node- the node to convert- Returns:
- iterable of elements
-
asStream
public java.util.stream.Stream<com.fasterxml.jackson.databind.JsonNode> asStream(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the node as a stream.- Specified by:
asStreamin interfaceNodeAdapter- Parameters:
node- the node to convert- Returns:
- stream of elements
-
isNull
public boolean isNull(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a null value.- Specified by:
isNullin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is null, false otherwise
-
isBoolean
public boolean isBoolean(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a boolean value.- Specified by:
isBooleanin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is a boolean, false otherwise
-
isMap
public boolean isMap(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a map (object) structure.- Specified by:
isMapin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is a map, false otherwise
-
isCollection
public boolean isCollection(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a collection.- Specified by:
isCollectionin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is a collection, false otherwise
-
isSet
public boolean isSet(java.lang.Object node)
Description copied from interface:NodeAdapterContains only unique elements. Is unordered.- Specified by:
isSetin interfaceNodeAdapter- Returns:
-
isList
public boolean isList(java.lang.Object node)
Description copied from interface:NodeAdapterPreserves insertion order and can contain duplicates.- Specified by:
isListin interfaceNodeAdapter- Returns:
-
isString
public boolean isString(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a string value.- Specified by:
isStringin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is a string, false otherwise
-
isNumber
public boolean isNumber(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node represents a numeric value.- Specified by:
isNumberin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is numeric, false otherwise
-
isIntegral
public boolean isIntegral(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether a number node represents an integral value.Returns false for floating-point or
BigDecimalnodes.- Specified by:
isIntegralin interfaceNodeAdapter- Parameters:
node- the number node- Returns:
- true if the node is integral, false otherwise
-
isBinary
public boolean isBinary(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether the node contains binary data.- Specified by:
isBinaryin interfaceNodeAdapter- Parameters:
node- the node to check- Returns:
- true if the node is binary, false otherwise
-
isEmpty
public boolean isEmpty(java.lang.Object node)
Description copied from interface:NodeAdapterChecks whether a map node has no entries or a collection node has no items.This method is intended for nodes of type
NodeType.MAPorNodeType.COLLECTION.- Specified by:
isEmptyin interfaceNodeAdapter- 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(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the number of entries in a map node or items in a collection node.Intended for nodes of type
NodeType.MAPorNodeType.COLLECTION.- Specified by:
sizein interfaceNodeAdapter- Parameters:
node- the node to inspect- Returns:
- number of entries (for map) or items (for collection)
-
asString
public java.lang.String asString(java.lang.Object node)
Description copied from interface:NodeAdapterReturns the string representation of the node, converting other types as needed.- Specified by:
asStringin interfaceNodeAdapter- Parameters:
node- the node to convert- Returns:
- string representation
-
asDecimal
public java.math.BigDecimal asDecimal(java.lang.Object node)
Description copied from interface:NodeAdapterConverts the given node to aBigDecimal, if possible.- Specified by:
asDecimalin interfaceNodeAdapter- Parameters:
node- the node to convert- Returns:
- BigDecimal representation of the node
-
-