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

Deserialize complex JSON in Kotlin

I would like to deserialize the following JSON: { "participants": { "0": { "layout": "layout1" } }, "layouts": { "layout1": { "width": 100, "height": 100 } } } Into the following…
Nahum Bazes
  • 610
  • 1
  • 6
  • 15
0
votes
2 answers

Exception deserializing Json in Retrofit, "Polymorphic serializer was not found for missing class discriminator ('null')"

Working with Kotlin and Retrofit, I'm getting the following exception thrown on response to my http post, before onResponse()/onFailure() can get called: kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found…
userCB
  • 3
  • 2
0
votes
1 answer

Kotlinx Serialization, inlining sealed class/interface

With a structure similar to the following: @Serializable sealed class Parameters @Serializable data class StringContainer(val value: String): Parameters() @Serializable data class IntContainer(val value: Int): Parameters() @Serializable data…
Rohde Fischer
  • 1,248
  • 2
  • 10
  • 32
0
votes
1 answer

Kotlin - What is the easiest way to create a generic data object to transform into JSON for API requests?

It is very easy to create native generic objects that can be serialized into JSON in dynamically typed languages like Python, PHP, or JS. With Kotlin, it seems like I will need to define a serializable data class, at the very least, in order to be…
ineedhelp
  • 17
  • 1
  • 4
0
votes
2 answers

Polymorphic deserialization of nested list object in Kotlin

Trying to deserialize a pretty complex json string, I've done simpler ones but am struggling with how to set this up. I've also found some examples on simple polymorphic deserialization but cannot figure out how to adapt it for this more complex…
0
votes
1 answer

API date field returns java.time.format.DateTimeParseException: Text could not be parsed

I have an API endpoint that has a datetime field and I'm unable to parse it correctly. The API is written in C# and I'm not finding much help on how to resolve this: kotlinx.datetime.DateTimeFormatException: java.time.format.DateTimeParseException:…
Eman
  • 1,093
  • 2
  • 26
  • 49
0
votes
1 answer

kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')

I am using sealed class to handle api response. I am trying some code but getting some error of serialization. I tried this solution as well but it not working. Can you guys help me out on this problem. I am new in serialization field. import…
0
votes
0 answers

kotlinx serialization custom serlializer for custom List (RealmList) class

I'm using the Realm database in Kotlin on Android along with some data that we're receiving over the wire using JSON, using kotlinx.serialization as the JSON library. I'd like to write a custom serializer for RealmList. An example class…
SuperDuperTango
  • 1,398
  • 1
  • 14
  • 32
0
votes
1 answer

Ktor how to get http code from request without body

I make a request to the server, but there is no body in the response. Accordingly, the return value type of response is Unit. suspend fun foo( url: String, id: Long ) { val requestUrl = "$url/Subscriptions?id=${id}" …
mitua1221
  • 473
  • 3
  • 6
0
votes
0 answers

kotlinx decodeFromString to 'ArrayList' vs 'CustomClass extending ArrayList'

Why does this work: val string = "[1, 2, 3]" val result: ArrayList = json.decodeFromString(string) but this doesn't: @Serializable class MyClass : ArrayList() val string = "[1, 2, 3]" val myClass: MyClass =…
TootsieRockNRoll
  • 3,218
  • 2
  • 25
  • 50
0
votes
1 answer

Parse a dynamic nested JSON into Map<*, *> using Kotlinx.serialization

I need to parse any dynamic JSON (In the Kotlin Multiplatform project) to key-value pair and convert it to a nested list after. I started with the default .toMap() function, but it can't go deeper to parse ArrayList<*> Any idea how to do it? Sample…
0
votes
1 answer

how to decode list of enum as root element excluding unknown enum

this is my json to parse: ["AA", "BB", "unknown"] I need to decode with List that doesn't contain unknown element. I can get list of 3 element with 1 null or list of 2 element. i tryed KindListSerializer but how to apply to root element?
Alessandro Scarozza
  • 4,273
  • 6
  • 31
  • 39
0
votes
0 answers

Polymorphic custom serialization strategy?

I'm having a really difficult time working this out. I have a polymorphic JSON structure that I want to parse and flatten. Basically this: { "objectType": "type", "data": {...} } And the data object changes depending on type. What I would like…
Ernest Zamelczyk
  • 2,562
  • 2
  • 18
  • 32
0
votes
1 answer

KTable-KTable FK join: can't select foreign key serde

summary I'm trying to do a KTable-KTable foreign-key join, but I get an error because the Kafka Streams is trying to use a String serde for the foreign key. I want it to use a Kotlinx Serialization serde. How can I specify this? details I want to…
aSemy
  • 5,485
  • 2
  • 25
  • 51
0
votes
0 answers

Kotlin serialization plugin does not generate serializers in Gradle project

I'm trying to use the Kotlinx Serialization libraries and the associated Gradle plugin. It's supposed to generate serializers for when when I annotate a class with @Serializable, like this: @Serializable data class Message(val version: String) //…
Silver Quettier
  • 2,045
  • 2
  • 26
  • 53