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
15
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.…
9
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,147
  • 1
  • 12
  • 22
9
votes
9 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,583
  • 14
  • 29
8
votes
1 answer

How to serialize a library class to Protobuf with kotlinx.serialization?

How to serialize a library class to Protobuf with kotlinx.serialization? Since it's non-editable, I can't add @SerialId annotations to its properties as instructed in runtime_usage.md#protobuf. If I write my own external serializer as in…
Shreck Ye
  • 1,122
  • 1
  • 8
  • 22
7
votes
1 answer

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
  • 66,691
  • 50
  • 172
  • 271
7
votes
2 answers

SerializationException: can't locate argument-less serializer

I'm creating a Kotlin Multiplatform library; actually I got 3 modules ( common, jvm and js ), In the classpath I got: classpath "org.jetbrains.kotlin:kotlin-serialization:${versions.kotlin}" And in my modules I got: common:…
6
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
  • 22,543
  • 24
  • 96
  • 158
6
votes
1 answer

How to disable kotlinx serialization polymorphic descriminator?

I'm generating JSON for a number of third-party APIs. Many of them accept a list (JSON array) of different objects, however, none of them will accept the "type": "com.mycom.someclass" automatically generated by kotlinx serialization due to the…
Newbie
  • 6,097
  • 9
  • 53
  • 77
6
votes
2 answers

How to get status code of HttpCall with Ktor and kotlinx serialization

I am trying to figure out how to check the http status code of a http request with Ktor I have a simple GET request like this with a HttpResponseObject that holds the data the server returns and any errors server side that I control val…
tyczj
  • 66,691
  • 50
  • 172
  • 271
6
votes
3 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)…
6
votes
1 answer

Parse a JSON array into Map using Kotlinx.serialization

I am writing a Kotlin multiplatform project (JVM/JS) and I am trying to parse a HTTP Json array response into a Map using Kotlinx.serialization The JSON is something like this: [{"someKey": "someValue"}, {"otherKey": "otherValue"}, {"anotherKey":…
5
votes
1 answer

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
  • 13,712
  • 15
  • 67
  • 99
4
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
  • 499
  • 1
  • 6
  • 17
4
votes
1 answer

Handling Kotlin Serialization MissingFieldException with Retrofit

I'm using kotlinx.serialization in conjunction with retrofit. The json response that I receive will vary in terms of what attributes it will contain. In most cases, the data model in my app has more fields than I will receive in the response. I…
AndroidDev
  • 18,031
  • 39
  • 126
  • 220
4
votes
1 answer

Kotlinx.Serializer - Create a quick JSON to send

I've been playing with Kotlinx.serialisation. I've been trying to find a quick way to use Kotlinx.serialisation to create a plain simple JSON (mostly to send it away), with minimum code clutter. For a simple string such as: {"Album": "Foxtrot",…
Maneki Neko
  • 809
  • 1
  • 9
  • 20
1
2 3
8 9