Questions tagged [kotlinx.serialization]

Kotlin serialization generated code to serialize objects without reflection only by marking a class with @Serializable annotation.

Kotlin serialization consists of a compiler plugin, which automatically produces visitor code for classes, and runtime library, which uses generated code to serialize objects without reflection.

  • Supports Kotlin classes marked as @Serializable and standard collections.
  • Supports JSON, CBOR, and Protobuf formats out-of-the-box.
  • The same code works on Kotlin/JVM, Kotlin/JS, and Kotlin/Native
262 questions
2
votes
0 answers

kotlinx.serialization Decode object from map value taking ID from the map key

Using koltinx.serialization, is there a way to enable the "value" serializer to take it's ID from the "key" of the map entry? For example, given the following JSON: { "data": { "key1": { "name": "value1" }, "key2": { "name": "value2" }, …
Brett
  • 298
  • 2
  • 16
2
votes
1 answer

Issue With Deserialization Json Array with Ktor

I am trying to use Ktor and Kotlinx Serialization to pull some dummy post data form jsonplaceholder.typicode.com (here) and deserialize the array. However, I get the following error: Error:Expected class kotlinx.serialization.json.JsonObject (Kotlin…
Kafka
  • 117
  • 8
2
votes
1 answer

Gradle fails after kotlin serialization plugin update to 1.7.10

My build.gradle.kts starts as follows: ... import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { ... kotlin("jvm") //target version is java 11 kotlin("plugin.serialization") version "1.6.21" } When I try to update the kotlin…
sigma1510
  • 1,165
  • 1
  • 11
  • 26
2
votes
1 answer

Kotlin serialization: nested polymorphic module

Recently I've been trying to implement Kotlinx Serialization for polymorphic class hierarchy. I used this guide, however my example was a bit more complex. I had the following class structure: @Serializable open class Project(val name:…
2
votes
1 answer

kotlinx deserialization: different types && scalar && arrays

I'm trying to deserialize a JSON like this (much more complex, but this is the essential part): [ { "field": "field1", "value": [1000, 2000] }, { "field": "field2", "value": 1 }, { "field":…
agathis
  • 78
  • 5
2
votes
1 answer

kotlinx.serialization JSON replacing default serializers in gradle mpp multiplatform project

I want to use my own custom KSerializer with kotlinx.serialization and kotlinx.datetime @ExperimentalSerializationApi @Serializer(forClass = LocalDateTime::class) object LocalDateTimeSerializer : KSerializer { ... I…
Dirk Hoffmann
  • 1,444
  • 17
  • 35
2
votes
1 answer

Serialize Enum as an Integer

I have generated some models using the Open API tool on one of our backend APIs with Swagger. I got the following as the enum definition: @Serializable enum class ClientBackgroundState(val value: kotlin.Int) { @SerialName(value = "0") …
FailedUnitTest
  • 1,637
  • 3
  • 20
  • 43
2
votes
1 answer

How to properly register primitives and nulls in polymorphic serialization?

I need to set up a serialization/deserialization mechanism for a polymorphic class hierarchy that also includes primitives and nulls. There are container classes containing collections with polymorphic objects, primitives, and nulls. And, the…
Matt Groth
  • 470
  • 4
  • 20
2
votes
0 answers

Kotlinx Json deserialize Retrofit null json response body

I am using retrofit with "jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.8.0" I've got an API call that returns JSON null as "not found" response. I thought that just marking return type as nullable will be enought like…
2
votes
1 answer

How to ignore unknown types with kotlinx.serialization?

I have several classes that I want to deserialize, that include lists of polymorphic types. I can get it to deserialize correctly known types, but deserializing an unknown type throws an exception. What I really want is that the list includes only…
anjosc
  • 923
  • 7
  • 13
2
votes
1 answer

How to access every SerialName associated with each enum constant in an enum class?

I have a data class that I want to make serializable data class ClassTemp ( val str: String, val tmp: Temp, ) @Serializable enum class Temp { @SerialName("Serial Name - 1") S1, @SerialName("Serial Name - 2") S2 } Example…
2
votes
1 answer

How to set serializer to an internal class extending a public interface?

I'm trying to create a serializer using kotlinx.serialization for Compose Desktop classes, I have this : @Serializer(forClass = MutableState::class) class MutableStateSerializer(private val dataSerializer: KSerializer) :…
Ayfri
  • 570
  • 1
  • 4
  • 24
2
votes
1 answer

using kotlinx serialization inside a gradle plugin that creates a gradle task

I am trying to create a gradle plugin that will generate files (serialized from data classes) from a gradle task that can run in another project. lets say that the classes that I am serializing are marked with some annotation @Annot and I find all…
YYJo
  • 1,064
  • 1
  • 9
  • 15
2
votes
1 answer

How to write KSerializer for Map having custom objects

I want to serializable following class using kotlinx-serializer. But since it contains Map with custom types - HttpUrl & List,it gives an error Serializer has not been found for type 'HttpUrl' Serializer has not been found for type…
Vipul
  • 27,808
  • 7
  • 60
  • 75
2
votes
1 answer

Build Error in kotlinx-serialization w/ Retrofit 2

I am using kotlinx-serialization along with Retrofit, but when I build my project I get the following error: Your current kotlinx.serialization core version is too low, while current Kotlin compiler plugin 1.4.21 requires at least 1.0-M1-SNAPSHOT.…
kc_dev
  • 578
  • 1
  • 6
  • 21