Skip to content

Commit ad61b2e

Browse files
committed
fix: mergeJSON
1 parent a65f56f commit ad61b2e

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

Diff for: app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ fun buildConfig(
440440

441441
// custom JSON merge
442442
if (bean.customOutboundJson.isNotBlank()) {
443-
Util.mergeJSON(bean.customOutboundJson, currentOutbound)
443+
Util.mergeJSON(
444+
bean.customOutboundJson,
445+
currentOutbound as MutableMap<String, Any?>
446+
)
444447
}
445448
}
446449

Diff for: app/src/main/java/moe/matsuri/nb4a/utils/Util.kt

+27-14
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,46 @@ object Util {
113113
}
114114
}
115115

116+
fun map2StringMap(m: Map<*, *>): MutableMap<String, Any?> {
117+
val o = mutableMapOf<String, Any?>()
118+
m.forEach {
119+
if (it.key is String) {
120+
o[it.key as String] = it.value as Any
121+
}
122+
}
123+
return o
124+
}
116125

117-
fun mergeJSON(j: String, to: MutableMap<String, Any>) {
118-
if (j.isBlank()) return
119-
val m = JavaUtil.gson.fromJson(j, to.javaClass)
120-
m.forEach { (k, v) ->
121-
if (v is Map<*, *> && to[k] is Map<*, *>) {
122-
val currentMap = (to[k] as Map<*, *>).toMutableMap()
123-
currentMap += v
124-
to[k] = currentMap
126+
fun mergeMap(dst: MutableMap<String, Any?>, src: Map<String, Any?>): MutableMap<String, Any?> {
127+
src.forEach { (k, v) ->
128+
if (v is Map<*, *> && dst[k] is Map<*, *>) {
129+
val currentMap = (dst[k] as Map<*, *>).toMutableMap()
130+
dst[k] = mergeMap(map2StringMap(currentMap), map2StringMap(v))
125131
} else if (v is List<*>) {
126132
if (k.startsWith("+")) { // prepend
127133
val dstKey = k.removePrefix("+")
128-
var currentList = (to[dstKey] as List<*>).toMutableList()
134+
var currentList = (dst[dstKey] as List<*>).toMutableList()
129135
currentList = (v + currentList).toMutableList()
130-
to[dstKey] = currentList
136+
dst[dstKey] = currentList
131137
} else if (k.endsWith("+")) { // append
132138
val dstKey = k.removeSuffix("+")
133-
var currentList = (to[dstKey] as List<*>).toMutableList()
139+
var currentList = (dst[dstKey] as List<*>).toMutableList()
134140
currentList = (currentList + v).toMutableList()
135-
to[dstKey] = currentList
141+
dst[dstKey] = currentList
136142
} else {
137-
to[k] = v
143+
dst[k] = v
138144
}
139145
} else {
140-
to[k] = v
146+
dst[k] = v
141147
}
142148
}
149+
return dst
150+
}
151+
152+
fun mergeJSON(j: String, dst: MutableMap<String, Any?>) {
153+
if (j.isBlank()) return
154+
val src = JavaUtil.gson.fromJson(j, dst.javaClass)
155+
mergeMap(dst, src)
143156
}
144157

145158
// Format Time

0 commit comments

Comments
 (0)