From ab2eae9daaf32f532de5618d6da7fa5938eb3a9b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 1 Apr 2023 09:55:48 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20type=E3=81=AA=E3=81=A9=E3=81=AE?= =?UTF-8?q?=E8=A6=81=E7=B4=A0=E3=81=8C=E4=B8=80=E3=81=A4=E3=81=AE=E3=81=A8?= =?UTF-8?q?=E3=81=8D=E9=85=8D=E5=88=97=E3=81=A7=E3=81=AF=E3=81=AA=E3=81=8F?= =?UTF-8?q?=E6=96=87=E5=AD=97=E5=88=97=E3=81=A7=E8=BF=94=E3=81=99=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/hideout/ap/JsonLd.kt | 24 +++++++++++++++++++ .../kotlin/dev/usbharu/hideout/ap/Object.kt | 22 +++++++++++++++++ .../hideout/ap/ContextSerializerTest.kt | 23 ++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/ap/ContextSerializerTest.kt diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt b/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt index b4749c7b..51985b34 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/JsonLd.kt @@ -3,15 +3,20 @@ package dev.usbharu.hideout.ap import com.fasterxml.jackson.annotation.JsonAutoDetect import com.fasterxml.jackson.annotation.JsonCreator import com.fasterxml.jackson.annotation.JsonProperty +import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.core.TreeNode import com.fasterxml.jackson.databind.JsonDeserializer import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.databind.annotation.JsonSerialize @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) open class JsonLd { @JsonProperty("@context") @JsonDeserialize(contentUsing = ContextDeserializer::class) + @JsonSerialize(using = ContextSerializer::class) var context:List = emptyList() @JsonCreator @@ -31,3 +36,22 @@ public class ContextDeserializer : JsonDeserializer() { return readTree.asText() } } + +public class ContextSerializer : JsonSerializer>() { + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider?) { + if (value.isNullOrEmpty()) { + gen?.writeNull() + return + } + if (value?.size == 1) { + gen?.writeString(value[0]) + } else { + gen?.writeStartArray() + value?.forEach { + gen?.writeString(it) + } + gen?.writeEndArray() + } + } + +} diff --git a/src/main/kotlin/dev/usbharu/hideout/ap/Object.kt b/src/main/kotlin/dev/usbharu/hideout/ap/Object.kt index aa3aa920..76d609e5 100644 --- a/src/main/kotlin/dev/usbharu/hideout/ap/Object.kt +++ b/src/main/kotlin/dev/usbharu/hideout/ap/Object.kt @@ -1,6 +1,12 @@ package dev.usbharu.hideout.ap +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.databind.JsonSerializer +import com.fasterxml.jackson.databind.SerializerProvider +import com.fasterxml.jackson.databind.annotation.JsonSerialize + open class Object : JsonLd { + @JsonSerialize(using = TypeSerializer::class) private var type: List = emptyList() var name: String? = null @@ -19,3 +25,19 @@ open class Object : JsonLd { } } } + +public class TypeSerializer : JsonSerializer>() { + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider?) { + println(value) + if (value?.size == 1) { + gen?.writeString(value[0]) + } else { + gen?.writeStartArray() + value?.forEach { + gen?.writeString(it) + } + gen?.writeEndArray() + } + } + +} diff --git a/src/test/kotlin/dev/usbharu/hideout/ap/ContextSerializerTest.kt b/src/test/kotlin/dev/usbharu/hideout/ap/ContextSerializerTest.kt new file mode 100644 index 00000000..5558cb68 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/ap/ContextSerializerTest.kt @@ -0,0 +1,23 @@ +package dev.usbharu.hideout.ap + +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +class ContextSerializerTest{ + + @Test + fun serialize() { + val accept = Accept( + name = "aaa", + actor = "bbb", + `object` = Follow( + name = "ccc", + `object` = "ddd", + actor = "aaa" + ) + ) + val writeValueAsString = jacksonObjectMapper().writeValueAsString(accept) + println(writeValueAsString) + } +}