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
9
votes
2 answers

How to disable kotlinx serialization polymorphic descriminator?

I'm generating JSON for a number of third-party APIs. Many of them accept a list (JSON array) of different objects, however, none of them will accept the "type": "com.mycom.someclass" automatically generated by kotlinx serialization due to the…
Newbie
  • 7,031
  • 9
  • 60
  • 85
9
votes
2 answers

Parse a JSON array into Map using Kotlinx.serialization

I am writing a Kotlin multiplatform project (JVM/JS) and I am trying to parse a HTTP Json array response into a Map using Kotlinx.serialization The JSON is something like this: [{"someKey": "someValue"}, {"otherKey": "otherValue"}, {"anotherKey":…
8
votes
1 answer

How to serialize "Any" type in Kotlinx Serialization?

I have a class that gets serialized for network traffic. @Serializable data class Packet(val dataType: String, val payload: Any) I've used Java serialization to send it over the wire. The receiver can't know the type of the payload but Java…
honkbert
  • 135
  • 1
  • 7
8
votes
1 answer

How to serialize a library class to Protobuf with kotlinx.serialization?

How to serialize a library class to Protobuf with kotlinx.serialization? Since it's non-editable, I can't add @SerialId annotations to its properties as instructed in runtime_usage.md#protobuf. If I write my own external serializer as in…
Shreck Ye
  • 1,591
  • 2
  • 16
  • 32
7
votes
2 answers

How to properly use class inheritance in Kotlin in combination with Kotlinx Serialization

I have a simple hierarchy containing of the following: abstract class BaseItem open class Item : BaseItem class Backpack : Item They should all work with Kotlinx Serialization. It went fine until I added the Backpack class. I use version 1.4.32 of…
xetra11
  • 7,671
  • 14
  • 84
  • 159
7
votes
3 answers

How to get status code of HttpCall with Ktor and kotlinx serialization

I am trying to figure out how to check the http status code of a http request with Ktor I have a simple GET request like this with a HttpResponseObject that holds the data the server returns and any errors server side that I control val…
tyczj
  • 71,600
  • 54
  • 194
  • 296
7
votes
1 answer

Serializer for interface / implementation

Suppose there is one interface , and 2 (or more) implementations : interface IRunnable { fun run() } class Horse : IRunnable { override fun run() { println("horse running") } } class Dog : IRunnable { override fun run() { println("dog…
smallufo
  • 11,516
  • 20
  • 73
  • 111
7
votes
1 answer

Kotlinx.Serializer - Create a quick JSON to send

I've been playing with Kotlinx.serialisation. I've been trying to find a quick way to use Kotlinx.serialisation to create a plain simple JSON (mostly to send it away), with minimum code clutter. For a simple string such as: {"Album": "Foxtrot",…
Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
7
votes
2 answers

SerializationException: can't locate argument-less serializer

I'm creating a Kotlin Multiplatform library; actually I got 3 modules ( common, jvm and js ), In the classpath I got: classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}" And in my modules I got: common:…
6
votes
1 answer

How can I JSON encode BigDecimal and BigInteger in Kotlinx Serialization without losing precision?

I'm using Kotlin/JVM 1.8.0 and Kotlinx Serialization 1.4.1. I need to encode a java.math.BigDecimal and java.math.BigInteger to JSON. I'm using BigDecimal and BigInteger because the values I want to encode can be larger than a Double can hold, and…
aSemy
  • 5,485
  • 2
  • 25
  • 51
6
votes
1 answer

Make a field optional in Kotlinx serialization

Here is my pojo class @Serializable data class Response( @SerialName("message") val message: String?, @SerialName("parameters") val parameters: Map? ) And this is Json, I was trying to decode from: { "message": "Some…
S Haque
  • 6,881
  • 5
  • 29
  • 35
6
votes
3 answers

Kotlinx.Serialization deserializing dates

I'm having a hard time finding documentation on how to deserialize date fields. How do i achieve this? Most solutions i found on SO don't work or they use classes that are no longer available @Serializable data class Dashboard( val someNumber:…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
6
votes
1 answer

Polymorphic deserialization with kotlinx.serialization in Kotlin/Native

I want to decode a json string containing a list of objects in a polymorphic class structure using kotlinx.serialization in a Kotlin Multiplatform project, but it works only on JVM, not on Native. Here is a minimum reproducible…
6
votes
1 answer

Deserialize JSON array with different values type with kotlinx.serialization library

I'm trying to deserialize following String: val stringJson = "{\"decomposed\":[\", \",{\"id\":4944372,\"name\":\"Johny\",\"various\":false,\"composer\":false,\"genres\":[]}]}" Deserialization works fine with following code @Serializable data…
Plato
  • 95
  • 1
  • 5
6
votes
1 answer

How to serialize kotlin sealed class with open val using kotlinx serialization

import kotlinx.serialization.Serializable @Serializable sealed class Exercise(open val id: String) { @Serializable data class Theory(override val id: String) : Exercise(id) } I have such kind of sealed class in my code, and compiler says…
1
2
3
17 18