Convert Java/Kotlin object to another object using Gson

fun <T> Any.convert(classOfT: Class<T>): T = Gson().fromJson(Gson().toJson(this), classOfT)
class User(val name: String)class SystemUser(val name: String)
val user : User = User("Alien")val systemUser = user.convert(SystemUser::class.java)

--

--

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
Aslam Anver

Aslam Anver

Passionate in AI Deep Learning, Find me on GitHub & StackOverflow