Class JsonNodeHelper
java.lang.Object
org.metricshub.maven.plugin.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 JacksonArrayNodeto aCollectionof strings.static <C extends Collection<String>>
CnodeToStringCollection(com.fasterxml.jackson.databind.JsonNode node, Supplier<C> collectionFactory) Converts aJsonNodeto aCollectionof 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 aJsonNodeto aListof strings.static booleannonNull(com.fasterxml.jackson.databind.JsonNode node) Checks if the provided JsonNode is non-null and not a JSON null node.static booleannonNullBooleanOrDefault(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 StringnonNullTextOrDefault(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:
trueif the node is non-null and not a JSON null,falseotherwise.
-
stream
public static Stream<com.fasterxml.jackson.databind.JsonNode> stream(com.fasterxml.jackson.databind.node.ArrayNode arrayNode) Converts anArrayNodeinto a filteredStreamof non-nullJsonNodeelements.This method transforms the provided
ArrayNodeinto a sequential stream ofJsonNodeobjects, filtering out any null elements from the stream. The resulting stream is ordered and immutable. -
nodeToStringList
Converts aJsonNodeto aListof strings. This method invokesnodeToStringCollection(JsonNode, Supplier)with anArrayListas the collection type.- Parameters:
node- TheJsonNodeto convert to a list of strings.- Returns:
- A
Listof 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 aJsonNodeto aCollectionof 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- TheJsonNodeto convert to a collection of strings.collectionFactory- The factory for the collection to be returned.- Returns:
- A
Collectionof 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 JacksonArrayNodeto aCollectionof strings.- Type Parameters:
C- The type of the collection.- Parameters:
arrayNode- TheArrayNodeto 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- ifarrayNodeisnull.
-
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.
-