Class JsonNodeHelper
java.lang.Object
org.sentrysoftware.maven.metricshub.connector.producer.JsonNodeHelper
Utility class for common operations and methods with JsonNode objects.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionconvertSimpleArrayNodeToList
(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Converts a JacksonArrayNode
to aList
of strings.nodeToStringList
(com.fasterxml.jackson.databind.JsonNode node) Converts aJsonNode
to a list 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 a list of strings.This method takes a
JsonNode
as input and converts it into a list of strings. If the input node is an array, the method usesconvertSimpleArrayNodeToList(ArrayNode)
to perform the conversion. If the node is a single value node, it is converted into a singleton list containing its text representation. If the input node is null or not of the expected types, an empty list is returned.- 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 or not of the expected types.
-
convertSimpleArrayNodeToList
public static List<String> convertSimpleArrayNodeToList(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Converts a JacksonArrayNode
to aList
of strings.- Parameters:
arrayNode
- TheArrayNode
to be converted to a list of strings.- 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.
-