This may not necessarily be bug, any reference in documentation would be helpful. When compiling the example as per the blog announcement
@Serializable
data class Project(
val name: String,
val owner: Account,
val group: String = "R&D"
)
@Serializable
data class Account(val userName: String)
val moonshot = Project("Moonshot", Account("Jane"))
val cleanup = Project("Cleanup", Account("Mike"), "Maintenance")
fun main() {
val string = Json.encodeToString(listOf(moonshot, cleanup))
println(string)
val projectCollection = Json.decodeFromString<List<Project>>(string)
println(projectCollection)
}
```
it works with Gradle `run` task (`application` plugin applied). However When I convert application to a graalvm native image, the compilation succeeds (and binary native image is generated) but when I run the native image, I get the following error:
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Project' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:29)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:59)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
at kotlinx.serialization.SerializersKt__SerializersKt.builtinSerializerOrNull$SerializersKt__SerializersKt(Serializers.kt:79)
at kotlinx.serialization.SerializersKt__SerializersKt.serializerByKTypeImpl$SerializersKt__SerializersKt(Serializers.kt:69)
at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:54)
at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
```
To Reproduce
Just run compile the example using graalvm native image.
Expected behavior
Since I assume, kotlinx.serialization does not use reflection at runtime, the native image should have worked properly.
If there is something needed in reflection config, please update the docs.
Environment
Thanks!
Since I assume, kotlinx.serialization does not use reflection at runtime,
kotlinx.serialization uses reflection is exactly one place -- serializer() function to lookup a serializer.
This function is used in reified methods that do not accept serializer as a parameter.
We are going to change that soon (around Kotlin 1.4.30), but now as a temporary workaround I'd suggest to use Json.encodeToString(ListSerializer(Project.serializer()), listOf(moonshot, cleanup)) and Json.decodeFromString(ListSerializer(Project.serializer(), string) respectively.
Thanks @qwwdfsad for quick response. I can confirm that the workaround works with GraalVM Native Image 馃憤
Looking forward for kotlin 1.4.30 though :)
Just ran into this, and the workaround is mildly annoying -- can we keep this open until
We are going to change that soon
this happens? Thanks!
Most helpful comment
Just ran into this, and the workaround is mildly annoying -- can we keep this open until
this happens? Thanks!