From ae43b5e793ebff5e41194d871b16f3af26b1b82f Mon Sep 17 00:00:00 2001 From: usbharu Date: Tue, 14 May 2024 09:32:35 +0900 Subject: [PATCH] wip --- .../activitypub/domain/model/JsonLd.kt | 20 +++-- .../domain/model/StringOrObject.kt | 79 +++++++++++++++++++ 2 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt index 0e1a3d79..ca6a1371 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/JsonLd.kt @@ -34,15 +34,15 @@ open class JsonLd { @JsonDeserialize(contentUsing = ContextDeserializer::class) @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class) @JsonInclude(JsonInclude.Include.NON_EMPTY) - var context: List = emptyList() + var context: List = emptyList() set(value) { - field = value.filterNotNull().filter { it.isNotBlank() } + field = value.filter { it.isEmpty() } } @JsonCreator - constructor(context: List?) { + constructor(context: List?) { if (context != null) { - this.context = context.filterNotNull().filter { it.isNotBlank() } + this.context = context.filterNotNull().filter { it.isEmpty() } } else { this.context = emptyList() } @@ -76,14 +76,14 @@ class ContextDeserializer : JsonDeserializer() { } } -class ContextSerializer : JsonSerializer>() { +class ContextSerializer : JsonSerializer>() { @Deprecated("Deprecated in Java") - override fun isEmpty(value: List?): Boolean = value.isNullOrEmpty() + override fun isEmpty(value: List?): Boolean = value.isNullOrEmpty() - override fun isEmpty(provider: SerializerProvider?, value: List?): Boolean = value.isNullOrEmpty() + override fun isEmpty(provider: SerializerProvider?, value: List?): Boolean = value.isNullOrEmpty() - override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider) { + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider) { if (value.isNullOrEmpty()) { serializers.defaultSerializeNull(gen) return @@ -98,4 +98,8 @@ class ContextSerializer : JsonSerializer>() { gen?.writeEndArray() } } + + override fun serialize(value: List?, gen: JsonGenerator?, serializers: SerializerProvider?) { + + } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt new file mode 100644 index 00000000..b0773b5a --- /dev/null +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/activitypub/domain/model/StringOrObject.kt @@ -0,0 +1,79 @@ +package dev.usbharu.hideout.activitypub.domain.model + +import com.fasterxml.jackson.annotation.JsonCreator +import com.fasterxml.jackson.core.JsonGenerator +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.* + +open class StringOrObject { + var contextString: String? = null + var contextObject: Map? = null + + @JsonCreator + protected constructor() + + constructor(string: String) : this() { + contextString = string + } + + constructor(contextObject: Map) : this() { + this.contextObject = contextObject + } + + fun isEmpty(): Boolean = contextString.isNullOrEmpty() and contextObject.isNullOrEmpty() + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as StringOrObject + + if (contextString != other.contextString) return false + if (contextObject != other.contextObject) return false + + return true + } + + override fun hashCode(): Int { + var result = contextString?.hashCode() ?: 0 + result = 31 * result + (contextObject?.hashCode() ?: 0) + return result + } + + override fun toString(): String { + return "StringOrObject(contextString=$contextString, contextObject=$contextObject)" + } + + +} + + +class StringOrObjectDeserializer : JsonDeserializer() { + override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): StringOrObject { + val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("") + return if (readTree.isValueNode) { + StringOrObject(readTree.textValue()) + } else { + + val map = p.readValueAs>(object : TypeReference>() {}) + + StringOrObject(map) + } + } + +} + +class StringORObjectSerializer : JsonSerializer() { + override fun serialize(value: StringOrObject?, gen: JsonGenerator?, serializers: SerializerProvider) { + if (value == null) { + serializers.defaultSerializeNull(gen) + return + } + if (value.contextString != null) { + gen?.writeString(value.contextString) + } else { + serializers.defaultSerializeValue(value.contextObject, gen) + } + } +}