Manuel Dossinger
08/25/2020, 12:13 PMinterface WithValue { val x: Int }
@Serializable
data class WithValueImpl(override val x: Int) : WithValue
@Serializable
data class MyClass(val y: Int) : WithValue by WithValueImpl(1)
fun main() {
println(Json.encodeToString(MyClass(5)))
// prints: {"y":5}
}
In the documentation, I have only found that delegated properties are transient, but nothing regarding delegated interfaces. The desired result would be, that MyClass is encoded as {"y": 5, "x": 1}
. Do you have any pointers for me?TwoClocks
08/26/2020, 4:00 AMManuel Dossinger
08/26/2020, 9:41 AM@Serializable annotation is ignored because it is impossible to serialize automatically interfaces or enums. Provide serializer manually via e.g. companion object
Manuel Dossinger
08/26/2020, 2:37 PMinterface WithValue { val x: Int }
@Serializable
data class WithValueImpl(override val x: Int) : WithValue
@Serializable
data class MyClass(val y: Int, val impl: WithValueImpl = WithValueImpl(1)) : WithValue by impl
but now I have .x
or .impl.x
for accessing the same value, this does not look that beautifulRyan Simon
11/17/2020, 10:48 PMRyan Simon
11/17/2020, 10:48 PM