From e5985345cea0ac607847e263af3c758ad0c182de Mon Sep 17 00:00:00 2001 From: ryoii Date: Sun, 8 Oct 2023 01:30:14 +0800 Subject: [PATCH] Use ContentNegotiation as json serializer --- .../api/http/adapter/http/router/base.kt | 4 ++ .../internal/serializer/BuiltinJson.kt | 38 ++++++++++--------- .../kotlin/framework/TestMahApplication.kt | 12 +++++- .../src/test/kotlin/framework/json.kt | 34 ----------------- 4 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 mirai-api-http/src/test/kotlin/framework/json.kt diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt index 56a5f2bd..65321c19 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/base.kt @@ -9,7 +9,9 @@ package net.mamoe.mirai.api.http.adapter.http.router +import io.ktor.serialization.kotlinx.json.* import io.ktor.server.application.* +import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.plugins.cors.routing.* import io.ktor.server.plugins.defaultheaders.* import net.mamoe.mirai.api.http.adapter.http.HttpAdapter @@ -17,6 +19,7 @@ import net.mamoe.mirai.api.http.adapter.http.plugin.Authorization import net.mamoe.mirai.api.http.adapter.http.plugin.DoubleReceive import net.mamoe.mirai.api.http.adapter.http.plugin.GlobalExceptionHandler import net.mamoe.mirai.api.http.adapter.http.plugin.HttpRouterMonitor +import net.mamoe.mirai.api.http.adapter.internal.serializer.BuiltinJsonSerializer import net.mamoe.mirai.api.http.context.MahContextHolder @@ -31,6 +34,7 @@ fun Application.httpModule(adapter: HttpAdapter) { } } + install(ContentNegotiation) { json(json = BuiltinJsonSerializer.buildJson()) } install(GlobalExceptionHandler) install(Authorization) if (MahContextHolder.debug) { diff --git a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/serializer/BuiltinJson.kt b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/serializer/BuiltinJson.kt index 1699c75b..6724af0a 100644 --- a/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/serializer/BuiltinJson.kt +++ b/mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/serializer/BuiltinJson.kt @@ -1,5 +1,5 @@ /* - * Copyright 2020 Mamoe Technologies and contributors. + * Copyright 2023 Mamoe Technologies and contributors. * * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. @@ -22,11 +22,8 @@ import kotlin.reflect.KClass @OptIn(InternalSerializationApi::class) internal class BuiltinJsonSerializer : InternalSerializer { - /** - * Json解析规则,需要注册支持的多态的类 - */ - private val json by lazy { - Json { + companion object { + fun buildJson() = Json { encodeDefaults = true isLenient = true ignoreUnknownKeys = true @@ -36,22 +33,27 @@ internal class BuiltinJsonSerializer : InternalSerializer { polymorphicSealedClass(EventDTO::class, BotEventDTO::class) } } + + /** + * 从 sealed class 里注册到多态序列化 + */ + @InternalSerializationApi + @Suppress("UNCHECKED_CAST") + private fun SerializersModuleBuilder.polymorphicSealedClass( + baseClass: KClass, + sealedClass: KClass + ) { + sealedClass.sealedSubclasses.forEach { + val c = it as KClass + polymorphic(baseClass, c, c.serializer()) + } + } } /** - * 从 sealed class 里注册到多态序列化 + * Json解析规则,需要注册支持的多态的类 */ - @InternalSerializationApi - @Suppress("UNCHECKED_CAST") - private fun SerializersModuleBuilder.polymorphicSealedClass( - baseClass: KClass, - sealedClass: KClass - ) { - sealedClass.sealedSubclasses.forEach { - val c = it as KClass - polymorphic(baseClass, c, c.serializer()) - } - } + private val json by lazy { buildJson() } override fun encode(dto: T, clazz: KClass): String = when (dto) { is StateCode -> json.encodeToString(StateCode.serializer(), dto) diff --git a/mirai-api-http/src/test/kotlin/framework/TestMahApplication.kt b/mirai-api-http/src/test/kotlin/framework/TestMahApplication.kt index b37656bb..2775729a 100644 --- a/mirai-api-http/src/test/kotlin/framework/TestMahApplication.kt +++ b/mirai-api-http/src/test/kotlin/framework/TestMahApplication.kt @@ -1,3 +1,12 @@ +/* + * Copyright 2023 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + package framework import io.ktor.client.* @@ -19,6 +28,7 @@ import kotlinx.serialization.json.Json import net.mamoe.mirai.api.http.adapter.MahAdapter import net.mamoe.mirai.api.http.adapter.MahAdapterFactory import net.mamoe.mirai.api.http.adapter.http.router.httpModule +import net.mamoe.mirai.api.http.adapter.internal.serializer.BuiltinJsonSerializer import net.mamoe.mirai.api.http.adapter.webhook.WebhookAdapter import net.mamoe.mirai.api.http.adapter.ws.router.websocketRouteModule import net.mamoe.mirai.api.http.context.MahContextHolder @@ -70,7 +80,7 @@ class MahApplicationTestBuilder(private val builder: ApplicationTestBuilder): Cl contentConverter = KotlinxWebsocketSerializationConverter(Json) } install(ContentNegotiation) { - json(json = buildPolyJson()) + json(json = BuiltinJsonSerializer.buildJson()) } }} diff --git a/mirai-api-http/src/test/kotlin/framework/json.kt b/mirai-api-http/src/test/kotlin/framework/json.kt deleted file mode 100644 index cb8f43f6..00000000 --- a/mirai-api-http/src/test/kotlin/framework/json.kt +++ /dev/null @@ -1,34 +0,0 @@ -package framework - -import kotlinx.serialization.InternalSerializationApi -import kotlinx.serialization.json.Json -import kotlinx.serialization.modules.SerializersModule -import kotlinx.serialization.modules.SerializersModuleBuilder -import kotlinx.serialization.serializer -import net.mamoe.mirai.api.http.adapter.internal.dto.BotEventDTO -import net.mamoe.mirai.api.http.adapter.internal.dto.EventDTO -import net.mamoe.mirai.api.http.adapter.internal.dto.MessagePacketDTO -import kotlin.reflect.KClass - - -fun buildPolyJson() = Json { - serializersModule = SerializersModule { - polymorphicSealedClass(EventDTO::class, MessagePacketDTO::class) - polymorphicSealedClass(EventDTO::class, BotEventDTO::class) - } -} - -/** - * 从 sealed class 里注册到多态序列化 - */ -@OptIn(InternalSerializationApi::class) -@Suppress("UNCHECKED_CAST") -private fun SerializersModuleBuilder.polymorphicSealedClass( - baseClass: KClass, - sealedClass: KClass -) { - sealedClass.sealedSubclasses.forEach { - val c = it as KClass - polymorphic(baseClass, c, c.serializer()) - } -} \ No newline at end of file