feat: @contextにオブジェクトが存在していてもシリアライズ・デシリアライズできるように

This commit is contained in:
usbharu 2024-05-14 10:54:18 +09:00
parent 8f553d3ecd
commit fb029098b7
3 changed files with 11 additions and 2 deletions

View File

@ -32,7 +32,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize
open class JsonLd {
@JsonProperty("@context")
@JsonDeserialize(contentUsing = StringOrObjectDeserializer::class)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, contentUsing = StringORObjectSerializer::class)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = ContextSerializer::class)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
var context: List<StringOrObject> = emptyList()
set(value) {

View File

@ -54,7 +54,7 @@ class StringOrObjectDeserializer : JsonDeserializer<StringOrObject>() {
val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("")
return if (readTree.isValueNode) {
StringOrObject(readTree.textValue())
} else {
} else if (readTree.isObject) {
val map: Map<String, String> = ctxt.readTreeAsValue<Map<String, String>>(
readTree,
ctxt.typeFactory.constructType(object : TypeReference<Map<String, String>>() {})
@ -64,6 +64,8 @@ class StringOrObjectDeserializer : JsonDeserializer<StringOrObject>() {
// val map = p.codec.readValue<Map<String, String>>(p,object : TypeReference<Map<String, String>>() {})
println(map)
StringOrObject(map)
} else {
StringOrObject("")
}
}

View File

@ -22,7 +22,10 @@ import com.fasterxml.jackson.annotation.Nulls
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import dev.usbharu.hideout.activitypub.domain.model.StringORObjectSerializer
import dev.usbharu.hideout.activitypub.domain.model.StringOrObject
import dev.usbharu.hideout.core.infrastructure.httpsignature.HttpRequestMixIn
import dev.usbharu.httpsignature.common.HttpRequest
import dev.usbharu.httpsignature.sign.HttpSignatureSigner
@ -39,7 +42,11 @@ class ActivityPubConfig {
@Bean
@Qualifier("activitypub")
fun objectMapper(): ObjectMapper {
val module = SimpleModule().addSerializer(StringOrObject::class.java, StringORObjectSerializer())
val objectMapper = jacksonObjectMapper()
.registerModules(module)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP))