Fix json android compatibility

Koitharu 4 years ago
parent fe243c8acf
commit e15dbf2a4b
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -9,10 +9,7 @@ import org.koitharu.kotatsu.parsers.MangaParser
import org.koitharu.kotatsu.parsers.config.ConfigKey
import org.koitharu.kotatsu.parsers.model.*
import org.koitharu.kotatsu.parsers.util.*
import org.koitharu.kotatsu.parsers.util.json.JSONIterator
import org.koitharu.kotatsu.parsers.util.json.getStringOrNull
import org.koitharu.kotatsu.parsers.util.json.mapJSON
import org.koitharu.kotatsu.parsers.util.json.mapJSONToSet
import org.koitharu.kotatsu.parsers.util.json.*
import java.text.SimpleDateFormat
import java.util.*
@ -82,7 +79,7 @@ internal class ComickFunParser(override val context: MangaLoaderContext) : Manga
altTitle = null,
url = slug,
publicUrl = "https://$domain/comic/$slug",
rating = jo.optDouble("rating", -10.0).toFloat() / 10f,
rating = jo.getDoubleOrDefault("rating", -10.0).toFloat() / 10f,
isNsfw = false,
coverUrl = jo.getString("cover_url"),
largeCoverUrl = null,

@ -151,7 +151,7 @@ internal class MangaDexParser(override val context: MangaLoaderContext) : MangaP
}
val locale = Locale.forLanguageTag(attrs.getString("translatedLanguage"))
val relations = jo.getJSONArray("relationships").associateByKey("type")
val number = attrs.optInt("chapter", 0)
val number = attrs.getIntOrDefault("chapter", 0)
MangaChapter(
id = generateUid(id),
name = attrs.getStringOrNull("title")?.takeUnless(String::isEmpty)

@ -6,6 +6,7 @@ import org.json.JSONObject
import org.koitharu.kotatsu.utils.json.JSONIterator
import org.koitharu.kotatsu.utils.json.JSONStringIterator
import org.koitharu.kotatsu.utils.json.JSONValuesIterator
import java.util.*
import kotlin.contracts.contract
inline fun <R, C : MutableCollection<in R>> JSONArray.mapJSONTo(
@ -56,13 +57,45 @@ fun JSONObject.getStringOrNull(name: String): String? = opt(name)?.takeUnless {
it.isEmpty()
}
fun JSONObject.getBooleanOrDefault(name: String, defaultValue: Boolean): Boolean = opt(name)?.takeUnless {
it === JSONObject.NULL
} as? Boolean ?: defaultValue
fun JSONObject.getBooleanOrDefault(name: String, defaultValue: Boolean): Boolean {
return when (val rawValue = opt(name)) {
null, JSONObject.NULL -> defaultValue
is Boolean -> rawValue
is Number -> rawValue.toInt() != 0
is String -> rawValue.lowercase(Locale.ROOT).toBooleanStrictOrNull() ?: defaultValue
else -> defaultValue
}
}
fun JSONObject.getLongOrDefault(name: String, defaultValue: Long): Long = opt(name)?.takeUnless {
it === JSONObject.NULL
} as? Long ?: defaultValue
fun JSONObject.getLongOrDefault(name: String, defaultValue: Long): Long {
return when (val rawValue = opt(name)) {
null, JSONObject.NULL -> defaultValue
is Long -> rawValue
is Number -> rawValue.toLong()
is String -> rawValue.toLongOrNull() ?: defaultValue
else -> defaultValue
}
}
fun JSONObject.getIntOrDefault(name: String, defaultValue: Int): Int {
return when (val rawValue = opt(name)) {
null, JSONObject.NULL -> defaultValue
is Int -> rawValue
is Number -> rawValue.toInt()
is String -> rawValue.toIntOrNull() ?: defaultValue
else -> defaultValue
}
}
fun JSONObject.getDoubleOrDefault(name: String, defaultValue: Double): Double {
return when (val rawValue = opt(name)) {
null, JSONObject.NULL -> defaultValue
is Double -> rawValue
is Number -> rawValue.toDouble()
is String -> rawValue.toDoubleOrNull() ?: defaultValue
else -> defaultValue
}
}
fun JSONArray.JSONIterator(): Iterator<JSONObject> = JSONIterator(this)

Loading…
Cancel
Save