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
125 questions
4
votes
1 answer

Serializing Lists with external kotlinx Serializer

So, I have this class Item.kt class Item { val name = "" val loc = "" val price = 0.0 override fun toString() = "$name <$loc> $price" } Since this class is in another library (I can't edit its source), I have an external Serializer…
andylamax
  • 1,305
  • 9
  • 28
3
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
  • 799
  • 5
  • 28
3
votes
2 answers

Serializer for data class Kotlin not found at build release

I want convert my json string response from API to object: val obj = Json.decodeFromString(jsonResponseString) My data class: @Serializable data class MyModel( @SerializedName("field") val field: String ) It look like very simple and…
Andy
  • 366
  • 5
  • 15
3
votes
2 answers

Why serializing collections of different element types is not supported in ktor-serialization?

I am trying to make a simple server which gives serialized List in JSON. The List to be serialized is the example in the official blog post's Polymorphic serialization section. But with the ktor's serialization feature, I get the following…
Hiro
  • 31
  • 1
3
votes
1 answer

Kotlinx Serialization: How to circumvent reified typeargs for deserialization?

Actually, the main problem is still that there are no reified typeargs for classes in Kotlin. But here is why this bothers me in this specific case: Suppose you have a wrapper class Wrapper that takes in a string content and a class* type and can…
3
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…
3
votes
1 answer

Receiving NoClassDefFoundError when invoking generated serializer() method

I’m getting a NoClassDefFoundError when trying to invoke the Foo.serializer() method on a @Serializable class. Here's my test case: @Serializable data class Foo(val data: String) val jsonString = json.stringify( Foo.serializer(), // <= Error…
rharter
  • 2,416
  • 15
  • 31
3
votes
1 answer

Deserializing into sealed subclass based on value of field

I have a field that I want to deserialize into an instance of a sealed subclass based on a value on that Json object. [ { "id": 1L, "some_array": [], "my_thing": { "type": "sealed_subclass_one", "other_thing": { …
tango whiskey double
  • 17,662
  • 15
  • 86
  • 132
3
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
  • 10,110
  • 19
  • 64
  • 101
3
votes
1 answer

How to serialize dynamic keys of a json with kotlinx

I have a json with this kind of key: ... "metaData": { "date:create": "2019-11-13t15:42:02+01:00", "date:modify": "2019-11-13t15:42:02+01:00", "exif:ColorSpace": "1", "exif:ExifImageLength": "1500", …
3
votes
2 answers

Parse JSON object string from json value

I'm making a request to a request to an API and the response is a JSON object, this json object contains a string that is another json object. I'm trying to use kotlinx.serialization to handle the deserialization of this object. I could override the…
3
votes
2 answers

kotlinx.serialization - Serialize ArrayList as data class variable with custom DateSerializer

I need to serialize ArrayList as data class variable with custom DateSerializer, with single date variable i use annotation: @Serializable data class SearchBundle( @Serializable(with = DateSerializer::class) var startDate: Date? =…
Webdma
  • 644
  • 5
  • 14
3
votes
1 answer

Can Ktor HttpClient JsonFeature use kotlinx.serialization

Ktor's HttpClient allows installing a Json feature. Does anyone know if this feature can use kotlinx.serialization library or is it only limited to Gson? I'm interested in enabling the Json feature for MPP projects.
Y2i
  • 3,455
  • 1
  • 26
  • 29
3
votes
0 answers

VerifyError: Rejecting class Serializer

I am facing a problem with kotlinx JSON serializing and having a @Serializer annotation on Android. The problem only happens on certain API-levels - seems to work above API level 23 and gives this error below: java.lang.VerifyError: Rejecting class…
ligi
  • 35,518
  • 37
  • 123
  • 220
3
votes
2 answers

SerializationComponentRegistrar is not compatible with this version of compiler

I am getting this error inside AS - even though I tripple checked to pick a valid version pairing (kotlin 1.2.40 with serialisation 0.5.0): Error:Kotlin: [Internal Error] java.lang.IllegalStateException: The provided plugin…
ligi
  • 35,518
  • 37
  • 123
  • 220
1
2
3
8 9