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
1 answer

How to serialize fields with varying type?

I have the following data classes to parse JSON. I can parse it easily with the decodeFromString method. However, the Info classes could contain the List type from time to time along with the Int type so that both are included in a single JSON.…
Erdem Tuna
  • 500
  • 4
  • 18
2
votes
1 answer

kotlinx.serialization , Encode Decode SnapshotStateList

In Jetpack Compose , SnapshotStateList is a derived class from List but whenever I try to put Serializable annotation over it , it says "Serializer Not Found For This Class , To use contextual use @Contextual annotation with type or property" I have…
Waqas Tahir
  • 7,171
  • 5
  • 25
  • 47
2
votes
0 answers

Using Ktor, Kmongo and kotlinx.serialization together causing ClassCastException ... What am I doing wrong?

https://github.com/reticent-monolith/winds_server is the github repo if anyone finds it easier looking there. I'm trying to use KMongo and Ktor with Kotlin's Serialization module but creating the MongoClient results in the following…
2
votes
1 answer

Kotlin commonMain with java.io.Serializable

Problem I have a data class in commonMain (called Person) that I would like to access from jvmMain as type java.io.Serializable. I have a solution, which is shown below, but I was wondering if this is the best approach. I also found that the library…
Devabc
  • 4,782
  • 5
  • 27
  • 40
2
votes
1 answer

Proper way to serialize a sealed class with kotlinx-serialization

I am not sure if it is possible yet but i would like to serialize the following class. @Serializable sealed class RestResponseDTO{ @Serializable @SerialName("Success") class Success(val value: T) :…
Chris Legge
  • 739
  • 2
  • 9
  • 24
2
votes
1 answer

Does kotlinx.serialization handle class versions?

With Java serialization, there was serialVersionUID. I don't know how well that worked, but it was, at least, a simple mechanism for testing whether a class changed between serialization and deserialization. Does kotlinx.serialization have any such…
Travis Well
  • 947
  • 10
  • 32
2
votes
1 answer

kotlinx.serialization: Deserialize JSON array as sealed class

My data is a rich text format, stored as nested JSON arrays. Text tokens store the plaintext of the string and annotations describing the formatting. I want to map the specific structure of these nested JSON arrays to a rich Kotlin class hierarchy…
Just Jake
  • 4,698
  • 4
  • 28
  • 33
2
votes
1 answer

How to Serialize Web Socket Frame.text in Ktor with kotlinx.serialization

webSocket("/ws") { try { while (true) { when(val frame = incoming.receive()){ is Frame.Text -> { val text = frame.readText() //i want to Serialize it…
Abd al-Rahman
  • 33
  • 1
  • 5
2
votes
2 answers

Kotlin: convert nested JSON object to literal string

I have a data class which has a property whose type is another data class, like this: @Serializable data class Vehicle ( val color: String, val miles: Int, val year: Int, val garage: Garage ) @Serializable data class Garage ( val…
ExactaBox
  • 3,235
  • 16
  • 27
2
votes
1 answer

Replace Jackson with kotlinx.serialization in Javalin framework

I'm trying to replace the default Javalin JSON serializer Jackson by Kotlinx.serialization. The documentation show how to do it with GSON serializer. Unfortunately kotlinx serializer has a different function signature and I can't figure out how to…
Freezystem
  • 4,494
  • 1
  • 32
  • 35
2
votes
3 answers

Kotlin - Cannot serialize polymorphic classes. No serializer found

I am trying to serialize polymorphic classes using kotlinx.serialization with kotlin/native. I am using the sample provided in the serialization guide: val module = SerializersModule { polymorphic(Project::class) { …
2
votes
1 answer

Map sorted by keys generated with kotlinx.serialization

I need to serialize a class with a map so that the keys in the map are sorted in the json. So if there's a class @Serializable class Example(val map: Map) and it's serialized with val example = Example(mapOf("b" to 2, "a" to 1, "c" to…
silyevsk
  • 4,021
  • 3
  • 31
  • 30
2
votes
1 answer

Ktor with Kmongo and kotlinx.serialization

I have a setup where I use KTor with KMongo and Kotlinx.Serialization. The Kmongo part works, I can get and put my Class @Serializable data class Task(@ContextualSerialization @SerialName("_id") val _id : Id = newId(), val…
2
votes
0 answers

Serialize a Map with Ktor and kotlinx.serialization

Here's a Ktor route that returns a map of LocalDates: route { get { val map = mapOf(LocalDate.now() to "test") call.respond(map) } } LocalDate is not serializable, however: kotlinx.serialization.SerializationException: Can't…
Malcolm Crum
  • 4,345
  • 4
  • 30
  • 49
2
votes
0 answers

Deserialize a response with Kotlinx.Serialization and Retrofit

I'm using kotlinx.serialization and implementation("com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:0.5.0") with Retrofit2 I'm probably missing something obvious, but I'm just not putting 2 and 2 together on this one. I have a…
tcox
  • 126
  • 2
  • 6