I'm upgrading to Kotlin 2.0.0 and serialization 1....
# serialization
r
I'm upgrading to Kotlin 2.0.0 and serialization 1.7.0-RC. I have an issue when using
@file:UseContextualSerialization
with expect/actual typealias. More in thread.
I'm using a serializable type, which is declared using expect in common module and implemented as actual typealias on jvm target.
common:
Copy code
expect class LocalDateTime
jvm:
Copy code
actual typealias LocalDateTime = java.time.LocalDateTime
The type is serlializable with a custom serializer registered in my app.
With Kotlin 1.9, I've been using
@file:UseContextualSerialization
to define serializable classes in common code using my type, e.g.:
Copy code
@file:UseContextualSerialization(LocalDateTime::class)

package com.example

import kotlinx.serialization.Serializable
import kotlinx.serialization.UseContextualSerialization
import io.kvision.types.LocalDateTime

@Serializable
data class Address(
    val id: Int? = 0,
    val firstName: String? = null,
    val lastName: String? = null,
    val createdAt: LocalDateTime? = null,
)
But with Kotlin 2.0.0 it doesn't work anymore.
The compiler somehow resolves my common type to a platform type and fails with an error:
Copy code
Serializer has not been found for type 'java.time.LocalDateTime?'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
It still works correctly when using
@Contextual
annotation directly on the property:
Copy code
@Serializable
data class Address(
    val id: Int? = 0,
    val firstName: String? = null,
    val lastName: String? = null,
    @Contextual val createdAt: LocalDateTime? = null,
)
but it's a problem when I have many classes with many fields.
Is this a known issue?
Should I fill a bug report?