Here is the easiest way to deep copy an object in Kotlin

dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
fun deepCopy():MyCustomClass {
val JSON = Gson().toJson(this)
return Gson().fromJson(JSON, MyCustomClass::class.java)
}
class MyCustomClass{//your object propertiesfun deepCopy():MyCustomClass {
val JSON = Gson().toJson(this)
return Gson().fromJson(JSON, MyCustomClass::class.java)
}
}
var object1 = MyCustomClass()
var object2 = MyCustomClass()
object1 = object2.deepCopy()

--

--

Produtor Musical, Compositor e Arranjador. Casado e pai de duas meninas. Desajustado e nerd.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store