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

Are there default serializer implementations for common Java classes?

I'm trying out kotlinx.serializaion, and finding the lack of default support for common types a bit off-putting. Take for example java.util.UUID; even applying @ContextualSerialization, I get an error that the compiler can't locate an argument-less…
Rollie
  • 4,391
  • 3
  • 33
  • 55
4
votes
1 answer

How to serialize dynamic keys of a json with kotlinx

I have a json with this kind of key: ... "metaData": { "date:create": "2019-11-13t15:42:02+01:00", "date:modify": "2019-11-13t15:42:02+01:00", "exif:ColorSpace": "1", "exif:ExifImageLength": "1500", …
Dylan Bourdere
  • 105
  • 2
  • 10
4
votes
2 answers

Serializing Lists with external kotlinx Serializer

So, I have this class Item.kt class Item { val name = "" val loc = "" val price = 0.0 override fun toString() = "$name <$loc> $price" } Since this class is in another library (I can't edit its source), I have an external Serializer…
andylamax
  • 1,858
  • 1
  • 14
  • 31
4
votes
1 answer

Can Ktor HttpClient JsonFeature use kotlinx.serialization

Ktor's HttpClient allows installing a Json feature. Does anyone know if this feature can use kotlinx.serialization library or is it only limited to Gson? I'm interested in enabling the Json feature for MPP projects.
Y2i
  • 3,748
  • 2
  • 28
  • 32
3
votes
1 answer

Kotlinx-serialization: Serialize raw string without quotes

What I try to achieve Using Jackson for serialization, there is the @JsonRawValue annotation to achieve that a string value is serialized without quotes. For instance, with the following code data class SomeData { @JsonRawValue value:…
3
votes
1 answer

Multiple names for JSON field

I use kotlinx.serialization library to serialize/deserialize JSONs. There is a JSON string: {"id":"1"} that can be also represented as {"uid":"1"} And I want to handle both names with one property, e.g.: @Serializable data class User(val id:…
Sergio
  • 27,326
  • 8
  • 128
  • 149
3
votes
0 answers

Deserialize empty string as null with kotlinx.serialization

I'm writing a client for a third-party REST API that returns JSON with a variety of alternative values instead of proper null or omitting the property entirely if null. Depending on the entity or even property in question, null could be represented…
Rolf W.
  • 1,379
  • 1
  • 15
  • 25
3
votes
1 answer

Polymorphic kotlinx serialization when type is integer, not string

I am trying to consume and emit JSON which contains a polymorphic list of items. The problem is: the items contain type key with integer values (not strings). The API endpoint produces and expects JSON similar to this: { "startTime":…
Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
3
votes
1 answer

Serialization omitting nulls

I have a class like this: @Serializable data class MyClass( val prop1: Int, val prop2: Int? ) When prop2 is null, I want to serialize this class without including the prop2 property. I can do this like this: val json = Json { explicitNulls…
k314159
  • 5,051
  • 10
  • 32
3
votes
2 answers

Serialize generic class using kotlinix.serialization in Kotlin/JS fails

Serializing a generic class with kotlinx.serialization succeeds in JVM but fails in JavaScript with message TypeError: tmp$.serializer is not a function. Please, see the following Unit Test. import kotlinx.serialization.Serializable import…
Jordi
  • 2,055
  • 1
  • 16
  • 34
3
votes
1 answer

JSON Serialization / Deserialization Kotlin Android Jetpack Compose Table

I'm in the process of creating a table section in Jetpack Compose that deserializes the data from a JSON. At this point, I'm simply trying to display the data (the table formatting and clean up will come later, so you can ignore that part). Things…
petestmart
  • 500
  • 11
  • 27
3
votes
2 answers

How do I handle Request Body Error in Ktor

I am new to Ktor and I have a route with a request body which i am parsing with Kotlin Serialization. I know that the request body is expected to conform to the request body data class but then, I tested by passing the wrong field in my test payload…
Joseph Ofem
  • 304
  • 2
  • 15
3
votes
1 answer

kotlinx serialization — best way to do polymorphic child deserialization

I have a Json input like: { "type": "type_1", "data": { // ... } } data field can vary depending on type. So, I need a deserializer, that looks on type (enum) and deserializes data respectively (for instance, for type_1 value it's…
artem
  • 16,382
  • 34
  • 113
  • 189
3
votes
1 answer

Meaning of -if in Proguard

For the following Proguard rule(take Kotlin Serialization) -if @kotlinx.serialization.Serializable class ** -keepclassmembers class <1> { static <1>$Companion Companion; } Here what is the meaning of -if in context with the above rule? I tried…
AndroidEngineX
  • 975
  • 1
  • 9
  • 22
3
votes
0 answers

How to create custom serialization in Kotlin with java.io.serializable

I am attempting to serialize with java.io.Serializable a Kotlin class that is already serializable with kotlinx.serialization. The java serialization will not work out of the box because some fields in the class are not serializable by default. But,…
Eytan
  • 728
  • 1
  • 7
  • 19