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

Kotlin: Deserialize parts of JSON into nested child objects

Is it possible to deserialize a JSON structure so that portions of that structure are collected into a nested child object? So given this JSON structure { "root_field1": "This field will be in root", "root_field2": "This field will be in root", …
Hrafn
  • 2,867
  • 3
  • 25
  • 44
3
votes
1 answer

Encode / decode JSON "string" in kotlinx.serialization

Is it possible to encode / decode any valid json objects in string format in a custom serializer. For example the code below but not let it serialize as json string but as any valid JSON with unknown structure? object JsonObjectSerializer :…
Chris
  • 8,168
  • 8
  • 36
  • 51
3
votes
2 answers

Can I use kotlinx classes in Kotlin scripts?

Is it possible to import classes from kotlinx package in a simple Kotlin script? myscript.kts: import kotlinx.serialization.* import kotlinx.serialization.json.* println("") Running the above script with kotlinc -script myscript.kts gives this…
Mahozad
  • 18,032
  • 13
  • 118
  • 133
3
votes
1 answer

how to compile kotlinx.serialization libraries on the command line?

This is very close to what I'm trying to accomplish. How to compile and run kotlin program in command line with external java library I really want to learn how to compile and run simple code that includes libraries but am getting a bit lost when it…
3
votes
1 answer

kotlinx.serialization.SerializationException: Serializer for class 'UnitEntity' (myClassName) is not found

I'm working on a Kotlin Multi-platform project and got a problem of saving database entity object. when I call insert method of my sqldelight dao classes exception where thrown kotlinx.serialization.SerializationException: Serializer for class…
3
votes
0 answers

Kotlinx Serialization Exception with Proguard and Retrofit

I am trying to use kotlinx.serialization library with retrofit and coroutines. This combination is working fine unless I try to use proguard. My proguard rules. -keepattributes *Annotation*, InnerClasses -dontnote…
trigri
  • 525
  • 1
  • 6
  • 16
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,495
  • 18
  • 34
3
votes
2 answers

Gradle is unable to locate the bintray repository for kotlinx-serialization

I am trying to use kotlinx.serialization and have had just no luck. Here is the pertinent portion of the build.gradle buildscript { ext.kotlin_version = '1.3.71' repositories { mavenCentral() google() jcenter() …
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
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": { …
Tyler
  • 19,113
  • 19
  • 94
  • 151
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
  • 714
  • 6
  • 16
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
  • 39,001
  • 44
  • 144
  • 244
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
  • 39,001
  • 44
  • 144
  • 244
2
votes
0 answers

Kotlin: How do I serialize LocalDateTime using kotlinx serializer?

How would I go about serializing LocalDateTime using kotlinx serializer I've read that I would need to include implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") in my dependencies, which I did, but I'm still getting this…
2
votes
1 answer

Is there any way to require an annotation on a type parameter in kotlin?

I am making a data structures that automatically persist data to the disk. To do this I am using kotlinx-serialization. Is there any way to require a generic type to have the @Serializabe annotation? class PersistentList <@Serializable E> () :…
Kyle M
  • 119
  • 7
2
votes
1 answer

Decoding JSON to a class based on the value of another JSON key

I am trying to work with a REST API that returns a JSON document who's structure depends on the value of a property named type. I have defined the main class as follows: @Serializable class Interaction( val type: Byte, val data:…
Adam Chance
  • 277
  • 1
  • 2
  • 10