-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prohibited use of elements other than JsonObject is When polymorphic …
…serialization for JsonTransformingSerializer If JsonTransformingSerializer is used as a serializer for the descendant of a polymorphic class, then we do not know how to add a type discriminator to the returned result of a primitive or array type. Since there is no general solution to this problem on the library side, the user must take care of the correct processing of such types and, possibly, manually implement polymorphism. Resolves #2164
- Loading branch information
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
formats/json-tests/commonTest/src/kotlinx/serialization/JsonElementPolymorphicErrorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization | ||
|
||
|
||
import kotlinx.serialization.json.* | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlinx.serialization.test.* | ||
import kotlin.test.* | ||
|
||
class JsonElementPolymorphicErrorTest : JsonTestBase() { | ||
|
||
@Serializable | ||
abstract class Abstract | ||
|
||
@Serializable | ||
data class IntChild(val value: Int) : Abstract() | ||
|
||
@Serializable | ||
data class CollectionChild(val value: Int) : Abstract() | ||
|
||
@Serializable | ||
data class Holder(val value: Abstract) | ||
|
||
private val format = Json { | ||
prettyPrint = false | ||
serializersModule = SerializersModule { | ||
polymorphic(Abstract::class) { | ||
subclass(IntChild::class, IntChildSerializer) | ||
subclass(CollectionChild::class, CollectionChildSerializer) | ||
} | ||
} | ||
} | ||
|
||
object IntChildSerializer : JsonTransformingSerializer<IntChild>(serializer()) { | ||
override fun transformSerialize(element: JsonElement): JsonElement { | ||
return element.jsonObject.getValue("value") | ||
} | ||
} | ||
|
||
object CollectionChildSerializer : JsonTransformingSerializer<CollectionChild>(serializer()) { | ||
override fun transformSerialize(element: JsonElement): JsonElement { | ||
val value = element.jsonObject.getValue("value") | ||
return JsonArray(listOf(value)) | ||
} | ||
} | ||
|
||
@Test | ||
fun test() = parametrizedTest { mode -> | ||
assertFailsWithMessage<SerializationException>("Json element JsonLiteral cannot be serialized polymorphous, for serial name 'kotlinx.serialization.JsonElementPolymorphicErrorTest.IntChild'") { | ||
format.encodeToString( | ||
Holder.serializer(), | ||
Holder(IntChild(42)), | ||
mode | ||
) | ||
} | ||
|
||
assertFailsWithMessage<SerializationException>("Json element JsonArray cannot be serialized polymorphous, for serial name 'kotlinx.serialization.JsonElementPolymorphicErrorTest.CollectionChild'") { | ||
format.encodeToString( | ||
Holder.serializer(), | ||
Holder(CollectionChild(42)), | ||
mode | ||
) | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters