You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm running into an issue where I need to use a JsonContentPolymorphicSerializer on a nested sealed class. The children of the nested sealed class share the same top level discriminator, and are differentiated by their content.
When running test deserialize D I get the following error:
kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Serializer for subclass 'b' is not found in the polymorphic scope of 'Base' at path: $
Check if class with serial name 'b' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'b' has to be '@Serializable', and the base class 'Base' has to be sealed and '@Serializable'.
JSON input: {
"type": "b",
"foo": 1,
"value": 2
}
at app//kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:24)
at app//kotlinx.serialization.json.internal.JsonExceptionsKt.JsonDecodingException(JsonExceptions.kt:32)
at app//kotlinx.serialization.json.internal.AbstractJsonLexer.fail(AbstractJsonLexer.kt:598)
at app//kotlinx.serialization.json.internal.AbstractJsonLexer.fail$default(AbstractJsonLexer.kt:596)
at app//kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:85)
at app//kotlinx.serialization.json.Json.decodeFromString(Json.kt:107)
Expected behavior
The JSON is deserialized to Base.InnerBase.D(2).
Environment
Kotlin version: 1.9.23
Library version: 1.6.3
Kotlin platforms: JVM (Android)
Gradle version: 8.6
The text was updated successfully, but these errors were encountered:
No, this behavior is 'as designed'. When building all subclasses' serializers for Base, serializers for non-concrete classes (i.e., InnerBase are not added — because they are sealed by default themselves. If serializer used for such InnerBase is custom, it has to be referenced in serializers module.
Perhaps this can be addressed as a part of #1865, but I'm not sure.
Describe the bug
I'm running into an issue where I need to use a
JsonContentPolymorphicSerializer
on a nested sealed class. The children of the nested sealed class share the same top level discriminator, and are differentiated by their content.To Reproduce
When running
test deserialize D
I get the following error:Expected behavior
The JSON is deserialized to
Base.InnerBase.D(2)
.Environment
The text was updated successfully, but these errors were encountered: