1010 * See the License for the specific language governing permissions and
1111 * limitations under the License.
1212 */
13- package org .flowable .common .engine .impl . util ;
13+ package org .flowable .common .engine .impl ;
1414
1515import java .time .Duration ;
1616import java .time .Instant ;
2222import java .util .Date ;
2323
2424import org .flowable .common .engine .api .FlowableException ;
25-
26- import tools .jackson .databind .JsonNode ;
27- import tools .jackson .databind .ObjectMapper ;
28- import tools .jackson .databind .node .ArrayNode ;
29- import tools .jackson .databind .node .ObjectNode ;
25+ import org .flowable .common .engine .impl .json .FlowableJsonNode ;
26+ import org .flowable .common .engine .impl .json .VariableJsonMapper ;
27+ import org .flowable .common .engine .impl .util .DateUtil ;
28+ import org .flowable .common .engine .impl .util .JsonUtil ;
3029
3130/**
32- * Utility class for converting variable values between types.
33- * Used by IO parameter processing in both the BPMN and CMMN engines.
31+ * Default implementation of {@link VariableValueConversionHandler}.
3432 *
3533 * @author Tijs Rademakers
3634 */
37- public class VariableValueConversionUtil {
35+ public class DefaultVariableValueConversionHandler implements VariableValueConversionHandler {
3836
39- /**
40- * Convert a value to the specified type.
41- *
42- * @param value the value to convert (must not be null)
43- * @param type the target type name (e.g. "string", "integer", "date", "json")
44- * @param objectMapper the ObjectMapper to use for JSON/array conversions (may be null if not converting to json/array)
45- * @return the converted value
46- */
47- public static Object convertValue (Object value , String type , ObjectMapper objectMapper ) {
37+ @ Override
38+ public Object convertValue (Object value , String type , VariableJsonMapper variableJsonMapper ) {
4839 return switch (type .toLowerCase ()) {
4940 case "string" -> convertToString (value );
5041 case "integer" , "int" -> convertToInteger (value );
@@ -53,17 +44,18 @@ public static Object convertValue(Object value, String type, ObjectMapper object
5344 case "boolean" -> convertToBoolean (value );
5445 case "date" -> convertToDate (value );
5546 case "localdate" -> convertToLocalDate (value );
56- case "json" -> convertToJson (value , objectMapper );
57- case "array" -> convertToArray (value , objectMapper );
47+ case "json" -> convertToJson (value , variableJsonMapper );
48+ case "array" -> convertToArray (value , variableJsonMapper );
5849 default -> throw new FlowableException ("Unsupported IO parameter type '" + type + "'" );
5950 };
6051 }
6152
62- public static String convertToString (Object value ) {
53+ protected String convertToString (Object value ) {
6354 if (value instanceof String stringValue ) {
6455 return stringValue ;
6556 }
66- if (value instanceof JsonNode jsonNode ) {
57+ if (value != null && JsonUtil .isJsonNode (value )) {
58+ FlowableJsonNode jsonNode = JsonUtil .asFlowableJsonNode (value );
6759 if (jsonNode .isString ()) {
6860 return jsonNode .asString ();
6961 }
@@ -72,7 +64,7 @@ public static String convertToString(Object value) {
7264 return value .toString ();
7365 }
7466
75- public static Integer convertToInteger (Object value ) {
67+ protected Integer convertToInteger (Object value ) {
7668 if (value instanceof Integer intValue ) {
7769 return intValue ;
7870 }
@@ -85,7 +77,7 @@ public static Integer convertToInteger(Object value) {
8577 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to Integer" );
8678 }
8779
88- public static Long convertToLong (Object value ) {
80+ protected Long convertToLong (Object value ) {
8981 if (value instanceof Long longValue ) {
9082 return longValue ;
9183 }
@@ -98,7 +90,7 @@ public static Long convertToLong(Object value) {
9890 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to Long" );
9991 }
10092
101- public static Double convertToDouble (Object value ) {
93+ protected Double convertToDouble (Object value ) {
10294 if (value instanceof Double doubleValue ) {
10395 return doubleValue ;
10496 }
@@ -111,7 +103,7 @@ public static Double convertToDouble(Object value) {
111103 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to Double" );
112104 }
113105
114- public static Boolean convertToBoolean (Object value ) {
106+ protected Boolean convertToBoolean (Object value ) {
115107 if (value instanceof Boolean booleanValue ) {
116108 return booleanValue ;
117109 }
@@ -121,7 +113,7 @@ public static Boolean convertToBoolean(Object value) {
121113 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to Boolean" );
122114 }
123115
124- public static Date convertToDate (Object value ) {
116+ protected Date convertToDate (Object value ) {
125117 if (value instanceof Date dateValue ) {
126118 return dateValue ;
127119 }
@@ -147,7 +139,7 @@ public static Date convertToDate(Object value) {
147139 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to Date" );
148140 }
149141
150- public static LocalDate convertToLocalDate (Object value ) {
142+ protected LocalDate convertToLocalDate (Object value ) {
151143 if (value instanceof LocalDate localDate ) {
152144 return localDate ;
153145 }
@@ -173,25 +165,39 @@ public static LocalDate convertToLocalDate(Object value) {
173165 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to LocalDate" );
174166 }
175167
176- public static ObjectNode convertToJson (Object value , ObjectMapper objectMapper ) {
177- if (value instanceof ObjectNode objectNode ) {
178- return objectNode ;
168+ protected Object convertToJson (Object value , VariableJsonMapper variableJsonMapper ) {
169+ if (value != null && JsonUtil . isObjectNode ( value ) ) {
170+ return value ;
179171 }
180172 if (value instanceof String stringValue ) {
181- JsonNode jsonNode = objectMapper .readTree (stringValue );
182- if (jsonNode instanceof ObjectNode objectNode ) {
183- return objectNode ;
173+ Object jsonNode = variableJsonMapper .readTree (stringValue );
174+ if (JsonUtil . isObjectNode ( jsonNode ) ) {
175+ return jsonNode ;
184176 }
185177 throw new FlowableException ("JSON string does not represent an object: " + stringValue );
186178 }
187179 throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to JSON object" );
188180 }
189181
182+ protected Object convertToArray (Object value , VariableJsonMapper variableJsonMapper ) {
183+ if (value != null && JsonUtil .isArrayNode (value )) {
184+ return value ;
185+ }
186+ if (value instanceof String stringValue ) {
187+ Object jsonNode = variableJsonMapper .readTree (stringValue );
188+ if (JsonUtil .isArrayNode (jsonNode )) {
189+ return jsonNode ;
190+ }
191+ throw new FlowableException ("JSON string does not represent an array: " + stringValue );
192+ }
193+ throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to JSON array" );
194+ }
195+
190196 /**
191197 * Parses an ISO 8601 duration/period string (e.g. "P10D", "PT10H", "P1Y2M3DT4H") and adds it to the current time.
192198 * Uses the same parsing logic as {@link org.flowable.common.engine.impl.calendar.DueDateBusinessCalendar}.
193199 */
194- protected static ZonedDateTime addDurationToNow (String durationString ) {
200+ protected ZonedDateTime addDurationToNow (String durationString ) {
195201 ZonedDateTime now = ZonedDateTime .now ();
196202 Period period ;
197203 Duration duration ;
@@ -211,18 +217,4 @@ protected static ZonedDateTime addDurationToNow(String durationString) {
211217 return now .plus (period ).plus (duration );
212218 }
213219
214- public static ArrayNode convertToArray (Object value , ObjectMapper objectMapper ) {
215- if (value instanceof ArrayNode arrayNode ) {
216- return arrayNode ;
217- }
218- if (value instanceof String stringValue ) {
219- JsonNode jsonNode = objectMapper .readTree (stringValue );
220- if (jsonNode instanceof ArrayNode arrayNode ) {
221- return arrayNode ;
222- }
223- throw new FlowableException ("JSON string does not represent an array: " + stringValue );
224- }
225- throw new FlowableException ("Cannot convert value of type " + value .getClass ().getName () + " to JSON array" );
226- }
227-
228220}
0 commit comments