This repository was archived by the owner on Nov 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +29
-4
lines changed
main/kotlin/io/github/dockyardmc/scroll Expand file tree Collapse file tree 4 files changed +29
-4
lines changed Original file line number Diff line number Diff line change 11<component name =" ProjectRunConfigurationManager" >
22 <configuration default =" false" name =" Run Tests" type =" JUnit" factoryName =" JUnit" >
33 <module name =" Scroll.test" />
4- <option name =" ALTERNATIVE_JRE_PATH_ENABLED" value =" true" />
5- <option name =" ALTERNATIVE_JRE_PATH" value =" corretto-21" />
64 <option name =" PACKAGE_NAME" value =" " />
75 <option name =" MAIN_CLASS_NAME" value =" " />
86 <option name =" METHOD_NAME" value =" " />
Original file line number Diff line number Diff line change 11package io.github.dockyardmc.scroll
22
3+ import io.github.dockyardmc.scroll.serializers.ComponentDeserializer
34import io.github.dockyardmc.scroll.serializers.ComponentToJsonSerializer
45import io.github.dockyardmc.scroll.serializers.ComponentSerializer
6+ import kotlinx.serialization.KeepGeneratedSerializer
57import kotlinx.serialization.SerialName
68import kotlinx.serialization.Serializable
79import net.kyori.adventure.nbt.CompoundBinaryTag
810
9- @Serializable
11+ @KeepGeneratedSerializer
12+ @Serializable(with = ComponentDeserializer ::class )
1013open class Component (
1114 var extra : MutableList <Component >? = null ,
1215 var keybind : String? = null ,
Original file line number Diff line number Diff line change @@ -2,9 +2,23 @@ package io.github.dockyardmc.scroll.serializers
22
33import io.github.dockyardmc.scroll.Component
44import kotlinx.serialization.json.Json
5+ import kotlinx.serialization.json.JsonElement
6+ import kotlinx.serialization.json.JsonPrimitive
7+ import kotlinx.serialization.json.JsonTransformingSerializer
8+ import kotlinx.serialization.json.buildJsonObject
59
610object JsonToComponentSerializer {
711 fun serialize (json : String ): Component {
8- return Json .decodeFromString<Component >(json)
12+ return Json .decodeFromString(ComponentDeserializer (), json)
13+ }
14+ }
15+
16+ internal class ComponentDeserializer : JsonTransformingSerializer <Component >(Component .generatedSerializer()) {
17+ override fun transformDeserialize (element : JsonElement ): JsonElement = when (element) {
18+ is JsonPrimitive -> buildJsonObject {
19+ put(" text" , element)
20+ }
21+
22+ else -> element
923 }
1024}
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import io.github.dockyardmc.scroll.ClickEvent
22import io.github.dockyardmc.scroll.Component
33import io.github.dockyardmc.scroll.serializers.JsonToComponentSerializer
44import io.github.dockyardmc.scroll.extensions.toComponent
5+ import org.junit.jupiter.api.assertDoesNotThrow
56import kotlin.test.Test
67import kotlin.test.assertEquals
78
@@ -43,4 +44,13 @@ class JsonSerializationTests {
4344
4445 assertEquals(input.toJson(), expected)
4546 }
47+
48+ @Test
49+ fun testJsonStringToComponent () {
50+ val input = " \" test_text\" "
51+ val expected = Component (text = " test_text" )
52+
53+ val result = assertDoesNotThrow { JsonToComponentSerializer .serialize(input) }
54+ assertEquals(expected.toJson(), result.toJson())
55+ }
4656}
You can’t perform that action at this time.
0 commit comments