24

I have the following data class

@Serializable
data class Income(val id: String, 
                  val description: String, 
                  val amount: Int, 
                  val Time: Date, 
                  val userId: String)

now when I try to use the .serializer() function it says that the .serializer() is not defined for Income class so my project doesn't compile.

    val response = Json.stringify(Income.serializer(), Incomes)
    call.respond(HttpStatusCode.OK,response) 

I Looked twice on the documentation in the readme.md. Even watched the announcement video from KotlinConf

Did anyone have the same problem. What am i doing wrong??

Edit:

I tried to just copy paste the samples from the readme.md and had the same problem.

import kotlinx.serialization.*
import kotlinx.serialization.json.*

@Serializable
data class Data(val a: Int, val b: String = "42")

fun main() {
    // Json also has .Default configuration which provides more reasonable settings,
    // but is subject to change in future versions
    val json = Json(JsonConfiguration.Stable)
    // serializing objects
    val jsonData = json.stringify(Data.serializer(), Data(42))
    // serializing lists
    val jsonList = json.stringify(Data.serializer().list, listOf(Data(42)))
    println(jsonData) // {"a": 42, "b": "42"}
    println(jsonList) // [{"a": 42, "b": "42"}]

    // parsing data back
    val obj = json.parse(Data.serializer(), """{"a":42}""") // b is optional since it has default value
    println(obj) // Data(a=42, b="42")
}

This does not compile as well in my code. I'm currently using Kotlin 1.3..61 and kotlinx-serialization-runtime 0.14.0

yonBav
  • 1,695
  • 2
  • 17
  • 31

2 Answers2

36

In addition to the kotlinx-serialization-runtime dependency you also need to add the plugin

plugins {
    kotlin("multiplatform") // or kotlin("jvm") or any other kotlin plugin
    kotlin("plugin.serialization") version "1.4.10"
}

with the same version as Kotlin itself.

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 4
    In order to get rid of the error on ktor I needed to add id 'org.jetbrains.kotlin.plugin.serialization' version '1.4.10' to the plugins section of my build.gradle file – Nicola Gallazzi Nov 17 '20 at 10:46
  • Bit late to the party here, but any chance you can explain to me why we need both the plugin and the dependency to be able to run this code? My limited understanding of Gradle is that plugins provide tasks to be used during the build process itself, whereas dependencies represent the classpath to be used during compilation and/or runtime. Not including this plugin doesn't prevent the code from being compiled, but it's necessary for it to run. Why? – Pastafarian Jan 14 '21 at 22:12
  • The question says it does prevent the code from being compiled: "it says that the .serializer() is not defined for Income class so my project doesn't compile". If in your case it doesn't, this is surprising to me as well. – Alexey Romanov Jan 15 '21 at 08:08
  • Hmm. Even assuming that the code doesn't compile without including the plugin, why would the compilation of any code be dependent on the plugins in a Gradle build file? – Pastafarian Jan 15 '21 at 09:42
  • Why wouldn't it? Plugins can: 1) generate code which is needed by the code the user writes; 2) change compilation options; 3) in Kotlin's case, activate compiler plugins. All of these can affect compilation. – Alexey Romanov Jan 15 '21 at 11:26
  • Thank you - I had the same problem with a quarkus project, and the addition of the serialization plugin fixed it for me. Thanks – Nathan Russell May 16 '22 at 08:34
4

I had a similar problem that my Navigation Drawer XML file didn't recognize my serializable class, for that I had to extend the java.io.Serializable myself, bit for kotlin it was fine:

@Serializable
data class Income(val id: String, 
                  val description: String, 
                  val amount: Int, 
                  val Time: Date, 
                  val userId: String) : java.io.Serializable
Boken
  • 4,825
  • 10
  • 32
  • 42
Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38