nacyolsa
02/07/2023, 3:57 PMAdam S
02/08/2023, 9:07 AMnacyolsa
02/08/2023, 9:10 AMAdam S
02/08/2023, 9:15 AMnacyolsa
02/08/2023, 9:16 AMmolikuner
02/08/2023, 1:26 PM@Serializable(with = MyClass.Serializer::class)
data class MyClass(val prop: Int) {
@Serializer(forClass = MyClass::class)
object Serializer : KSerializer<MyClass> {
override fun deserialize(decoder: Decoder): MyClass {
// custom logic here
}
}
}
(I've not tested it in a IDE and just wrote in Slack. Probably there is some syntax error or so)
You might ask, why this works. Well, the @Serializer
annotation generates the KSerializer, but skips those methods, that are implemented manually. In this case the deserialize method is defined manually, so the plugin just generates the serialize method and the descriptor.nacyolsa
02/08/2023, 3:21 PMdeserialize()
I'm adding additional things and at the end I would like to call there generated deserializer. At this moment I have it solved by created second data class with the same model. Is there a better way to achieve this?molikuner
02/08/2023, 3:26 PM@Serializer(forClass = MyClass::class)
private object OriginalSerializer : KSerializer<MyClass>
Inside the deserialize
from the Serializer
object, you can then use OriginalSerializer.deserialize(decoder)
.