fix: カスタムシリアライザーを修正

This commit is contained in:
usbharu 2023-03-30 20:55:12 +09:00
parent 94d7564336
commit b1d341c944
4 changed files with 67 additions and 4 deletions

View File

@ -20,6 +20,10 @@ application {
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")
}
tasks.withType<Test> {
useJUnitPlatform()
}
repositories {
mavenCentral()
}
@ -54,6 +58,7 @@ dependencies {
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
}
jib {

View File

@ -13,4 +13,6 @@ open class Follow : Object{
this.`object` = `object`
this.actor = actor
}
}

View File

@ -5,12 +5,13 @@ import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.core.TreeNode
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
open class JsonLd {
@JsonProperty("@context")
@JsonDeserialize(using = ContextDeserializer::class)
@JsonDeserialize(contentUsing = ContextDeserializer::class)
var context:List<String> = emptyList()
@JsonCreator
@ -21,8 +22,12 @@ open class JsonLd {
protected constructor()
}
public class ContextDeserializer : JsonDeserializer<List<String>>() {
override fun deserialize(p0: com.fasterxml.jackson.core.JsonParser?, p1: com.fasterxml.jackson.databind.DeserializationContext?): List<String> {
return emptyList()
public class ContextDeserializer : JsonDeserializer<String>() {
override fun deserialize(p0: com.fasterxml.jackson.core.JsonParser?, p1: com.fasterxml.jackson.databind.DeserializationContext?): String {
val readTree : JsonNode = p0?.codec?.readTree(p0) ?: return ""
if (readTree.isObject) {
return ""
}
return readTree.asText()
}
}

View File

@ -0,0 +1,51 @@
package dev.usbharu.hideout.ap
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Assertions.*
class ContextDeserializerTest {
@org.junit.jupiter.api.Test
fun deserialize() {
//language=JSON
val s = """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"sensitive": "as:sensitive",
"Hashtag": "as:Hashtag",
"quoteUrl": "as:quoteUrl",
"toot": "http://joinmastodon.org/ns#",
"Emoji": "toot:Emoji",
"featured": "toot:featured",
"discoverable": "toot:discoverable",
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value",
"misskey": "https://misskey-hub.net/ns#",
"_misskey_content": "misskey:_misskey_content",
"_misskey_quote": "misskey:_misskey_quote",
"_misskey_reaction": "misskey:_misskey_reaction",
"_misskey_votes": "misskey:_misskey_votes",
"_misskey_talk": "misskey:_misskey_talk",
"isCat": "misskey:isCat",
"vcard": "http://www.w3.org/2006/vcard/ns#"
}
],
"id": "https://test-misskey-v12.usbharu.dev/follows/9bg1zu54y7/9cydqvpjcn",
"type": "Follow",
"actor": "https://test-misskey-v12.usbharu.dev/users/9bg1zu54y7",
"object": "https://test-hideout.usbharu.dev/users/test3"
}
"""
val readValue = jacksonObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false).readValue<Follow>(s)
println(readValue)
println(readValue.actor)
}
}