Class JsonNodeUtils

java.lang.Object
com.bonitasoft.processbuilder.extension.JsonNodeUtils

public final class JsonNodeUtils extends Object
Utility class for working with Jackson JsonNode objects and evaluating conditions.

Provides methods for:

  • Converting JsonNode instances to their corresponding Java types
  • Evaluating comparison conditions between values
  • Safely comparing Comparable values of potentially different types
  • Safely navigating JSON structures using a dot-separated path.

This class is designed to be non-instantiable and should only be accessed via static methods.

Since:
1.0
Author:
Bonitasoft
  • Field Details

    • OP_EQUALS

      public static final String OP_EQUALS
      Operator constant for equals comparison.
      See Also:
    • OP_EQUALS_SYMBOL

      public static final String OP_EQUALS_SYMBOL
      Operator constant for equals comparison (symbol form).
      See Also:
    • OP_NOT_EQUALS

      public static final String OP_NOT_EQUALS
      Operator constant for not equals comparison.
      See Also:
    • OP_NOT_EQUALS_SYMBOL

      public static final String OP_NOT_EQUALS_SYMBOL
      Operator constant for not equals comparison (symbol form).
      See Also:
    • OP_CONTAINS

      public static final String OP_CONTAINS
      Operator constant for contains comparison.
      See Also:
    • OP_NOT_CONTAINS

      public static final String OP_NOT_CONTAINS
      Operator constant for not contains comparison.
      See Also:
    • OP_GREATER_THAN

      public static final String OP_GREATER_THAN
      Operator constant for greater than comparison.
      See Also:
    • OP_GREATER_THAN_SYMBOL

      public static final String OP_GREATER_THAN_SYMBOL
      Operator constant for greater than comparison (symbol form).
      See Also:
    • OP_LESS_THAN

      public static final String OP_LESS_THAN
      Operator constant for less than comparison.
      See Also:
    • OP_LESS_THAN_SYMBOL

      public static final String OP_LESS_THAN_SYMBOL
      Operator constant for less than comparison (symbol form).
      See Also:
    • OP_GREATER_OR_EQUAL

      public static final String OP_GREATER_OR_EQUAL
      Operator constant for greater or equal comparison.
      See Also:
    • OP_GREATER_OR_EQUAL_SYMBOL

      public static final String OP_GREATER_OR_EQUAL_SYMBOL
      Operator constant for greater or equal comparison (symbol form).
      See Also:
    • OP_LESS_OR_EQUAL

      public static final String OP_LESS_OR_EQUAL
      Operator constant for less or equal comparison.
      See Also:
    • OP_LESS_OR_EQUAL_SYMBOL

      public static final String OP_LESS_OR_EQUAL_SYMBOL
      Operator constant for less or equal comparison (symbol form).
      See Also:
    • OP_IS_EMPTY

      public static final String OP_IS_EMPTY
      Operator constant for is empty check.

      Evaluates to true if the value is null, an empty string, or a string containing only whitespace characters.

      See Also:
    • OP_IS_NOT_EMPTY

      public static final String OP_IS_NOT_EMPTY
      Operator constant for is not empty check.

      Evaluates to true if the value is not null, not an empty string, and not a string containing only whitespace characters.

      See Also:
    • DEFAULT_REDIRECTION_NAME

      public static final String DEFAULT_REDIRECTION_NAME
      Default value returned when the redirection name cannot be determined.
      See Also:
  • Method Details

    • convertJsonNodeToObject

      public static Object convertJsonNodeToObject(com.fasterxml.jackson.databind.JsonNode node)
      Converts a JsonNode to its corresponding Java object type.

      The conversion follows these rules:

      • null or NullNode returns null
      • Text nodes return String
      • Boolean nodes return Boolean
      • Integer nodes return Integer
      • Long nodes return Long
      • Double or Float nodes return Double
      • Array or Object nodes return their JSON string representation
      • Any other type returns the text representation
      Parameters:
      node - the JsonNode to convert (may be null)
      Returns:
      the converted Java object, or null if the node is null or represents a JSON null
    • getValueByPath

      public static com.fasterxml.jackson.databind.JsonNode getValueByPath(com.fasterxml.jackson.databind.JsonNode rootNode, String path)
      Safely retrieves the value of a field from a JSON structure using a dot-separated path.

      This method prevents NullPointerException by safely navigating through nested objects.

      Parameters:
      rootNode - the starting JsonNode (e.g., the root of the JSON structure).
      path - the dot-separated path to the desired field (e.g., "subject", "recipients.type").
      Returns:
      the JsonNode representing the value at the specified path, or null if the path is invalid, the field does not exist, or the value is JSON null.
    • getValueByPath

      public static com.fasterxml.jackson.databind.JsonNode getValueByPath(String jsonString, String path)
      Safely retrieves the value of a field from a JSON string using a dot-separated path.

      This is a convenience method that first converts the JSON string to a JsonNode and then navigates to the specified path.

      Parameters:
      jsonString - the JSON string to parse (e.g., "{\"name\": \"John\", \"address\": {\"city\": \"Madrid\"}}").
      path - the dot-separated path to the desired field (e.g., "address.city").
      Returns:
      the JsonNode representing the value at the specified path, or null if the JSON is invalid, the path is invalid, the field does not exist, or the value is JSON null.
    • getTextValueByPath

      public static String getTextValueByPath(com.fasterxml.jackson.databind.JsonNode rootNode, String path)
      Retrieves the text value at the specified path from a JsonNode.

      This is a convenience method that navigates to the specified path and returns the value as a plain String (without JSON quotes). Unlike getValueByPath(JsonNode, String) which returns a JsonNode, this method directly returns the text representation.

      Usage Example:

      
       JsonNode root = objectMapper.readTree("{\"user\": {\"name\": \"John\"}}");
       String name = JsonNodeUtils.getTextValueByPath(root, "user.name");
       // Returns: "John" (without quotes)
       
      Parameters:
      rootNode - the starting JsonNode (e.g., the root of the JSON structure).
      path - the dot-separated path to the desired field (e.g., "user.name", "address.city").
      Returns:
      the text value at the specified path, or null if the path is invalid, the field does not exist, or the value is JSON null.
    • getTextValueByPath

      public static String getTextValueByPath(String jsonString, String path)
      Retrieves the text value at the specified path from a JSON string.

      This is a convenience method that first parses the JSON string, navigates to the specified path, and returns the value as a plain String (without JSON quotes).

      Usage Example:

      
       String json = "{\"address\": {\"city\": \"Madrid\", \"zip\": \"28001\"}}";
       String city = JsonNodeUtils.getTextValueByPath(json, "address.city");
       // Returns: "Madrid" (without quotes)
       
      Parameters:
      jsonString - the JSON string to parse (e.g., "{\"name\": \"John\"}").
      path - the dot-separated path to the desired field (e.g., "address.city").
      Returns:
      the text value at the specified path, or null if the JSON is invalid, the path is invalid, the field does not exist, or the value is JSON null.
    • convertStringToJsonNode

      public static com.fasterxml.jackson.databind.JsonNode convertStringToJsonNode(String jsonString)
      Converts a JSON string to a JsonNode.

      This method safely parses a JSON string and returns the corresponding JsonNode. If the input is null, empty, or not valid JSON, it returns null and logs the error.

      Parameters:
      jsonString - the JSON string to parse (may be null or empty).
      Returns:
      the parsed JsonNode, or null if the input is null, empty, or invalid JSON.
    • evaluateCondition

      public static boolean evaluateCondition(Object currentValue, String operator, Object expectedValue)
      Evaluates a condition comparing two values using the specified operator.

      Supported operators (case-insensitive):

      • equals or ==: equality comparison
      • notequals or !=: inequality comparison
      • contains: string containment check
      • greaterthan or >: greater than comparison
      • lessthan or <: less than comparison
      • greaterorequal or >=: greater than or equal comparison
      • lessorequal or <=: less than or equal comparison
      Parameters:
      currentValue - the current value to compare (may be null)
      operator - the comparison operator (case-insensitive)
      expectedValue - the expected value to compare against (may be null)
      Returns:
      true if the condition is satisfied, false otherwise
    • compareValues

      public static int compareValues(Comparable<?> actual, Comparable<?> expected)
      Compares two Comparable values in a type-safe manner.

      Comparison rules:

      • If both values are Number, they are converted to Double for consistent comparison
      • If both values are of the same type, they are compared directly
      • If types differ (and are not both numbers), they are compared as strings
      Parameters:
      actual - the actual value to compare (must not be null)
      expected - the expected value to compare against (must not be null)
      Returns:
      a negative integer, zero, or a positive integer as the actual value is less than, equal to, or greater than the expected value
      Throws:
      NullPointerException - if actual or expected is null
    • evaluateAllConditions

      public static boolean evaluateAllConditions(com.fasterxml.jackson.databind.JsonNode conditionsNode, BiFunction<String,String,String> dataValueResolver)
      Evaluates all conditions in a JSON array, returning true only if ALL conditions are met.

      This method is designed for evaluating process conditions where each condition references a step and field, compares against an expected value using an operator. The actual data retrieval is delegated to the caller via the dataValueResolver function.

      Each condition in the array must have the following structure:

      
       {
       "stepRef": "step_identifier",
       "variableName": "field_name",
       "variableOperator": "equals",
       "variableValue": "expected_value"
       }
       

      The method short-circuits on the first failing condition (uses allMatch).

      Parameters:
      conditionsNode - the JSON array containing condition objects (may be null or empty)
      dataValueResolver - a function that takes (fieldRef, stepRef) and returns the current data value as a String, or null if not found
      Returns:
      true if all conditions are met or if conditionsNode is null/empty, false if any condition fails or has invalid structure
    • getRedirectionName

      public static String getRedirectionName(com.fasterxml.jackson.databind.JsonNode redirection)
      Retrieves the redirection name from a JSON node, supporting both old and new data structures.

      This method provides backward compatibility by checking:

      1. New structure: parameters.name
      2. Old structure: name (directly on the node)

      If neither structure contains the name, returns "Unknown".

      Parameters:
      redirection - the JSON node containing redirection data (may be null)
      Returns:
      the redirection name, or "Unknown" if not found
    • getTargetStep

      public static String getTargetStep(com.fasterxml.jackson.databind.JsonNode redirection)
      Retrieves the target step from a JSON node, supporting both old and new data structures.

      This method provides backward compatibility by checking:

      1. New structure: parameters.targetStep
      2. Old structure: targetStep (directly on the node)

      If neither structure contains the target step, returns null.

      Parameters:
      redirection - the JSON node containing redirection data (may be null)
      Returns:
      the target step identifier, or null if not found