Clyde
Clyde

Reputation: 7549

Exception when deserializing JSON in Kotlin JS app

I'm trying to get a Kotlin JS app working, and when consuming data from a server using this code:

val client = HttpClient {
    install(JsonFeature) {
        serializer =  KotlinxSerializer()

    }
}

@Serializable
data class Entry(val start: String, val end: String)

suspend fun loadData() {
    val data = client.get<List<Entry>>("http://localhost:8080/data") {
        accept(ContentType.Application.Json)
    }
    console.log(data)
}

I get exceptions like this:

Serializer for class 'Entry' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation

even though the class is marked as @Serializable.

If I change it to client.get<List<Map<String,String>>> then I get a valid result.

What am I doing wrong?

Upvotes: 1

Views: 107

Answers (1)

Clyde
Clyde

Reputation: 7549

I needed to add this to build.gradle.kts:

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

Upvotes: 1

Related Questions