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

kotlinx.serialization: inject local parameter

I need to inject a local value to a class constructor during deserialization. For example, look at the following class. @Serializable class SomeClass(val local: Context, val serialized: String) I want the field local to be skipped during…
6
votes
3 answers

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
  • 20,466
  • 42
  • 148
  • 239
6
votes
1 answer

Parse JSON object string from json value

I'm making a request to a request to an API and the response is a JSON object, this json object contains a string that is another json object. I'm trying to use kotlinx.serialization to handle the deserialization of this object. I could override the…
Spiderbiggen
  • 61
  • 2
  • 3
5
votes
1 answer

Kotlinx deserialization does not include properties from abstract class when using "JsonContentPolymorphicSerializer"

Lets say there is abstract class BaseClass ... import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable(with = PolymorphicSerializer::class) abstract class BaseClass { @SerialName("bid") var baseId:…
Ivan Škugor
  • 558
  • 1
  • 6
  • 21
5
votes
2 answers

Why serializing collections of different element types is not supported in ktor-serialization?

I am trying to make a simple server which gives serialized List in JSON. The List to be serialized is the example in the official blog post's Polymorphic serialization section. But with the ktor's serialization feature, I get the following…
Hiro
  • 51
  • 3
5
votes
1 answer

android kotlin multiplatform kotlinx.serialization works on debug but does not work on release version

We are making our next project in the company with kotlin multiplatform. Everything worked fine until I tried to create a release version for android to beta test. I got this error in release versions while everything works fine in…
4
votes
1 answer

Is there an interface applied to serializable types in Kotlin?

I want to create a class that looks roughly like this: class MyWrapperClass(val someData: T) { fun stringifyMe(): String { return Json.encodeToString(someData) } } My question is about that IsSerializable type. I'd…
zv777mnw
  • 93
  • 3
4
votes
1 answer

How to make proguard keep kotlinx serializers for objects?

i have a kotlin class and an object @Serializable @SerialName("Cl") class Cl(...) @Serializable @SerialName("Obj") object Obj and my proguard is configured like this -keepclassmembers class kotlinx.serialization.json.** { ***…
Noam Freeman
  • 143
  • 1
  • 8
4
votes
2 answers

How to control layout of polymorphic Kotlinx serialization?

I have a json that holds a list of polymorphic objects, { "someList": [ { "type": "A", "value": {"aProp": false } }, { "type": "B", "value": {"bProp": "1" } }, ] } which represents a type hierarchy like this one: sealed class…
Noam Freeman
  • 143
  • 1
  • 8
4
votes
1 answer

Deserializing JSON into Serializable class with generic field - error: Star projections in type arguments are not allowed

Intro I'm sending JSON messages between two backend servers that use different languages. The producing server creates a variety of JSON messages, wrapped inside a message with metadata. The wrapping class is Message, The consuming server has to…
aSemy
  • 5,485
  • 2
  • 25
  • 51
4
votes
0 answers

Class 'TaskPayload' is not registered for polymorphic serialization in the scope of 'EventPayload'

I have a simple sealed class and a concrete class using @Serializable. However when compiling I always end up with an error. // EventPayload.kt @Serializable sealed class EventPayload { val type: String get() =…
xetra11
  • 7,671
  • 14
  • 84
  • 159
4
votes
2 answers

Optional field vs null value in kotlinx.serialization

How to distinguish between {data: null} and {} in kotlinx.serialization when deserializing JSON? @Serializable class MyClass(val data: String?)
wilddev
  • 1,904
  • 2
  • 27
  • 45
4
votes
0 answers

How to parse protobuf message in kotlinx serialization

I have a protobuf message Request and Response. What is the correct representation of this for Kotlin data class for kolinx.serialization. Protobuf serialization is still experimental docs. Can I use the kolinx.serialization to serialize and…
Jan Veselý
  • 1,329
  • 1
  • 15
  • 24
4
votes
2 answers

kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found

I'm trying to upload multiple files. val ktorVersion = "1.5.0" val serializationVersion = "1.0.1" That is how I'm doing that: override suspend fun uploadFiles( binaryFiles: Map ): BaseResponse> { return…
4
votes
1 answer

Kotlinx Serialization: How to circumvent reified typeargs for deserialization?

Actually, the main problem is still that there are no reified typeargs for classes in Kotlin. But here is why this bothers me in this specific case: Suppose you have a wrapper class Wrapper that takes in a string content and a class* type and can…
1 2
3
17 18