Kotlin – Convert object of a Data Class to another Data Class object

In this Kotlin tutorial, I will show you two ways to transform/convert object of a Data Class to another Data Class object.

Overview

Assume that you want to do object mapping, or convert a Data Class object to another one.
For example, we have Customer data class like this:

data class Customer(
  val firstName: String,
  val lastName: String,
  val phone: String,
  val age: Int,
  val location: String
)

What we need is to transform/convert Customer object to CustomerRecord object which is defined by the following data class:

data class CustomerRecord(
  val fullName: String, // firstName + lastName
  val contact: String,  // location + phone
  val age: Int
)

How to do this?
Let’s explore all of them right now.

Using Kotlin extension Function

We’re gonna create an extension Function to map Customer -> CustomerRecord.

fun Customer.toCustomerRecord() = CustomerRecord(
  fullName = "$firstName $lastName",
  contact = "$location $phone",
  age = age
)

Run the code below to check it:

fun main(args: Array) {
  val customer = Customer("zKoder", "Guy", "(+90)12345678", 22, "Happy Island")

  val record = customer.toCustomerRecord();
  println(record)
}

Output:

CustomerRecord(fullName=zKoder Guy, contact=Happy Island (+90)12345678, age=22)

Using Kotlin Reflection

In case the source class is a boilerplate class, most of its fields will be mapped to the same field name in the target class.

For example, Customer and CustomerRecord could be like this:

data class Customer(
  val firstName: String,
  val lastName: String,
  val phone: String,
  val location: String,
  val age: Int,
  // 30 fields here
)

data class CustomerRecord(
  val fullName: String,
  val contact: String,
  val age: Int
  // 30 fields here (the same as Customer's fields)
)

Add Kotlin Reflection

We need to import Kotlin Reflection to our project first.

  1. Add the kotlin-reflect dependency
  2. implementation "org.jetbrains.kotlin:kotlin-reflect"
  3. import memberProperties function
  4. import kotlin.reflect.full.memberProperties

Create extension Function

Let’s use Kotlin Reflection in an extension Function as the following code:

import kotlin.reflect.full.memberProperties

fun Customer.toCustomerRecordRefl() = with(::CustomerRecord) {
  val propertiesByName = Customer::class.memberProperties.associateBy { it.name }
  
  callBy(args = parameters.associate { parameter ->
    parameter to when (parameter.name) {
      "fullName" -> "$firstName $lastName"
      "contact" -> "$location $phone"
      else -> propertiesByName[parameter.name]?.get(this@toCustomerRecordRefl)
    }
  })
}

Now we can implement object mapping easily.

fun main(args: Array) {
  val customer = Customer("zKoder", "Guy", "(+90)12345678", "Happy Island", 22, ...)

  val record = customer.toCustomerRecordRefl();
  println(record)
}

Conclusion

This tutorial introduces two ways to convert object of a Data Class to another Data Class object:
– Simple and fast: using Kotlin extension Function
– For many replica fields (not for performance): using Kotlin Reflection

Depending on your case, you can choose one of them.
Happy learning! See you again.

Further Reading