This commit is contained in:
usbharu 2024-05-14 09:32:35 +09:00
parent 0baabb9fd0
commit ae43b5e793
2 changed files with 91 additions and 8 deletions

View File

@ -34,15 +34,15 @@ open class JsonLd {
@JsonDeserialize(contentUsing = ContextDeserializer::class) @JsonDeserialize(contentUsing = ContextDeserializer::class)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class) @JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class)
@JsonInclude(JsonInclude.Include.NON_EMPTY) @JsonInclude(JsonInclude.Include.NON_EMPTY)
var context: List<String> = emptyList() var context: List<StringOrObject> = emptyList()
set(value) { set(value) {
field = value.filterNotNull().filter { it.isNotBlank() } field = value.filter { it.isEmpty() }
} }
@JsonCreator @JsonCreator
constructor(context: List<String?>?) { constructor(context: List<StringOrObject?>?) {
if (context != null) { if (context != null) {
this.context = context.filterNotNull().filter { it.isNotBlank() } this.context = context.filterNotNull().filter { it.isEmpty() }
} else { } else {
this.context = emptyList() this.context = emptyList()
} }
@ -76,14 +76,14 @@ class ContextDeserializer : JsonDeserializer<String>() {
} }
} }
class ContextSerializer : JsonSerializer<List<String>>() { class ContextSerializer : JsonSerializer<List<StringOrObject>>() {
@Deprecated("Deprecated in Java") @Deprecated("Deprecated in Java")
override fun isEmpty(value: List<String>?): Boolean = value.isNullOrEmpty() override fun isEmpty(value: List<StringOrObject>?): Boolean = value.isNullOrEmpty()
override fun isEmpty(provider: SerializerProvider?, value: List<String>?): Boolean = value.isNullOrEmpty() override fun isEmpty(provider: SerializerProvider?, value: List<StringOrObject>?): Boolean = value.isNullOrEmpty()
override fun serialize(value: List<String>?, gen: JsonGenerator?, serializers: SerializerProvider) { override fun serialize(value: List<StringOrObject>?, gen: JsonGenerator?, serializers: SerializerProvider) {
if (value.isNullOrEmpty()) { if (value.isNullOrEmpty()) {
serializers.defaultSerializeNull(gen) serializers.defaultSerializeNull(gen)
return return
@ -98,4 +98,8 @@ class ContextSerializer : JsonSerializer<List<String>>() {
gen?.writeEndArray() gen?.writeEndArray()
} }
} }
override fun serialize(value: List<StringOrObject>?, gen: JsonGenerator?, serializers: SerializerProvider?) {
}
} }

View File

@ -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<String, String>? = null
@JsonCreator
protected constructor()
constructor(string: String) : this() {
contextString = string
}
constructor(contextObject: Map<String, String>) : 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<StringOrObject>() {
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<Map<String, String>>(object : TypeReference<Map<String, String>>() {})
StringOrObject(map)
}
}
}
class StringORObjectSerializer : JsonSerializer<StringOrObject>() {
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)
}
}
}