dev.bpmcrafters.processengine.worker.registrar.VariableConverter.kt Maven / Gradle / Ivy
package dev.bpmcrafters.processengine.worker.registrar
import com.fasterxml.jackson.databind.ObjectMapper
/**
* Helper converting variables from a JSON map to type using Jackson.
* @since 0.0.3
*/
open class VariableConverter(
private val objectMapper: ObjectMapper
) {
/**
* Reads from the map-tree structure to a target type.
* @param value map-tree structure.
* @param type target class.
* @return cast value.
*/
open fun mapToType(value: Any?, type: Class): T {
return if (value != null && !type.isInstance(value)) {
objectMapper.readValue(objectMapper.writeValueAsString(value), type)
} else {
type.cast(value)
}
}
}