convert

fun <T> Any.convert(targetType: Class<T>): T

Converts this object to the specified target type.

Uses the pre-configured JsonSerializer to convert the object.

Receiver

The object to convert.

Return

The converted object.

Example:

data class User(val name: String, val age: Int)
data class UserDto(val userName: String, val userAge: Int)
val user = User("John", 30)
val dto = user.convert(UserDto::class.java)

Parameters

T

The type of object to convert to.

targetType

The Class representing the target type.


fun <T> Any.convert(targetType: JavaType): T

Converts this object to the specified target type.

Uses the pre-configured JsonSerializer to convert the object.

Receiver

The object to convert.

Return

The converted object.

Example:

val json = """{"name":"John","age":30}"""
val node = json.toJsonNode<ObjectNode>()
val type = JsonSerializer.typeFactory.constructMapType(HashMap::class.java, String::class.java, Any::class.java)
val map = node.convert<MutableMap<String, Any>>(type)

Parameters

T

The type of object to convert to.

targetType

The JavaType representing the target type.


fun <T> Any.convert(targetType: TypeReference<T>): T

Converts this object to the specified target type.

Uses the pre-configured JsonSerializer to convert the object.

Receiver

The object to convert.

Return

The converted object.

Example:

data class User(val name: String, val addresses: List<Address>)
val user = User("John", listOf(Address("123 Main St")))
val typeRef = object : TypeReference<User>() {}
val converted = user.convert(typeRef)

Parameters

T

The type of object to convert to.

targetType

The TypeReference representing the target type.


inline fun <T> Any.convert(): T

Converts this object to the specified target type using reified generics.

Convenience method that uses the reified type parameter to avoid specifying the class explicitly.

Receiver

The object to convert.

Return

The converted object.

Example:

data class User(val name: String, val age: Int)
data class UserDto(val userName: String, val userAge: Int)
val user = User("John", 30)
val dto = user.convert<UserDto>()

Parameters

T

The type of object to convert to, inferred from the call site.