Class JsonNodeHelper
java.lang.Object
org.sentrysoftware.maven.metricshub.connector.producer.JsonNodeHelper
Utility class for common operations and methods with JsonNode objects.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <C extends Collection<String>>
CconvertSimpleArrayNodeToCollection
(com.fasterxml.jackson.databind.node.ArrayNode arrayNode, Supplier<C> collectionFactory) Converts a JacksonArrayNode
to aCollection
of strings.static <C extends Collection<String>>
CnodeToStringCollection
(com.fasterxml.jackson.databind.JsonNode node, Supplier<C> collectionFactory) Converts aJsonNode
to aCollection
of strings.
For a string node, the string is split using a comma as a separator, trailing and leading spaces are removed from each element.nodeToStringList
(com.fasterxml.jackson.databind.JsonNode node) Converts aJsonNode
to aList
of strings.static boolean
nonNull
(com.fasterxml.jackson.databind.JsonNode node) Checks if the provided JsonNode is non-null and not a JSON null node.static boolean
nonNullBooleanOrDefault
(com.fasterxml.jackson.databind.JsonNode node, boolean defaultValue) Returns the boolean value of the provided JsonNode if it is non-null and not a JSON null node.static String
nonNullTextOrDefault
(com.fasterxml.jackson.databind.JsonNode node, String defaultValue) Returns the text value of the provided JsonNode if it is non-null and not a JSON null node.static Stream<com.fasterxml.jackson.databind.JsonNode>
stream
(com.fasterxml.jackson.databind.node.ArrayNode arrayNode)
-
Constructor Details
-
JsonNodeHelper
public JsonNodeHelper()
-
-
Method Details
-
nonNull
public static boolean nonNull(com.fasterxml.jackson.databind.JsonNode node) Checks if the provided JsonNode is non-null and not a JSON null node.- Parameters:
node
- The JsonNode to be checked.- Returns:
true
if the node is non-null and not a JSON null,false
otherwise.
-
stream
public static Stream<com.fasterxml.jackson.databind.JsonNode> stream(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Converts anArrayNode
into a filteredStream
of non-nullJsonNode
elements.This method transforms the provided
ArrayNode
into a sequential stream ofJsonNode
objects, filtering out any null elements from the stream. The resulting stream is ordered and immutable. -
nodeToStringList
Converts aJsonNode
to aList
of strings. This method invokesnodeToStringCollection(JsonNode, Supplier)
with anArrayList
as the collection type.- Parameters:
node
- TheJsonNode
to convert to a list of strings.- Returns:
- A
List
of strings representing the contents of the input node, or an empty list if the node is null.
-
nodeToStringCollection
public static <C extends Collection<String>> C nodeToStringCollection(com.fasterxml.jackson.databind.JsonNode node, Supplier<C> collectionFactory) Converts aJsonNode
to aCollection
of strings.
For a string node, the string is split using a comma as a separator, trailing and leading spaces are removed from each element.- Type Parameters:
C
- The type of the collection.- Parameters:
node
- TheJsonNode
to convert to a collection of strings.collectionFactory
- The factory for the collection to be returned.- Returns:
- A
Collection
of strings representing the contents of the input node, or an empty Collection if the node is null.
-
convertSimpleArrayNodeToCollection
public static <C extends Collection<String>> C convertSimpleArrayNodeToCollection(com.fasterxml.jackson.databind.node.ArrayNode arrayNode, Supplier<C> collectionFactory) Converts a JacksonArrayNode
to aCollection
of strings.- Type Parameters:
C
- The type of the collection.- Parameters:
arrayNode
- TheArrayNode
to be converted to a collection of strings.collectionFactory
- The factory for the collection to be returned.- Returns:
- A
List<String>
containing the text representations of the elements in theArrayNode
. - Throws:
NullPointerException
- ifarrayNode
isnull
.
-
nonNullTextOrDefault
public static String nonNullTextOrDefault(com.fasterxml.jackson.databind.JsonNode node, String defaultValue) Returns the text value of the provided JsonNode if it is non-null and not a JSON null node. If the node is null or represents a JSON null, the specified default value is returned.- Parameters:
node
- The JsonNode to extract text from.defaultValue
- The default value to return if the node is null or represents a JSON null.- Returns:
- The text value of the JsonNode or the specified default value if the node is null or represents a JSON null.
-
nonNullBooleanOrDefault
public static boolean nonNullBooleanOrDefault(com.fasterxml.jackson.databind.JsonNode node, boolean defaultValue) Returns the boolean value of the provided JsonNode if it is non-null and not a JSON null node. If the node is null or represents a JSON null, the specified default value is returned.- Parameters:
node
- The JsonNode to extract text from.defaultValue
- The default value to return if the node is null or represents a JSON null.- Returns:
- The boolean value of the JsonNode or the specified default value if the node is null or represents a JSON null.
-