That is an interesting new use-case for Kotlin Serialization. See general discussion here: Kotlin Serialization - #21 by mikehearn
In fact, existing Kotlin Serialization prototype is powerful enough so that what you are describing can be implemented with few lines of code. The following code actually works in the prototype compiler with serialization support:
@KSerializable
data class Person(val firstName:String, val lastName:String)
object LowercaseTransformer : ElementValueTransformer() {
override fun transformStringValue(desc: KSerialClassDesc, index: Int, value: String): String =
value.toLowerCase()
}
fun main(args: Array<String>) {
val p = Person("Dart", "Vader")
println("Original person: $p")
val q = LowercaseTransformer.transform(p)
println("Transformed person: $q")
}
Produces
Original person: Person(firstName=Dart, lastName=Vader)
Transformed person: Person(firstName=dart, lastName=vader)
It already works for quite complex structures inside the Person class, including nested data objects, lists, maps, arrays, etc. ElementValueTransformer
is a helper class that serializes the object into internal representation (flattened array list of elements) and deserializes it back, while applying the specified transformation. It can be also used to clone serializable objects.
It is just a prototype, though. A usual disclaimer is that, of course, everything is subject to change (and will change).