How do I add serialization into Kotlin Jet Compose project?

30 views Asked by At

I'm new to Kotlin and IntelliJ overall, and I'm trying to add the serialization libraries into a Jet Compose project, but editing the build.gradle.kts doesn't seem to add them properly. I'm not sure if the versions I'm using aren't compatible or if I'm not adding them correctly, but when I write a data class like this:

import kotlinx.serialization.Serializable

@Serializable
data class Cuenta(val id: String, val pagina: String, val email: String, val usuario: String, val cont: String)

it doesn't recognize the library, therefore the @Serializable is also not recognized.

What am I doing wrong and how can I make it work?

Or, if this doesn't work, is there other ways that I can serialize data classes in Kotlin Jet Compose?

Currently my build.gradle looks like this:

import org.jetbrains.compose.desktop.application.dsl.TargetFormat

plugins {
    kotlin("jvm")
    id("org.jetbrains.compose")
    kotlin("plugin.serialization") version "1.6.0"
}

group = "com.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
    google()
}

dependencies {
    implementation(compose.desktop.currentOs)
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
}

compose.desktop {
    application {
        mainClass = "MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "PassGes2"
            packageVersion = "1.0.0"
        }
    }
}

2

There are 2 answers

0
Simon Jacobs On

The Kotlin serialization plugin and the JSON library follow a different versioning system (docs). The first's version should match the version of Kotlin that is being used, and the second chosen as whatever version is released as being compatible with that.

The latest version of Kotlin is 1.9.23, so assuming you are using that, you need to update the Kotlin serialization plugin to match it:

kotlin("plugin.serialization") version "1.9.23"

I suggest that you confirm that this is the same as the Kotlin version you are using. I expect that you will either find this version in the plugins block in the build.gradle.kts file in the root folder of your project, or the definition of the version will be directly referenced there.

For the JSON library, 1.6.x versions go with the 1.9.x Kotlin versions (docs). The very latest is 1.6.3, which you might as well use:

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3")
0
Mic O On

This happened to me and after much research, i solved it by enabling Early Access Preview and downloading the latest kotlin version, go to file -> Settings -> then search eap or early access,

Move out of stable and download the eap build