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
1
vote
1 answer

How to decode a JSON string to a given KClass?

I have my code structure like this: File 1: abstract class SomeClass { abstract fun print() companion object { val versions = arrayOf(ClassV1::class, ClassV2::class) } } @Serializable data class ClassV1(val x: Int) :…
Arpit Shukla
  • 9,612
  • 1
  • 14
  • 40
1
vote
1 answer

How can I encode a typed class with Kotlinx Serialization?

I'd like to encode a given class of type T: EventData with Kotlinx Serialization encodeToString. This is my code: class EventDispatcher( val pubSubTemplate: PubSubTemplate ) { /** * Dispatch an event to the game engine…
xetra11
  • 7,671
  • 14
  • 84
  • 159
1
vote
1 answer

Kotlin Serialization of Generic Type that is Serializable

Kotlin serialization is hard! How do I do get Kotlin to believe that the values in my properties map are either primitives or classes annotated with @Serializable? I'm trying to turn a class like this: class Entity(val id: String, val type: String,…
Jason
  • 2,579
  • 1
  • 17
  • 19
1
vote
1 answer

Kotlinx.Serialization using OkHTTPClient return always Failure

Hello I have a problem with my JSON. I am using OkHTTPClient to get JSON from web - to get objects from JSON using kotlinx.serialization via method which contains this and return value from method should be Result : private suspend inline fun…
1
vote
1 answer

kotlinx-serialization - Why default value inherited from base class is always encoded?

@Serializable open class A { val default: String = "hello" } @Serializable open class B(val value: String): A() fun main(){ val obj = B("foo") val str = Json.encodeToString(obj) println(str) } encoding…
R.h
  • 60
  • 8
1
vote
0 answers

Serializer for class is not found. Mark the class as @Serializable or provide the serializer explicitly

Released APK crashes with next error: Serializer for class 'User' is not found. Mark the class as @Serializable or provide the serializer explicitly. @Keep @Serializable data class User( val id: Long = 0, val name: String? = null, val…
user924
  • 8,146
  • 7
  • 57
  • 139
1
vote
0 answers

Kotlin generic class error: Only KClass supported as classifier

I have a Kotlin class with two generic parameterized types, but when I try to use it, I get this error: "Only KClass supported as classifier, got K". How can I fix it without changing client code (main method)? Code: import…
1
vote
1 answer

How to register a global type adapter for an interface, without annotating every class, while using kotlinx.serialization?

Gson would let me do a GsonBuilder().registerTypeAdapter(MyInterface::class.java, MyConcreteClassAdapter()) but I'm unable to to the same with kotlinx.serialization I want to expose only the interface (ValueInterface), so I can hide implementation…
anjosc
  • 923
  • 7
  • 13
1
vote
1 answer

Maps and variable key names in Kotlinx-Serialization

The meta is simple, but how do I model analysis for Kotlinx-Serialization? { "meta": { "subject": "33306", "interval": "weekly" }, "analysis": { "2021-07-20": { "dose": "0.6410" }, "2021-07-16": { "dose":…
es0329
  • 1,366
  • 1
  • 18
  • 35
1
vote
1 answer

kotlinx-serialization - How to serialize ONLY properties on interface

The documentation here shows that when serializing an object that implements an interface, as long as the subclass is @Serializable, it will be able to serialize it. However, this serializes a number of properties that are on the subclass that I…
FatalCatharsis
  • 3,407
  • 5
  • 44
  • 74
1
vote
1 answer

How to use Kotlinx serialization with jvm plugin

I can't use Kotlinx serialization with the Kotlin JVM plugin In the instructions for Groovy DSL: plugins { id 'org.jetbrains.kotlin.multiplatform' version '1.5.0' id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.0' } Because I…
Alperen
  • 3,772
  • 3
  • 27
  • 49
1
vote
1 answer

How to deserialize json to generic type in Kotlin using Jackson/kotlinx.serialization

I have a generic class class MyClass : MyInterface and I want to deserialize a json to generic type T. I tried using Jackson and kotlinx.serialization libraries to deserialize json but I get following error cannot use T as reified type…
sap
  • 75
  • 1
  • 1
  • 10
1
vote
0 answers

java.lang.VerifyError: Verifier rejected. Rejecting invocation, expected 36 argument registers, method signature has 37 or more

when I'm trying to decode a bunch of json data represented as a String it crashes and says: java.lang.VerifyError: Verifier rejected class com.example.package.MySampleClass$$serializer: failed to verify: com.example.package.MySampleClass…
Jemo Mgebrishvili
  • 5,187
  • 7
  • 38
  • 63
1
vote
3 answers

How to use Navigation Safe Args with Kotlinx serializable data

I'm trying to use Navigation Safe Args with Kotlinx @Serializable types, but I keep getting the same issue during run-time when I pass the serializable data: Caused by: java.lang.IllegalArgumentException:…
1
vote
2 answers

How to deserialize an array of values into a collection using kotlinx serialization

Hi I am a newbie to kotlinx serialization and I am using KMP, my requirement is a little different my data class @Serializable data class Student(val name : String , val age : Int) and my simple JSON would be "['Avinash', 22]", which should be…