Thread
#serialization
    Amaan

    Amaan

    7 months ago
    I'm trying to compile with generic classes like in the documentation, but I can't seem to get it to work. https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/basic-serialization.md#generic-classes
    Here is what my code looks like in my scratch.kts file when I try to follow along:
    import kotlinx.serialization.Serializable
    import kotlinx.serialization.encodeToString
    import kotlinx.serialization.json.Json
    
    @Serializable
    class Box<T>(val contents: T)
    
    @Serializable
    class Data(
        val a: Box<Int>,
        val b: Box<String>
    )
    
    val data = Data(Box(42), Box("Kotlin"))
    println(Json.encodeToString(data))
    And this is my error output:
    Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Data' 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:60)
    	at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
    	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated$ScratchFileRunnerGenerated.generated_get_instance_res0(tmp.kt:39)
    	at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt:30)
    Oliver.O

    Oliver.O

    7 months ago
    Did you forget to configure the Gradle plugin (in addition to the library dependency)? A multiplatform example:
    plugins {
        kotlin("multiplatform") version "1.6.10"
        kotlin("plugin.serialization") version "1.6.10"
        application
    }
    Amaan

    Amaan

    7 months ago
    I already have it working for non-generics since I'm using it in Ktor.
    Oliver.O

    Oliver.O

    7 months ago
    I have verified that your code compiled and runs fine with a Gradle build configured as described above.