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
Requirement is to decode polymorphic JSON, where the JSON can be one of
String
Set<String>
Map<String, String>
To try to solve this I created a sealed interface, AnyValue, with 3 value class subtypes. To polymorphically decode I defined a JsonContentPolymorphicSerializer.
selected Single.serializer()
Single(value=I'm a string)
selected Complex.serializer()
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 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.remove(ArrayList.java:536)
at kotlinx.serialization.internal.TaggedDecoder.popTag(Tagged.kt:322)
at kotlinx.serialization.internal.TaggedDecoder.decodeInline(Tagged.kt:213)
at AnyValue$Complex$$serializer.deserialize-0-2muUA(main.kt:30)
at AnyValue$Complex$$serializer.deserialize(main.kt:30)
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:115)
at kotlinx.serialization.json.JsonContentPolymorphicSerializer.deserialize(JsonContentPolymorphicSerializer.kt:93)
at kotlinx.serialization.json.internal.StreamingJsonDecoder.decodeSerializableValue(StreamingJsonDecoder.kt:70)
at kotlinx.serialization.json.Json.decodeFromString(Json.kt:95)
at MainKt.main(main.kt:86)
at MainKt.main(main.kt)
Expected behavior
""" {"id": "1", "name": "object"} """ can be decoded to AnyValue.Complex
"""["list", "of", "strings"]""" can be decoded to AnyValue.Multi
Environment
Kotlin version: 1.8.0
Library version: 1.4.1
Kotlin platforms: JVM
Gradle version: 7.8
The text was updated successfully, but these errors were encountered:
- Value class is located at top-level, but wraps non-primitive and thus does not fall in 'primitive on top-level' branch
- Value class is a subclass in a polymorphic hierarchy, but either is primitive or explicitly recorded without type info
Note that type info is omitted in the latter case and 'can't add type info to primitive' error is not thrown deliberately, as
there seems to be use-cases for that.
Fixes#1774Fixes#2159
- Value class is located at top-level, but wraps non-primitive and thus does not fall in 'primitive on top-level' branch
- Value class is a subclass in a polymorphic hierarchy, but either is primitive or explicitly recorded without type info
Note that type info is omitted in the latter case and 'can't add type info to primitive' error is not thrown deliberately, as
there seems to be use-cases for that.
FixesKotlin#1774FixesKotlin#2159
Describe the bug
Requirement is to decode polymorphic JSON, where the JSON can be one of
String
Set<String>
Map<String, String>
To try to solve this I created a sealed interface,
AnyValue
, with 3 value class subtypes. To polymorphically decode I defined aJsonContentPolymorphicSerializer
.To Reproduce
Expected behavior
""" {"id": "1", "name": "object"} """
can be decoded toAnyValue.Complex
"""["list", "of", "strings"]"""
can be decoded toAnyValue.Multi
Environment
The text was updated successfully, but these errors were encountered: