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
0 answers

Convert a list to String in kotlinx.serialization

I want to convert a list of a model that I've defined to String for saving it in Database I know how to do that in Gson but I want to do the same in kotlinx.serialization Convert a list of a model to String In Gson: val gson = Gson() return…
1
vote
0 answers

How to polymorphically serialize a Kotlin enum that implements a sealed interface?

I have the following sealed interface (simplified example): sealed interface Validation { val result: Int } ...which is implemented by several enums - each one for a particular application. For example: @Serializable(with = BoxedSerializer) // one…
Ulrich Schuster
  • 1,670
  • 15
  • 24
1
vote
0 answers

Is it possible to keep type-safty with kotlinx.serialization of a 3rd-party sealed class?

There is an Event sealed interface with UserConnected and EstimationSent implementations. data class User( val id: String, val displayName: String, ) sealed interface Event { val id: String } data class UserConnected( override val id:…
zokni
  • 41
  • 5
1
vote
0 answers

KotlinX Serialization validate input bad json

Let's say I have a Request data class: data class Request( val firstName: String, val lastName: String ) which I want to serialize when getting to a specific api route. Using Ktor, it would look like this: call.receive() This would…
Ofek
  • 324
  • 3
  • 13
1
vote
1 answer

Kotlinx serialisation, common interface or type class

I am working on a plugin type system where 3rd parties will register classes that will expose data. I don't know exactly what the data will look like but I will enumerate these plugin instances collect the data and I would like to serialise it. A…
Luke De Feo
  • 2,025
  • 3
  • 22
  • 40
1
vote
1 answer

How to use @Serializable as a interface?

I would like to write an function, that would work on any object which is serializable. Something like this: inline fun T.serialiseToJson(): String { return format.encodeToString(this) } This doesnt work because you…
beretis
  • 899
  • 1
  • 9
  • 24
1
vote
1 answer

For serial name Duration there already exist DurationSerializer / Serializer has not been found for type 'Duration'

Up until now, I had been using the following libraries: kotlin("jvm") kotlin("plugin.serialization") version "1.7.10" implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2") Since until now, there is no serializer for the type…
sigma1510
  • 1,165
  • 1
  • 11
  • 26
1
vote
1 answer

How do you configure kotlinx serialization in Spring Boot?

I am wondering how I can configure kotlinx as a default serialization in Spring Boot app. For Jackson, I would use e.g. spring.jackson.deserialization.fail-on-unknown-properties=false But I haven't found any configuration options for kotlinx in…
PunchyRascal
  • 280
  • 2
  • 9
1
vote
1 answer

Kotlin enforce data class type

I have various types of events in my application that are represented by Data classes. All events must have an attribute called EventContent that stores additional data about the event in String format. These events are saved to a database and…
john
  • 1,561
  • 3
  • 20
  • 44
1
vote
1 answer

Kotlin Serialization issues: Class is not registered for polymorphic serialization in the scope of its interface

I am facing issues with serialization using Kotlin. I've followed through the steps here https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serialization-guide.md but unfortunately, no luck... This is my code: sealed interface…
Remo
  • 1,112
  • 2
  • 12
  • 25
1
vote
0 answers

Error with Kotlin 1.7.0 Serialization in Android project

I've been using Kotlinx Serialization perfectly happily in my Android project. The main project build.gradle file has the following dependency: dependencies { classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.project.kotlin}" } And…
Barry Irvine
  • 13,858
  • 3
  • 25
  • 36
1
vote
1 answer

Java cannot access Kotlin's Companion

I am trying to use kotlinx serialization in java code. The problem I encounter is that I cannot use Companion object in java code to access it's static serializer() method which is generated by kotlin serializaton plugin. Here is the kotlin code…
Barracuda
  • 477
  • 5
  • 19
1
vote
1 answer

@JsonClassDiscriminator doesn't change json class discriminator

The Problem Due to project architecture, backward compatibility and so on, I need to change class discriminator on one abstract class and all classes that inherit from it. Ideally, I want it to be an enum. I tried to use @JsonClassDiscriminator but…
Elas
  • 212
  • 2
  • 16
1
vote
1 answer

Parse Json to Map kotlin multiplatform

My resialized is "{"2":"Hello","Tesst":"Value"}" I tried to parse this string to Map val resialized = readFile(createStorageDirectoryPath(getManifestFilePath()), MANIFEST_FILE_NAME, errorOut) manifest =…
Anhdevit
  • 1,926
  • 2
  • 16
  • 29
1
vote
0 answers

How to correctly serialize an mongoDB ObjectId with kotlinx

I am trying to serialize a MongoDB ObjectId using a Kotlin, Spring Boot, KMongo and Kotlinx. The problem is, that the ObjectId is getting serialized as timestamp: Expected Result: {"name":"Game…
EyedPeas
  • 146
  • 4
  • 17