-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create an implementation that allows automatically registering the se…
…aled children of a class for polymorphic serialization of a shared (non-sealed) type.
- Loading branch information
Showing
4 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
...ts/json-tests/commonTest/src/kotlinx/serialization/features/PolymorphicSealedChildTest.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,65 @@ | ||
/* | ||
* Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.features | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.* | ||
import kotlinx.serialization.test.assertStringFormAndRestored | ||
import kotlin.test.Test | ||
|
||
class PolymorphicSealedChildTest { | ||
|
||
@Serializable | ||
data class FooHolder( | ||
val someMetadata: Int, | ||
val payload: List<@Polymorphic FooBase> | ||
) | ||
|
||
@Serializable | ||
abstract class FooBase | ||
|
||
@Serializable | ||
@SerialName("Foo") | ||
sealed class Foo: FooBase() { | ||
@Serializable | ||
@SerialName("Bar") | ||
data class Bar(val bar: Int) : Foo() | ||
@Serializable | ||
@SerialName("Baz") | ||
data class Baz(val baz: String) : Foo() | ||
} | ||
|
||
val sealedModule = SerializersModule { | ||
polymorphic(FooBase::class) { | ||
subclassesOf(Foo.serializer()) | ||
} | ||
} | ||
|
||
val json = Json { serializersModule = sealedModule } | ||
|
||
@Test | ||
fun testSaveSealedClassesList() { | ||
assertStringFormAndRestored( | ||
"""{"someMetadata":42,"payload":[ | ||
|{"type":"Bar","bar":1}, | ||
|{"type":"Baz","baz":"2"}]}""".trimMargin().replace("\n", ""), | ||
FooHolder(42, listOf(Foo.Bar(1), Foo.Baz("2"))), | ||
FooHolder.serializer(), | ||
json, | ||
printResult = true | ||
) | ||
} | ||
|
||
@Test | ||
fun testCanSerializeSealedClassPolymorphicallyOnTopLevel() { | ||
assertStringFormAndRestored( | ||
"""{"type":"Bar","bar":1}""", | ||
Foo.Bar(1), | ||
PolymorphicSerializer(FooBase::class), | ||
json | ||
) | ||
} | ||
} |