I hate kotlinx serialization errors so much they s...
# serialization
z
I hate kotlinx serialization errors so much they sometimes give okay errors, but other times it's ones that are so vague it makes it impossible to diagnose. I cannot pinpoint the exact cause of the error in my application that has multiple layers for serialization. is there anything I can try to at least narrow down the exact cause of an error?
o
Can't expect to get help if you don't post the error.
Be aware that sometimes
kotlinx.serialization
errors will give you a completely different piece of the input than where the actual error occurred, throwing you a red herring.
If you get an error that points to an OBVIOUSLY correct input, look at the adjacent space. Especially with JSON arrays the error might have occurred at the beginning of the array while the library prints the tail as the "incorrect input".
z
Copy code
io.ktor.serialization.JsonConvertException: Illegal input
	at io.ktor.serialization.kotlinx.KotlinxSerializationConverter.deserialize(KotlinxSerializationConverter.kt:90)
	at io.ktor.serialization.kotlinx.KotlinxSerializationConverter$deserialize$1.invokeSuspend(Unknown Source:15)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
	at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Caused by: kotlinx.serialization.json.internal.JsonDecodingException: Expected class kotlinx.serialization.json.JsonArray (Kotlin reflection is not available) as the serialized body of kotlin.collections.ArrayList, but had class kotlinx.serialization.json.JsonObject (Kotlin reflection is not available)
	at kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
	at kotlinx.serialization.json.internal.AbstractJsonTreeDecoder.beginStructure(TreeJsonDecoder.kt:335)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:29)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:61)
	at kotlinx.serialization.json.internal.AbstractJsonTreeDecoder.decodeSerializableValue(TreeJsonDecoder.kt:52)
	at kotlinx.serialization.internal.TaggedDecoder.decodeSerializableValue(Tagged.kt:207)
	at kotlinx.serialization.internal.TaggedDecoder$decodeSerializableElement$1.invoke(Tagged.kt:280)
	at kotlinx.serialization.internal.TaggedDecoder.tagBlock(Tagged.kt:297)
	at kotlinx.serialization.internal.TaggedDecoder.decodeSerializableElement(Tagged.kt:280)
	at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:533)
	at kotlinx.serialization.internal.CollectionLikeSerializer.readElement(CollectionSerializers.kt:80)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:61)
	at kotlinx.serialization.json.internal.AbstractJsonTreeDecoder.decodeSerializableValue(TreeJsonDecoder.kt:52)
	at kotlinx.serialization.internal.TaggedDecoder.decodeSerializableValue(Tagged.kt:207)
	at kotlinx.serialization.internal.TaggedDecoder$decodeSerializableElement$1.invoke(Tagged.kt:280)
	at kotlinx.serialization.internal.TaggedDecoder.tagBlock(Tagged.kt:297)
	at kotlinx.serialization.internal.TaggedDecoder.decodeSerializableElement(Tagged.kt:280)
	at kotlinx.serialization.encoding.CompositeDecoder$DefaultImpls.decodeSerializableElement$default(Decoding.kt:533)
	at kotlinx.serialization.internal.CollectionLikeSerializer.readElement(CollectionSerializers.kt:80)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:61)
	at kotlinx.serialization.json.internal.AbstractJsonTreeDecoder.decodeSerializableValue(TreeJsonDecoder.kt:52)
	at kotlinx.serialization.json.internal.TreeJsonDecoderKt.readJson(TreeJsonDecoder.kt:25)
	at kotlinx.serialization.json.Json.decodeFromJsonElement(Json.kt:127)
	at kotlinx.serialization.json.JsonTransformingSerializer.deserialize(JsonTransformingSerializer.kt:78)
	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:70)
+ many more internal lines
b
Was there more to the
Caused by
message? It looks cut off in the middle of
reflection
. I think the error comes from here, and this full message seems like it would be useful. I could see a JSON path being useful too, but besides that, the message has the JSON (
value
) it tried to parse, and the
descriptor
it tried to parse it as, and that seems pretty helpful. Maybe intimidating though if you're new to the library?
z
Oh I didn't notice that, I've updated the stacktrace
311 Views