-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expand on ComponentListAsMapSerializer to support any polymorphic ser…
…ializer
- Loading branch information
Showing
5 changed files
with
135 additions
and
32 deletions.
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
83 changes: 83 additions & 0 deletions
83
addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/SerializerTest.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,83 @@ | ||
package com.mineinabyss.geary.prefabs | ||
|
||
import com.mineinabyss.geary.prefabs.serializers.PolymorphicListAsMapSerializer | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.serialization.Polymorphic | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlinx.serialization.modules.subclass | ||
import org.junit.jupiter.api.Test | ||
|
||
class SerializerTest { | ||
interface Components | ||
|
||
@SerialName("test:thing.a") | ||
@Serializable | ||
object A : Components | ||
|
||
@SerialName("test:thing.b") | ||
@Serializable | ||
object B : Components | ||
|
||
private val json = Json { | ||
serializersModule = SerializersModule { | ||
polymorphic(Components::class) { | ||
subclass(A::class) | ||
subclass(B::class) | ||
} | ||
} | ||
} | ||
|
||
@Serializable | ||
class SerializerTest(val components: @Serializable(with = PolymorphicListAsMapSerializer::class) List<@Polymorphic Components>) | ||
|
||
@Test | ||
fun `should serialize via @Serializable`() { | ||
json.decodeFromString( | ||
SerializerTest.serializer(), | ||
""" | ||
{ | ||
"components": { | ||
"test:thing.a": {}, | ||
"test:thing.b": {} | ||
} | ||
} | ||
""".trimIndent() | ||
).components shouldBe listOf(A, B) | ||
} | ||
|
||
@Test | ||
fun `should support subkey syntax`() { | ||
json.decodeFromString( | ||
SerializerTest.serializer(), | ||
""" | ||
{ | ||
"components": { | ||
"test:thing.*": { | ||
"a": {}, | ||
"b": {} | ||
} | ||
} | ||
} | ||
""".trimIndent() | ||
).components shouldBe listOf(A, B) | ||
} | ||
@Test | ||
fun `should support importing namespaces`() { | ||
json.decodeFromString( | ||
SerializerTest.serializer(), | ||
""" | ||
{ | ||
"components": { | ||
"namespaces": ["test"], | ||
"thing.a": {}, | ||
"thing.b": {} | ||
} | ||
} | ||
""".trimIndent() | ||
).components shouldBe listOf(A, B) | ||
} | ||
} |
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