Skip to content

Commit

Permalink
Use ContentNegotiation as json serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Oct 7, 2023
1 parent b92823a commit e598534
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@

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
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


Expand All @@ -31,6 +34,7 @@ fun Application.httpModule(adapter: HttpAdapter) {
}
}

install(ContentNegotiation) { json(json = BuiltinJsonSerializer.buildJson()) }
install(GlobalExceptionHandler)
install(Authorization)
if (MahContextHolder.debug) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand All @@ -36,22 +33,27 @@ internal class BuiltinJsonSerializer : InternalSerializer {
polymorphicSealedClass(EventDTO::class, BotEventDTO::class)
}
}

/**
* 从 sealed class 里注册到多态序列化
*/
@InternalSerializationApi
@Suppress("UNCHECKED_CAST")
private fun <B : Any, S : B> SerializersModuleBuilder.polymorphicSealedClass(
baseClass: KClass<B>,
sealedClass: KClass<S>
) {
sealedClass.sealedSubclasses.forEach {
val c = it as KClass<S>
polymorphic(baseClass, c, c.serializer())
}
}
}

/**
* 从 sealed class 里注册到多态序列化
* Json解析规则,需要注册支持的多态的类
*/
@InternalSerializationApi
@Suppress("UNCHECKED_CAST")
private fun <B : Any, S : B> SerializersModuleBuilder.polymorphicSealedClass(
baseClass: KClass<B>,
sealedClass: KClass<S>
) {
sealedClass.sealedSubclasses.forEach {
val c = it as KClass<S>
polymorphic(baseClass, c, c.serializer())
}
}
private val json by lazy { buildJson() }

override fun <T : Any> encode(dto: T, clazz: KClass<T>): String = when (dto) {
is StateCode -> json.encodeToString(StateCode.serializer(), dto)
Expand Down
12 changes: 11 additions & 1 deletion mirai-api-http/src/test/kotlin/framework/TestMahApplication.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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
Expand Down Expand Up @@ -70,7 +80,7 @@ class MahApplicationTestBuilder(private val builder: ApplicationTestBuilder): Cl
contentConverter = KotlinxWebsocketSerializationConverter(Json)
}
install(ContentNegotiation) {
json(json = buildPolyJson())
json(json = BuiltinJsonSerializer.buildJson())
}
}}

Expand Down
34 changes: 0 additions & 34 deletions mirai-api-http/src/test/kotlin/framework/json.kt

This file was deleted.

0 comments on commit e598534

Please sign in to comment.