question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error in deserialize function for parameterized abstract and sealed classes

See original GitHub issue

Describe the bug Errors occurred when trying to deserialize instances for parameterized abstract and sealed classes.

To Reproduce

    @Serializable
    sealed class TypedSealedClass<T>(val a: T) {
        @Serializable
        data class Child(val y: Int) : TypedSealedClass<Int>(10)
    }

and try to deserialize it

        val child = Json.encodeToString(TypedSealedClass.serializer(Int.serializer()), TypedSealedClass.Child(10))
        Json.decodeFromString(TypedSealedClass.serializer(Int.serializer()), child)

For JVM backend error occurred

java.lang.NoSuchFieldError: typeSerial0
	at com.jetbrains.example.serialization.MainTest$TypedSealedClass$Child$$serializer.deserialize(Project.kt)
	at com.jetbrains.example.serialization.MainTest$TypedSealedClass$Child$$serializer.deserialize(Project.kt:23)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:63)
	at kotlinx.serialization.json.internal.AbstractJsonTreeDecoder.decodeSerializableValue(TreeJsonDecoder.kt:51)
	at kotlinx.serialization.json.internal.TreeJsonDecoderKt.readPolymorphicJson(TreeJsonDecoder.kt:32)
	at kotlinx.serialization.json.internal.PolymorphicKt.decodeSerializableValuePolymorphic(Polymorphic.kt:73)
	at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:33)
	at kotlinx.serialization.json.Json.decodeFromString(Json.kt:85)
        ...

for JVM IR

e: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:372)
	at java.base/java.util.ArrayList.get(ArrayList.java:458)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.IrBuilderExtension$serializerInstance$1.invoke(GeneratorHelpers.kt:623)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.IrBuilderExtension$serializerInstance$1.invoke(GeneratorHelpers.kt:622)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.IrBuilderExtension$DefaultImpls.serializerInstance(GeneratorHelpers.kt:639)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator.serializerInstance(SerializerIrGenerator.kt:45)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.IrBuilderExtension$DefaultImpls.serializerInstance(GeneratorHelpers.kt:616)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator.serializerInstance(SerializerIrGenerator.kt:45)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator$generateSave$1.invoke(SerializerIrGenerator.kt:540)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator$generateSave$1.invoke(SerializerIrGenerator.kt:236)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.IrBuilderExtension$DefaultImpls.contributeFunction(GeneratorHelpers.kt:55)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator.contributeFunction(SerializerIrGenerator.kt:45)
	at org.jetbrains.kotlinx.serialization.compiler.backend.ir.SerializerIrGenerator.generateSave(SerializerIrGenerator.kt:236)
	at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generateSaveIfNeeded(SerializerCodegen.kt:109)
	at org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializerCodegen.generate(SerializerCodegen.kt:37)
        ...

same for abstract classes

Expected behavior deserialize finished successfully

Environment

  • Kotlin version: 1.4.30-M1
  • Library version: 1.0.1
  • Kotlin platforms: JVM, JVM IR

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sandwwraithcommented, Oct 4, 2022

There’s an easy workaround, the goal is to get rid of concrete property in parent class:

Change

    @Serializable
    sealed class TypedSealedClass<T>(val a: T) {
        @Serializable
        data class Child(val y: Int) : TypedSealedClass<Int>(10)
    }

Into

    @Serializable
    sealed class TypedSealedClass<T>() {
        abstract val a: T
        
        @Serializable
        data class Child(val y: Int) : TypedSealedClass<Int>() {
            @EncodeDefault override val a: Int = 10
        }
    }
2reactions
phreakadelle2kcommented, Aug 25, 2022

This is still an issue in “org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0” in combination with Kotlin 1.7.10 and Java 17.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlinx Serialization, inlining sealed class/interface [duplicate]
The answer with kotlinx deserialization: different types && scalar && arrays is basically the answer, and the one I will accept.
Read more >
Using Kotlin Sealed classes to map out a domain space
The following is one of my favorite Kotlinic patterns: Using sealed classes to enumerate all possible return value groups of an API.
Read more >
Sealed Classes - Oracle Help Center
A formal parameter of an abstract method of an interface type or annotation type (9.4, 9.6.1). An exception parameter of an exception handler...
Read more >
How to serialize properties of derived classes with System ...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
Read more >
C# | Sealed Class - GeeksforGeeks
Now, if it tries to inherit a class from a sealed class then an error will be produced stating that ” It cannot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found