I have a complicated data class with a generic parameter and I would like the toString method to display the whole class in a clean way. Instead of writing my own complex toString logic, I figured I would just use the pretty printed json version.
Here is what I would like to happen:
private val json = Json {
prettyPrint = true
}
data class ComplicatedClass<E>(
val oneOfManyNestedValues: Int
) {
override fun toString(): String {
return json.encodeToString(this)
}
}
This does not work, because encodeToString really needs the type parameter to be reified. I cannot do this for the parameter T because it's a class type variable, not for a function.
Is there an idiomatic way to do this? Or do I need to give up on making my toString function the way I want?