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 { open class JsonLd {
@JsonProperty("@context") @JsonProperty("@context")
@JsonDeserialize(contentUsing = StringOrObjectDeserializer::class) @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) @JsonInclude(JsonInclude.Include.NON_EMPTY)
var context: List<StringOrObject> = emptyList() var context: List<StringOrObject> = emptyList()
set(value) { set(value) {

View File

@ -54,7 +54,7 @@ class StringOrObjectDeserializer : JsonDeserializer<StringOrObject>() {
val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("") val readTree: JsonNode = p?.codec?.readTree(p) ?: return StringOrObject("")
return if (readTree.isValueNode) { return if (readTree.isValueNode) {
StringOrObject(readTree.textValue()) StringOrObject(readTree.textValue())
} else { } else if (readTree.isObject) {
val map: Map<String, String> = ctxt.readTreeAsValue<Map<String, String>>( val map: Map<String, String> = ctxt.readTreeAsValue<Map<String, String>>(
readTree, readTree,
ctxt.typeFactory.constructType(object : TypeReference<Map<String, String>>() {}) 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>>() {}) // val map = p.codec.readValue<Map<String, String>>(p,object : TypeReference<Map<String, String>>() {})
println(map) println(map)
StringOrObject(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.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper 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.hideout.core.infrastructure.httpsignature.HttpRequestMixIn
import dev.usbharu.httpsignature.common.HttpRequest import dev.usbharu.httpsignature.common.HttpRequest
import dev.usbharu.httpsignature.sign.HttpSignatureSigner import dev.usbharu.httpsignature.sign.HttpSignatureSigner
@ -39,7 +42,11 @@ class ActivityPubConfig {
@Bean @Bean
@Qualifier("activitypub") @Qualifier("activitypub")
fun objectMapper(): ObjectMapper { fun objectMapper(): ObjectMapper {
val module = SimpleModule().addSerializer(StringOrObject::class.java, StringORObjectSerializer())
val objectMapper = jacksonObjectMapper() val objectMapper = jacksonObjectMapper()
.registerModules(module)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
.setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP)) .setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.SKIP))