feat: ListSerializerを追加
This commit is contained in:
parent
f76592f0fe
commit
32a8de699c
|
@ -0,0 +1,24 @@
|
|||
package dev.usbharu.ap.model.json
|
||||
|
||||
import dev.usbharu.ap.serialization.Deserializable
|
||||
import dev.usbharu.ap.serialization.Serializable
|
||||
import dev.usbharu.ap.serialization.SerializerSpec
|
||||
import kotlinx.serialization.json.JsonArray
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
class ListSerializer<T : Any>(private val clazz: KClass<T>) : Serializable<List<T>>, Deserializable<List<T>> {
|
||||
override fun deserialize(spec: SerializerSpec, element: JsonElement): List<T> {
|
||||
if (element is JsonArray) {
|
||||
return element.map { spec.deserializer(clazz).deserialize(spec, it) }
|
||||
}
|
||||
return listOf(spec.deserializer(clazz).deserialize(spec, element))
|
||||
}
|
||||
|
||||
override fun serialize(spec: SerializerSpec, value: List<T>): JsonElement {
|
||||
return buildJsonArray {
|
||||
value.forEach { add(spec.serializer(clazz).serialize(spec, it)) }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,18 @@
|
|||
package dev.usbharu.ap.model.json
|
||||
|
||||
import dev.usbharu.ap.serialization.Deserializable
|
||||
import dev.usbharu.ap.serialization.Serializable
|
||||
import dev.usbharu.ap.serialization.SerializerSpec
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
|
||||
object StringSerializer : Deserializable<String> {
|
||||
object StringSerializer : Deserializable<String>,Serializable<String> {
|
||||
override fun deserialize(spec: SerializerSpec, element: JsonElement): String {
|
||||
return element.jsonPrimitive.content
|
||||
}
|
||||
|
||||
override fun serialize(spec: SerializerSpec, value: String): JsonElement {
|
||||
return JsonPrimitive(value)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import kotlinx.serialization.json.JsonElement
|
|||
import kotlin.reflect.KClass
|
||||
|
||||
class SerializerSpecImpl(
|
||||
val deserializers: MutableMap<KClass<*>, Deserializable<*>> = mutableMapOf(),
|
||||
val serializers: Map<KClass<*>, Serializable<*>> = emptyMap()
|
||||
val deserializers: Map<KClass<*>, Deserializable<*>> = emptyMap(),
|
||||
val serializers: Map<KClass<*>, Serializable<*>> = emptyMap(),
|
||||
) : SerializerSpec {
|
||||
override fun <T : Any> deserializer(clazz: KClass<T>): Deserializable<T> =
|
||||
deserializers.getValue(clazz) as Deserializable<T>
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package dev.usbharu.ap
|
||||
|
||||
import dev.usbharu.ap.model.json.StringSerializer
|
||||
import dev.usbharu.ap.serialization.SerializerSpecImpl
|
||||
import dev.usbharu.ap.serialization.serialize
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class ListSerializerTest {
|
||||
@Test
|
||||
fun Listをシリアライズ出来るか() {
|
||||
val serializerSpecImpl = SerializerSpecImpl(
|
||||
serializers = mapOf(
|
||||
List::class to dev.usbharu.ap.model.json.ListSerializer(String::class),
|
||||
String::class to StringSerializer
|
||||
)
|
||||
)
|
||||
println(serializerSpecImpl.serialize(listOf(1,2,3)))
|
||||
}
|
||||
}
|
|
@ -33,7 +33,7 @@ class PersonSerializeTest {
|
|||
Person::class to Person.Companion,
|
||||
PrimitiveOrArray::class to PrimitiveOrArray.Companion,
|
||||
PublicKey::class to PublicKey.Companion,
|
||||
Image::class to Image.Companion
|
||||
Image::class to Image.Companion,
|
||||
)
|
||||
).serialize(person)
|
||||
println(serialize)
|
||||
|
|
Loading…
Reference in New Issue