mirror of https://github.com/usbharu/Hideout.git
feat: typeなどの要素が一つのとき配列ではなく文字列で返すように
This commit is contained in:
parent
25bc5e9761
commit
7d716a4d35
|
@ -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<String> = emptyList()
|
||||
|
||||
@JsonCreator
|
||||
|
@ -31,3 +36,22 @@ public class ContextDeserializer : JsonDeserializer<String>() {
|
|||
return readTree.asText()
|
||||
}
|
||||
}
|
||||
|
||||
public class ContextSerializer : JsonSerializer<List<String>>() {
|
||||
override fun serialize(value: List<String>?, 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()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> = emptyList()
|
||||
var name: String? = null
|
||||
|
||||
|
@ -19,3 +25,19 @@ open class Object : JsonLd {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeSerializer : JsonSerializer<List<String>>() {
|
||||
override fun serialize(value: List<String>?, gen: JsonGenerator?, serializers: SerializerProvider?) {
|
||||
println(value)
|
||||
if (value?.size == 1) {
|
||||
gen?.writeString(value[0])
|
||||
} else {
|
||||
gen?.writeStartArray()
|
||||
value?.forEach {
|
||||
gen?.writeString(it)
|
||||
}
|
||||
gen?.writeEndArray()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue