This commit is contained in:
Horis
2025-02-12 12:47:59 +08:00
parent 1ea9952f60
commit 8e66c7ed66
2 changed files with 21 additions and 10 deletions
@@ -64,8 +64,8 @@ suspend fun BookSource.exploreKinds(): List<ExploreKind> {
}
}
if (ruleStr.isJsonArray()) {
GSON.fromJsonArray<ExploreKind?>(ruleStr).getOrThrow().let {
kinds.addAll(it.filterNotNull())
GSON.fromJsonArray<ExploreKind>(ruleStr).getOrThrow().let {
kinds.addAll(it)
}
} else {
ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr ->
@@ -65,10 +65,13 @@ inline fun <reified T> Gson.fromJsonArray(json: String?): Result<List<T>> {
if (json == null) {
throw JsonSyntaxException("解析字符串为空")
}
fromJson(
json,
TypeToken.getParameterized(List::class.java, T::class.java).type
) as List<T>
val type = TypeToken.getParameterized(List::class.java, T::class.java).type
val list = fromJson(json, type) as List<T?>
if (list.contains(null)) {
throw JsonSyntaxException("存在null")
}
@Suppress("UNCHECKED_CAST")
list as List<T>
}
}
@@ -88,10 +91,13 @@ inline fun <reified T> Gson.fromJsonArray(inputStream: InputStream?): Result<Lis
throw JsonSyntaxException("解析流为空")
}
val reader = InputStreamReader(inputStream)
fromJson(
reader,
TypeToken.getParameterized(List::class.java, T::class.java).type
) as List<T>
val type = TypeToken.getParameterized(List::class.java, T::class.java).type
val list = fromJson(reader, type) as List<T?>
if (list.contains(null)) {
throw JsonSyntaxException("存在null")
}
@Suppress("UNCHECKED_CAST")
list as List<T>
}
}
@@ -150,6 +156,7 @@ class IntJsonDeserializer : JsonDeserializer<Int?> {
null
}
}
else -> null
}
}
@@ -182,6 +189,7 @@ class MapDeserializerDoubleAsIntFix :
}
return list
}
json.isJsonObject -> {
val map: MutableMap<String, Any?> =
LinkedTreeMap()
@@ -193,15 +201,18 @@ class MapDeserializerDoubleAsIntFix :
}
return map
}
json.isJsonPrimitive -> {
val prim = json.asJsonPrimitive
when {
prim.isBoolean -> {
return prim.asBoolean
}
prim.isString -> {
return prim.asString
}
prim.isNumber -> {
val num: Number = prim.asNumber
// here you can handle double int/long values