From 084280564af58d1af22db5b57c67577d93bd820e Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 24 Jun 2022 19:36:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E6=B7=BB=E5=8A=A0=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E5=BA=8F=E5=88=97=E5=8C=96=E5=99=A8=E6=9D=A5=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=9B=A0=20Gson=20=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于 Gson 的解析方式不能正确处理 Kotlin 的 null-safety 属性, 因此添加两个 Serializer, 手动解析 Json 以避开这个问题. Close #9 --- .../src/main/kotlin/serializer/Serializer.kt | 85 ++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt index d28e42e..1341272 100644 --- a/scalabot-meta/src/main/kotlin/serializer/Serializer.kt +++ b/scalabot-meta/src/main/kotlin/serializer/Serializer.kt @@ -1,15 +1,15 @@ package net.lamgc.scalabot.config.serializer import com.google.gson.* -import net.lamgc.scalabot.config.MavenRepositoryConfig -import net.lamgc.scalabot.config.ProxyType -import net.lamgc.scalabot.config.UsernameAuthenticator +import com.google.gson.reflect.TypeToken +import net.lamgc.scalabot.config.* import org.eclipse.aether.artifact.AbstractArtifact import org.eclipse.aether.artifact.Artifact import org.eclipse.aether.artifact.DefaultArtifact import org.eclipse.aether.repository.Authentication import org.eclipse.aether.repository.Proxy import org.eclipse.aether.util.repository.AuthenticationBuilder +import org.telegram.telegrambots.meta.ApiConstants import java.lang.reflect.Type import java.net.MalformedURLException import java.net.URL @@ -180,3 +180,82 @@ object UsernameAuthenticatorSerializer : JsonSerializer, } } + +object ProxyConfigSerializer : JsonSerializer, JsonDeserializer { + override fun serialize(src: ProxyConfig?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement { + if (src == null) { + return JsonNull.INSTANCE + } + return JsonObject().apply { + addProperty("type", src.type.name) + addProperty("host", src.host) + addProperty("port", src.port) + } + } + + override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): ProxyConfig { + if (json == null || json.isJsonNull) { + return ProxyConfig() + } else if (json !is JsonObject) { + throw JsonParseException("Invalid json type.") + } + + val typeStr = json["type"]?.asString ?: return ProxyConfig() + val type = try { + ProxyType.valueOf(typeStr) + } catch (e: IllegalArgumentException) { + throw JsonParseException("Invalid proxy type: `$typeStr`") + } + + if (!json.has("host") || !json.has("port")) { + throw JsonParseException("Missing `host` field or `port` field.") + } + + return ProxyConfig( + type = type, + host = json["host"].asString, + port = json["port"].asInt + ) + } + +} + +object BotConfigSerializer : JsonSerializer, JsonDeserializer { + + override fun serialize(src: BotConfig, typeOfSrc: Type, context: JsonSerializationContext): JsonElement { + return JsonObject().apply { + addProperty("enabled", src.enabled) + add("account", context.serialize(src.account)) + addProperty("disableBuiltInAbility", src.disableBuiltInAbility) + addProperty("autoUpdateCommandList", src.autoUpdateCommandList) + add("extensions", context.serialize(src.extensions)) + add("proxy", ProxyConfigSerializer.serialize(src.proxy, ProxyConfig::class.java, context)) + addProperty("baseApiUrl", src.baseApiUrl) + } + } + + override fun deserialize(json: JsonElement, typeOfT: Type, context: JsonDeserializationContext): BotConfig { + if (json !is JsonObject) { + throw JsonParseException("Unsupported JSON type.") + } + + if (!json.has("account")) { + throw JsonParseException("Missing `account` field.") + } else if (!json.get("account").isJsonObject) { + throw JsonParseException("Invalid `account` field type.") + } + + // 从 json 反序列化 BotConfig(使用构造函数) + return BotConfig( + enabled = json.get("enabled")?.asBoolean ?: true, + account = context.deserialize(json.get("account"), BotAccount::class.java)!!, + disableBuiltInAbility = json.get("disableBuiltInAbility")?.asBoolean ?: false, + autoUpdateCommandList = json.get("autoUpdateCommandList")?.asBoolean ?: false, + extensions = context.deserialize(json.get("extensions"), object : TypeToken>() {}.type) + ?: emptySet(), + proxy = context.deserialize(json.get("proxy"), ProxyConfig::class.java) ?: ProxyConfig(), + baseApiUrl = json.get("baseApiUrl")?.asString ?: ApiConstants.BASE_URL + ) + } +} +