Hi,
What's the best way to abstract out the fromJson functionality so that it can be re-used? There doesn't seem to be a way to do it using interfaces that I can find...
Copy code
data class MyClass(val type: String) {
companion object {
fun fromJson(j: String) = Gson().fromJson(j, MyClass::class.java)
}
}
data class MyOtherClass(val type: String) {
companion object {
fun fromJson(j: String) = Gson().fromJson(j, MyOtherClass::class.java)
}
}
m
Milan Hruban
07/04/2020, 6:57 AM
not sure what exactly you want but this seems pretty reusable
Copy code
inline fun <reified T> String.fromJson(): T = Gson().fromJson(this, T::class.java)
t
Tony Blundell
07/04/2020, 12:33 PM
thanks, i was looking for valid syntax to include the function in an interface, but this works too