Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“Serializer for class is not found” error in Kotlin script even though @Serializable annotation is added #2617

Open
unclechu opened this issue Mar 27, 2024 · 2 comments
Labels

Comments

@unclechu
Copy link

Describe the bug

I have a simple Kotlin script (foo.kts):

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.encodeToString

@Serializable
data class Foo(val a: Int, val b: String)

val str: String = Json.encodeToString(Foo(42, "test"))
println(str)

val foo: Foo = Json.decodeFromString<Foo>("{\"a\":42,\"b\":\"test\"}")
println(foo)

I start it like this:

kotlin -classpath "$(./gradlew -q printDependenciesClasspath)" ./test-kotlin-script-json.kts

Where in Gradle I have this task:

tasks.register("printDependenciesClasspath") {
  println(
    configurations.runtimeClasspath.get()
    .filter { it.name.endsWith("jar") }
    .map { file(it) }
    .joinToString(":")
  )
}

This is a bit fancy, just wanted to set the CLASSPATH and avoid constantly invoking Gradle but still have dependencies included for the script. The problem is that I keep getting this error:

kotlinx.serialization.SerializationException: Serializer for class 'Foo' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
        at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
        at kotlinx.serialization.internal.PlatformKt.platformSpecificSerializerNotRegistered(Platform.kt:28)
        at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:134)
        at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
        ...

Even though @Serializable annotation is added for the class.

Environment

  • Kotlin version: Kotlin version 1.9.255-SNAPSHOT (JRE 21+35-nixos)
  • Library version: 1.6.0
  • Kotlin platforms: JVM
  • Gradle version: 8.4
@sandwwraith
Copy link
Member

sandwwraith commented Mar 28, 2024

The answer to your question is in the second part of the exception message:

and that the serialization compiler plugin is applied.

Given that you're invoking the compiler manually, passing -Xplugin argument to it is required. Serialization compiler plugin is a separate jar that is not a part of runtime. You can find it using these coordinates: https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-serialization-compiler-plugin or in ${KOTLIN_HOME}/lib/kotlin-serialization-compiler-plugin.jar

@sandwwraith
Copy link
Member

This ticket contains more information: https://youtrack.jetbrains.com/issue/KT-47384

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants