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 with generic message wrapper

So I have this class: @Serializable sealed class Message{ @Serializable class Success(val value: T) : Message() @Serializable class Error(val errorMessage: String) : Message() } Which I want to use for…
Typhaon
  • 828
  • 8
  • 27
2
votes
1 answer

Kotlin serialization serializer for ReamList and List

I just tried to use Kotlin serialization library to replace Gson however I am having issues using it for RealmList in my model. Any help will be appreciated. Am getting error kotlinx.serialization.SerializationException: Can't locate argument-less…
saintjab
  • 1,626
  • 20
  • 31
2
votes
1 answer

Polymorphic kotlinx serialization for complex hierarchy

With polymorphic serialization, I want to serialize classes A, B1, B2. I want to get the following line with class B2 {"type":"type_2","baseField":"base_field_B","fieldB":true,"fieldB2":"field_B_2"} I use hierarchy like interface BaseClass { …
2
votes
1 answer

Custom serializer with polymorphic kotlinx serialization

With kotlinx.serialization polymorphism, I want to get {"type":"veh_t","owner":"Ivan","bodyType":"cistern","carryingCapacityInTons":5,"detachable":false} but I…
2
votes
2 answers

Kotlinx Serialization, avoid crashes on other datatype

I'm using external API in the app, while deserialisation is done with Kotlinx Serialization package, i'm facing issues when api result is Array of Int for multiple values and primitive int for single value. How can i avoid crash in this process. Is…
OK200
  • 727
  • 6
  • 22
2
votes
1 answer

Serialize a list of optionals with kotlinx serialization

How can I stringify a list of optional strings with kotlinx serialization? I would like to execute something similar to: val json = Json(JsonConfiguration.Stable) val data: List = listOf("v1", null, "v3") val dataJson =…
2
votes
0 answers

How do I avoid an error when attempting to parse a Map from an empty JSON element

When my endpoint's JSON response's "data" element returns results, I am successfully parsing said results out into a Map. But when my endpoint's JSON response's "data" element returns empty e.g. "data": [], my deserializing…
2
votes
2 answers

Parse a nested JSON with Kotlinx.Serialization

I've been playing with Kotlinx.serialization, and I have been trying to parse a substring: Given a JSON like: { "Parent" : { "SpaceShip":"Tardis", "Mark":40 } } And my code is something like: data class SomeClass( …
Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
2
votes
0 answers

Kotlinx-serialization does not recognize Json.parse() or Data.serializer()

I tried following example to parse a Json String to corresponding object. The Data is marked with @Serializable Both functions Json.parse() and Data.serializer() are makred red and thus not recognized. I tried that with kotlinx-serializable 0.9.1…
Tranquillo
  • 235
  • 2
  • 13
2
votes
2 answers

Kotlin Deserialize Any Type Not Supported

I am trying to deserialize a JSON into a kotlin class using kotlin.serialization. However when the code goes to deserialize the json it throws the error kotlinx.serialization.SerializationException: Any type is not supported Can anyone help me…
Adrian Le Roy Devezin
  • 672
  • 1
  • 13
  • 41
2
votes
1 answer

kotlinx JSON.stringify ignore null values

Is there a way to ignore null values when stringify? Currently it renders as: "field":null it would be great if there is a way that null fields are not rendered at all - but I did not find such an option. Anyone knows how this can work?
ligi
  • 39,001
  • 44
  • 144
  • 244
1
vote
1 answer

How to Serialize a List of Primitive Objects with Kotlin Serializer?

I have a json response which is a list of list of objects, I am trying to serialize it. How do I Serialize it with kotlin serializer? { "body": [ [ "Fresho Apple - Royal Gala, Regular, 4 pcs", "Red", "146", 82878, …
1
vote
0 answers

Kotlin UShort deserialization with jackson

I am trying to see how to go about inline types such as UShort, UInt in Kotlin and their deserialization using jackson. build.gradle.kts plugins { kotlin("jvm") version "1.8.10" kotlin("plugin.serialization") version "1.8.10" } group =…
1
vote
1 answer

problems with serialization of sealed interface in kotlin

Hoping you might be able to help, as I'm slowly tearing my hair out and I'm not sure where I've gone wrong, but hope it might also be obvious. @Serializable data class Foo ( var text: String? ) : Bar @Serializable sealed interface Bar { …
1
vote
2 answers

Kotlin Serialisation with Enum (kotlinx.serialisation library)

I am using kotlinx.serialisation and had a question. I have JSON like this: { "state": "bad" } state can return bad|good|near_good My State data class looks like this: @Serializable internal enum class State { @SerialName("good") GOOD, …
Ayohaych
  • 5,099
  • 7
  • 29
  • 51