toObject

fun <T> String.toObject(objectType: JavaType): T

Deserializes this JSON string into an object of the specified JavaType.

Uses the pre-configured JsonSerializer to read the value.

Receiver

The JSON string to deserialize.

Return

The deserialized object.

Parameters

T

The type of object to deserialize to.

objectType

The JavaType representing the target type.

Throws

JsonProcessingException

if deserialization fails.

Example:

val json = """["John","Jane"]"""
val type = JsonSerializer.typeFactory.constructCollectionType(List::class.java, String::class.java)
val list = json.toObject<List<String>>(type)

fun <T> String.toObject(objectType: Class<T>): T

Deserializes this JSON string into an object of the specified Class.

Uses the pre-configured JsonSerializer to read the value.

Receiver

The JSON string to deserialize.

Return

The deserialized object.

Parameters

T

The type of object to deserialize to.

objectType

The Class representing the target type.

Throws

JsonProcessingException

if deserialization fails.

Example:

data class User(val name: String, val age: Int)
val json = """{"name":"John","age":30}"""
val user = json.toObject<User>(User::class.java)

fun <T> JsonNode.toObject(objectType: Class<T>): T

Converts this JsonNode into an object of the specified Class.

Uses the pre-configured JsonSerializer to convert the tree to value.

Receiver

The JsonNode to convert.

Return

The converted object.

Parameters

T

The type of object to convert to.

objectType

The Class representing the target type.

Throws

JsonProcessingException

if conversion fails.

Example:

val json = """{"name":"John","age":30}"""
val node = json.toJsonNode<ObjectNode>()
val user = node.toObject<User>(User::class.java)

inline fun <T> String.toObject(): T

Deserializes this JSON string into an object using reified generics.

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

Receiver

The JSON string to deserialize.

Return

The deserialized object.

Parameters

T

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

Throws

JsonProcessingException

if deserialization fails.

Example:

data class User(val name: String, val age: Int)
val json = """{"name":"John","age":30}"""
val user = json.toObject<User>()

inline fun <T> JsonNode.toObject(): T

Converts this JsonNode into an object using reified generics.

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

Receiver

The JsonNode to convert.

Return

The converted object.

Parameters

T

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

Throws

JsonProcessingException

if conversion fails.

Example:

val json = """{"name":"John","age":30}"""
val node = json.toJsonNode<ObjectNode>()
val user = node.toObject<User>()