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

How to use custom field in polymorphic Json deserialization using kotlinx.serialization

I have the following data structure that I want to deserialize: @Serializable data class SearchResponse(val results: List) { @Serializable data class SearchResultContainer( val type: ResultType, val…
pixel
  • 24,905
  • 36
  • 149
  • 251
0
votes
1 answer

kotlinx.serialization.MissingFieldException: Error in kotlin

I am pulling a word json file data from my asset file. then I try to save these data in my room database. I 'm gonna read these data from room after I save. this is my plan but I have a error. I will share my error and codes below This json file…
NewPartizal
  • 604
  • 4
  • 18
0
votes
0 answers

How use kotlinx.serialization decode generics list?

i have a json like this: {"code":200,"msg":"111",data:[{"name":"va","list":[{"code":"11"}]},{"name":"va","list":[{"code":"11"}]}]} my class like this: @Serializable class ServerResponse { @SerialName(value = "code") var code = 0 …
Gary
  • 13
  • 4
0
votes
1 answer

Error: Class is not registered for polymorphic serialization in the scope of its interface

I need set serializable interface for using any data class in methods, example data: @Serializable interface Todo{} @Serializable data class userDataForRegistration(val name: String, val number: String, val password: String):…
rjunovskii
  • 23
  • 5
0
votes
0 answers

kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the object '{', but had 'EOF' instead JSON input: collocations.json

I'm trying to parce my collocation.json file from asset file in kotlin. I wrote some codes for this. There is a json file named collocations.json in my own asset file and I want to parse it, but when I run the project, the following error appears.…
NewPartizal
  • 604
  • 4
  • 18
0
votes
0 answers

Why do we need kotlinx custom serialization?

I have an curiosity of asking about why do we need custom kotlinx serialization ? What I understand is whenever there is an object inside another object then we need to write custom serializer for that particular object for example : data class…
0
votes
0 answers

kotlinx.serialization - partial deserialization

I have a very large json file within my android app with the following struture: [ { "id": 123, "data": { ... } }, { "id": 456, "data": { ... } }, ... ] Depending on the item selected in the UI, I need…
0
votes
1 answer

typealias for global kotlinx.serialization not work

First I'll post the code @OptIn(ExperimentalSerializationApi::class) @Serializer(forClass = UUID::class) object UUIDserializer : KSerializer { override fun deserialize(decoder: Decoder): UUID = UUID.fromString(decoder.decodeString()) …
Kyle M
  • 119
  • 7
0
votes
1 answer

Deserialize empty json value to null with kotlinx.serialization

I have the following response from a backend: { "title": "House", "translations": { "es": "Casa", "fr": "Maison", "de": "Haus" } } To process it I am using the kotlinx serializer and this is my data class. @Serializable data class…
rimes
  • 761
  • 1
  • 8
  • 25
0
votes
2 answers

How to get a nested specific json property with Kotlinx serialization in Ktor Kotlin without a data class?

I'm writing an web app with Kotlin and Ktor and I want to parse the json response from Google Books Api and select some specific values from it to store. For serialization I am using Kotlinx serialization. I want to avoid creating a data class The…
nicowi
  • 470
  • 2
  • 4
  • 15
0
votes
0 answers

Parse a byte array into a ByteArray

from a server I get an array of bytes, but Kotlinx serializable doesen't seem to like it as it can't parse it to the native Kotlin datatype ByteArray and logs the error Unexpected JSON token at offset 902: Failed to parse 'byte': JSON = "my_bytes":…
WiseEye
  • 133
  • 9
0
votes
0 answers

Is there a way to serialize object without putting it to element in descriptor and opening new structure?

I have a class and a custom serializer for it. Also I have a Layer object and another serializer and descriptor for it. internal data class LayerItem( internal var x: Float = 0f, internal var y: Float = 0f, internal var w: Float = 100f, …
0
votes
1 answer

Serializer for class '...' is not found. ktor + kotlinx.serialization

I am using ktor + kotlinx.serialization and i want to achieve a json response from call.respond(Respond("some mesage",null)) something like this : when result = null { "message" : "some mesage" } when result is any Type { "message" : "some…
aeri79
  • 15
  • 6
0
votes
1 answer

Create generic transform serializer

I have a beautiful JSON, where numbers (but not only) are "nulled" in the flowing way: [{ "bar": "" }, { "bar": 123 }] I would like to parse it to the: data class(val bar: Long?) I found out it can be quite easily done with following…
majkrzak
  • 1,332
  • 3
  • 14
  • 30
0
votes
1 answer

Kotlinx serialization serializer not found for arraylist

I am trying to set a property for my feature which is a list of string objects. Which for the feature has to be converted to a jsonElement. But when I try to get the result I get a serialization error for a ArrayList instead. The error is the…
MrAndre
  • 811
  • 1
  • 10
  • 26