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

Serialize sealed class objects as if they were Enum constants

I'm developing a small library that defines some domain objects as sealed class objects and I'd like to serialize those objects as simple Strings using the kotlinx-serialization library. The structure of the sealed class with its objects is…
Daniel Schröder
  • 547
  • 4
  • 18
0
votes
1 answer

Fail to get POST response from Server, using Ktor Client android

First time i am using Ktor in my android project I am not getting response from server, my requirement is to make a POST request with JSON in body request format of JSON as { "country": "Malaysia" } I tried on postman it is working fine, but not…
Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33
0
votes
0 answers

Can't serialize a type with a abstract/interface property even when passing a SerializersModule

I get: SerializationException: Class 'Porsche' is not registered for polymorphic serialization in the scope of 'SdkExtraData'. Mark the base class as 'sealed' or register the serializer explicitly. This is the GitHub repo to reproduce the…
Jorge E. Hernández
  • 2,800
  • 1
  • 26
  • 50
0
votes
1 answer

Deserialize JSON from Riot API with kotlinx.serialization

I have some difficulties to deserialise this JSON object from RIOT API: { "type":"champion", "version":"6.1.1", "data":{ "Thresh":{ "id":412, "key":"Thresh", "name":"Thresh", "title":"the…
MakiX
  • 326
  • 3
  • 8
0
votes
4 answers

Kotlinx Deserialization Object or List of Object to List of Object

I am currently implementing an API client with Ktor. The API I am requesting does not return a consistent JSON format. for Example: sometimes the JSON looks like this: { "description": { "lang": "en", "value": "an English…
0
votes
1 answer

How to handle Any serialization with kotlinx serialization

I have a JSON response with a parameter which may be of different types depending on the enum value of another parameter. Example JSON: [ { "type":1, "value":"String" }, { "type":2, "value": 4 }, …
Marc
  • 49
  • 8
0
votes
1 answer

How to encode a raw, unescaped JSON string?

I have a Kotlin server that acts as a gateway, handling communication between a client and a number of backing services over a REST APIs, using JSON. My server uses Kotlinx Serialization for serialization. Usually I need to parse and adapt the…
aSemy
  • 5,485
  • 2
  • 25
  • 51
0
votes
1 answer

java.io.NotSerializableException with serializable data class in Kotlin (kotlinx.serialization)

I'm facing java.io.NotSerializableException when trying to writeObject and object of a data class into an ObjectOutputStream. Following is the main activity function of my project : class MainActivity : AppCompatActivity() { override fun…
0
votes
1 answer

Pass type to Json.encodeToString() without reified

I'm having problem with Json.encodeToString . Because it's required reified type . But i can't provide reified type on my program. So, how i can get it happen for below code... // I don't want to use reified fun encodeToString(model: I): String…
0
votes
1 answer

Kotlin @Serializable skip properties with default values

Why JSON doesn't content property with default value? I suppose that is serialized only what is actually needed to rebuild object, but is totally useless when i just want create API JsonResponses... import kotlinx.serialization.Serializable import…
WhipsterCZ
  • 638
  • 1
  • 8
  • 13
0
votes
1 answer

Kotlin generics Cannot use 'T' as reified type parameter. Use a class instead

Decided to try Datastore and it needs Serializer to be implemented so it can save data. I came up with such class using Protobuf: object UserDataSerializer : Serializer { override val defaultValue: LogInState =…
0
votes
1 answer

How to serialize a Kotlin Set to a JSON object whose values are empty objects?

I would like to make a Kotlin class serializable using Kotlin serialization. The class is very simple, something like this: @Serializable(with = CustomSerializer::class) data class MyObject(val keys: Set) Now, I need the serialization…
Renato
  • 12,940
  • 3
  • 54
  • 85
0
votes
0 answers

Is it possible to use two JsonClassDiscriminators in the same class hierarchy; or emulate the function of such?

I'm trying to de-serialize the following class hierarchy: @Serializable @JsonClassDiscriminator("op") @OptIn(ExperimentalSerializationApi::class) sealed class Event(val opcode: Int) { @Serializable @SerialName("0") class EXAMPLE1 private…
0
votes
0 answers

"Abstract types are not supported" when deserialising ktor session data

I use kmongo with the kotlinx serialization library and the Id implementation implementation("org.litote.kmongo:kmongo-id-serialization:4.8.0") // This is the dependency for the id implementation I declared my ktor deserialiser like that …
Giuliopime
  • 707
  • 4
  • 21
0
votes
1 answer

Kotlin Polymorphic deserialization not deserializing values

I'm trying to deserialize JSON into different sealed subclasses, the class mappings work, but the actual values are all null Example1: { "eventName": "SUCCESS", "productName": "BLAH" } Example2: { "eventName": "FAILURE", "productName":…
Jan Vladimir Mostert
  • 12,380
  • 15
  • 80
  • 137