kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Deriving external serializer for another Kotlin class

Open melvinotieno opened this issue 11 months ago • 3 comments

I have a third party class that is generated as the following

data class Example (
    property: String
)

data class AnotherExample (
    property: Example
)

I want to serialize the class AnotherExample so I am doing it as below

@OptIn(ExperimentalSerializationApi::class)
@Serializer(forClass = AnotherExample::class)
object AnotherExampleSerializer

However, during compilation, I am getting the error Serializer for element of type Example has not been found. Going by this, I need to create another custom serializer but this time for Example, which I have done the same way as I did for AnotherExample class. Problem is, how do I link the two given they are both 3rd party classes?

melvinotieno avatar Jan 06 '25 22:01 melvinotieno

There is currently no such feature. Perhaps if you use @file:UseSerializers annotation, it can be picked by the plugin.

sandwwraith avatar Jan 20 '25 15:01 sandwwraith

It is also possible to use a type alias: typealias MyExample=@Serializable(YourSerializer::class) Example

pdvrieze avatar Jan 22 '25 17:01 pdvrieze

Tried both the above suggestions but none of them seem to work unfortunately.

melvinotieno avatar Feb 27 '25 23:02 melvinotieno