fix #79 enum toJson serialization

This commit is contained in:
Sergey Chernov 2025-12-05 21:39:43 +01:00
parent b630d69186
commit 1e2bbe1fc5
2 changed files with 16 additions and 8 deletions

View File

@ -15,6 +15,8 @@
* *
*/ */
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import net.sergeych.lyng.Scope import net.sergeych.lyng.Scope
import net.sergeych.lyng.obj.* import net.sergeych.lyng.obj.*
import net.sergeych.lynon.LynonDecoder import net.sergeych.lynon.LynonDecoder
@ -38,6 +40,10 @@ open class ObjEnumEntry(enumClass: ObjEnumClass, val name: ObjString, val ordina
return ordinal.compareTo(scope, other.ordinal) return ordinal.compareTo(scope, other.ordinal)
} }
override suspend fun toJson(scope: Scope): JsonElement {
return JsonPrimitive(name.value)
}
} }
object EnumBase : ObjClass("Enum") { object EnumBase : ObjClass("Enum") {

View File

@ -3997,17 +3997,19 @@ class ScriptTest {
} }
@Serializable @Serializable
data class TestEnum( enum class TestEnum {
val value: Int, One, Two
val inner: JsonObject }
) @Serializable
data class TestJson4(val value: TestEnum)
@Test @Test
fun deserializeEnumJsonTest() = runTest { fun deserializeEnumJsonTest() = runTest {
val x = eval(""" val x = eval("""
import lyng.serialization import lyng.serialization
enum enum TestEnum { One, Two }
{ value: 12, inner: { "foo": 1, "bar": "two" }} { value: TestEnum.One }
""".trimIndent()).decodeSerializable<TestJson3>() """.trimIndent()).decodeSerializable<TestJson4>()
assertEquals(TestJson3(12, JsonObject(mapOf("foo" to JsonPrimitive(1), "bar" to Json.encodeToJsonElement("two")))), x) assertEquals( TestJson4(TestEnum.One), x)
} }
} }