deepCody

fun <T : Any> T.deepCody(objectType: Class<T> = this.javaClass): T

Creates a deep copy of this object by serializing to JSON and deserializing back.

This method performs a deep copy by converting the object to JSON and then parsing it back, ensuring that all nested objects are also copied.

Receiver

The object to deep copy.

Return

A deep copy of the object.

Parameters

T

The type of the object being copied.

objectType

The Class representing the type to copy to. Defaults to the object's class.

Throws

JsonProcessingException

if serialization or deserialization fails.

Example:

data class User(val name: String, val address: Address)
val original = User("John", Address("123 Main St"))
val copy = original.deepCopy()
// copy is a separate instance with copied nested objects