fix #79 enum toJson serialization
This commit is contained in:
parent
b630d69186
commit
1e2bbe1fc5
@ -15,6 +15,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lyng.obj.*
|
||||
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)
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement {
|
||||
return JsonPrimitive(name.value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object EnumBase : ObjClass("Enum") {
|
||||
|
||||
@ -3997,17 +3997,19 @@ class ScriptTest {
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class TestEnum(
|
||||
val value: Int,
|
||||
val inner: JsonObject
|
||||
)
|
||||
enum class TestEnum {
|
||||
One, Two
|
||||
}
|
||||
@Serializable
|
||||
data class TestJson4(val value: TestEnum)
|
||||
|
||||
@Test
|
||||
fun deserializeEnumJsonTest() = runTest {
|
||||
val x = eval("""
|
||||
import lyng.serialization
|
||||
enum
|
||||
{ value: 12, inner: { "foo": 1, "bar": "two" }}
|
||||
""".trimIndent()).decodeSerializable<TestJson3>()
|
||||
assertEquals(TestJson3(12, JsonObject(mapOf("foo" to JsonPrimitive(1), "bar" to Json.encodeToJsonElement("two")))), x)
|
||||
enum TestEnum { One, Two }
|
||||
{ value: TestEnum.One }
|
||||
""".trimIndent()).decodeSerializable<TestJson4>()
|
||||
assertEquals( TestJson4(TestEnum.One), x)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user