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
44
votes
2 answers

Kotlinx Serialization MissingFieldException

I am in the process of converting from Moshi to kotlinx serialization with Ktor and when I try to make a request to get data I am getting this error kotlinx.serialization.MissingFieldException: Field 'attachments' is required, but it was…
tyczj
  • 71,600
  • 54
  • 194
  • 296
27
votes
3 answers

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

Hi I have a problem with serialization of class in kotlin build.gradl.kt ... plugins { application kotlin("jvm") version "1.6.21" kotlin("plugin.serialization").version("1.6.21") } ... depenedancies{ ... …
Victor Orlyk
  • 1,454
  • 2
  • 15
  • 27
25
votes
2 answers

kotlinx.serialization : How to parse to different varaiable name than the exact name of JSON key

With GSON we used @SerializedName to parse JSON object which didn't have the same key to that of the variable name in Kotlin. data class User ( @SerializedName("id") long userId; @SerializedName("fullName") String name; ) In…
erluxman
  • 18,155
  • 20
  • 92
  • 126
24
votes
2 answers

kotlinx-serialization class marked @Serializable does not have the .serializer() extension function

I have the following data class @Serializable data class Income(val id: String, val description: String, val amount: Int, val Time: Date, val userId: String) now when I try…
yonBav
  • 1,695
  • 2
  • 17
  • 31
18
votes
10 answers

ktor with kotlinx serialization: how to use JSON.nonstrict

I'm trying to initialize Ktor http client and setup json serialization. I need to allow non-strict deserialization which JSON.nonstrict object allows. Just can't get how to apply this setting to serializer. val client = HttpClient { …
Rodion Altshuler
  • 1,713
  • 1
  • 15
  • 31
17
votes
2 answers

kotlinx-serialization: Polymorphic serializer was not found for missing class discriminator ('null')

I am trying to serialize a json, but its throwing JsonDecodingException. Check the code: SerializationTestCase.kt: import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import…
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
16
votes
3 answers

Class X is not abstract and does not implement fun writeToParcel() defined in android.os.Parcelable

In my Android app, I want to add a Bundle including a Place object described below to my Intent. Since serializable was slow and not recommended, I preferred Parcelable. Althoug I use Kotlin 1.3.31, I have problems parcelizing some data classes.…
13
votes
2 answers

How to serialize Kotlin data-class with default values into JSON using kotlinx.serialization?

There is an example of a class with a default value: @Serializable data class TestClass( val obligatory: String, val optional: Int = 0 ) It can be correctly deserialize from jsons like: { "obligatory":"text", "optional":1 } and {…
12
votes
6 answers

Kotlinx Serialization - Custom serializer to ignore null value

Let's say I'm having a class like: @Serializable data class MyClass( @SerialName("a") val a: String?, @SerialName("b") val b: String ) Assume the a is null and b's value is "b value", then Json.stringify(MyClass.serializer(), this)…
11
votes
4 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
  • 751
  • 1
  • 12
  • 25
11
votes
1 answer

Custom serializer for data class without @Serializable

I'm trying to deserialize a JSON file to a Kotlin data class I cannot control using kotlinx.serialization. The class looks something along the lines of: public data class Lesson( val uid: String, val start: Instant, val end: Instant, …
Endzeit
  • 4,810
  • 5
  • 29
  • 52
11
votes
1 answer

How to swap Spring Boot mapper from Jackson to kotlinx.serialization

I would like my Spring Boot project to use kotlinx.serialization. I can't figure out how to swap the mapper correctly... If I wanted to use GSON, I could just note it in the props via spring.http.converters.preferred-json-mapper=gson. Has anyone…
Chris Legge
  • 739
  • 2
  • 9
  • 24
10
votes
2 answers

How do i serialize a generic sealed class with kotlinx.serialization

Not sure if it is possible yet but for the life of me I cannot figure out how to serialize this. sealed class ServiceResult { data class Success(val data: T) : ServiceResult() data class Error(val exception:…
Chris Legge
  • 739
  • 2
  • 9
  • 24
10
votes
2 answers

Deserializing a json object property to a String using kotlinx.serialization

Given json as follows where the structure of the payload object will vary: { "id": 1, "displayName": "Success", "payload": { "someProperty": "example", "someOtherProperty": { "someNestedProperty": "example" …
user1173706
  • 2,612
  • 3
  • 19
  • 29
9
votes
2 answers

How to partially decode a JSON string using kotlinx.serialization?

I have a JSON string that looks like {"code": "FOO"}. Now I want to deserialize this string using kotlinx.serialization. I've tried the following: import kotlinx.serialization.* @Serializable data class Result(val code: String?) val decoded =…
Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
1
2 3
17 18