snowe
10/15/2021, 4:24 PMresponse:
productType: one | two
nestedClassOne:
anotherType: A | B
nestedClassTwo:
type that only shows up if productType is One
or
type that only shows up if productType is Two
nestedClassThree:
type that only shows up if anotherType is A
type that only shows up if anotherType is B
snowe
10/15/2021, 4:43 PMsnowe
10/15/2021, 9:08 PM@InheritableSerialInfo
?snowe
10/15/2021, 10:02 PMephemient
10/15/2021, 11:50 PMsnowe
10/15/2021, 11:53 PMpdvrieze
10/18/2021, 10:09 AMsnowe
10/18/2021, 4:02 PMpdvrieze
10/19/2021, 8:53 AM@Serializable(Response.Companion::class)
class Response(
val productType: PTBase
) {
private class SerialDelegate(
val anotherType: PTBase
val nestedClassTwo: NestedDelegate2
val nestedClassThree: NestedDelegate3Base
) {
constructor(response: Response): this(/*...*/)
fun toResponse(): Response = TODO()
}
companion object: KSerializer<Response> {
override val serialDescriptor: SerialDescriptor = TODO()
override fun serialize(encoder: Encoder, value: Response) {
SerialDelegate.serializer().serialize(encoder, SerialDelegate(value))
}
override fun deserialize(decoder: Decoder): Response {
return SerialDelegate.serializer().deserialize(decoder).toResponse()
}
}
}
internal class NestedDelegate2( /* properties for both variants */)
internal class NestedDelegate3( /* properties for both variants */)
Where the NestedDelegate2
is an internal classes for the inner types that has properties for both. Then in toResponse
you can verify the properties on correctness. Note that the properties have to be nullable/optional.snowe
10/19/2021, 8:28 PMpdvrieze
10/19/2021, 8:33 PM