From ad61b2ee66b86986c765b1be7afd24017b19230c Mon Sep 17 00:00:00 2001 From: armv9 <48624112+arm64v8a@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:17:14 +0900 Subject: [PATCH] fix: mergeJSON --- .../nekohasekai/sagernet/fmt/ConfigBuilder.kt | 5 ++- .../main/java/moe/matsuri/nb4a/utils/Util.kt | 41 ++++++++++++------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt b/app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt index 896b4f9e..c4b59fde 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt @@ -440,7 +440,10 @@ fun buildConfig( // custom JSON merge if (bean.customOutboundJson.isNotBlank()) { - Util.mergeJSON(bean.customOutboundJson, currentOutbound) + Util.mergeJSON( + bean.customOutboundJson, + currentOutbound as MutableMap + ) } } diff --git a/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt b/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt index ef695f7c..77bb1901 100644 --- a/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt +++ b/app/src/main/java/moe/matsuri/nb4a/utils/Util.kt @@ -113,33 +113,46 @@ object Util { } } + fun map2StringMap(m: Map<*, *>): MutableMap { + val o = mutableMapOf() + m.forEach { + if (it.key is String) { + o[it.key as String] = it.value as Any + } + } + return o + } - fun mergeJSON(j: String, to: MutableMap) { - if (j.isBlank()) return - val m = JavaUtil.gson.fromJson(j, to.javaClass) - m.forEach { (k, v) -> - if (v is Map<*, *> && to[k] is Map<*, *>) { - val currentMap = (to[k] as Map<*, *>).toMutableMap() - currentMap += v - to[k] = currentMap + fun mergeMap(dst: MutableMap, src: Map): MutableMap { + src.forEach { (k, v) -> + if (v is Map<*, *> && dst[k] is Map<*, *>) { + val currentMap = (dst[k] as Map<*, *>).toMutableMap() + dst[k] = mergeMap(map2StringMap(currentMap), map2StringMap(v)) } else if (v is List<*>) { if (k.startsWith("+")) { // prepend val dstKey = k.removePrefix("+") - var currentList = (to[dstKey] as List<*>).toMutableList() + var currentList = (dst[dstKey] as List<*>).toMutableList() currentList = (v + currentList).toMutableList() - to[dstKey] = currentList + dst[dstKey] = currentList } else if (k.endsWith("+")) { // append val dstKey = k.removeSuffix("+") - var currentList = (to[dstKey] as List<*>).toMutableList() + var currentList = (dst[dstKey] as List<*>).toMutableList() currentList = (currentList + v).toMutableList() - to[dstKey] = currentList + dst[dstKey] = currentList } else { - to[k] = v + dst[k] = v } } else { - to[k] = v + dst[k] = v } } + return dst + } + + fun mergeJSON(j: String, dst: MutableMap) { + if (j.isBlank()) return + val src = JavaUtil.gson.fromJson(j, dst.javaClass) + mergeMap(dst, src) } // Format Time