pull/32/head
kunfei 5 years ago
parent 61cc1aba06
commit f09168ca98
  1. 5
      app/src/main/java/io/legado/app/help/ReadBookConfig.kt
  2. 14
      app/src/main/java/io/legado/app/utils/GsonExtensions.kt

@ -6,6 +6,7 @@ import android.graphics.drawable.Drawable
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import io.legado.app.App import io.legado.app.App
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefInt
import io.legado.app.utils.putPrefInt import io.legado.app.utils.putPrefInt
import java.io.BufferedWriter import java.io.BufferedWriter
@ -23,8 +24,8 @@ object ReadBookConfig {
String(App.INSTANCE.assets.open("readConfig.json").readBytes()) String(App.INSTANCE.assets.open("readConfig.json").readBytes())
} }
try { try {
val listType = object : TypeToken<List<Config>>() {}.type // val listType = object : TypeToken<List<Config>>() {}.type
GSON.fromJson<List<Config>>(json, listType)?.let { GSON.fromJsonArray<Config>(json)?.let {
list.addAll(it) list.addAll(it)
} }
} catch (e: Exception) { } catch (e: Exception) {

@ -4,6 +4,8 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import org.jetbrains.anko.attempt import org.jetbrains.anko.attempt
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
val GSON: Gson by lazy { val GSON: Gson by lazy {
GsonBuilder() GsonBuilder()
@ -23,7 +25,15 @@ inline fun <reified T> Gson.fromJsonObject(json: String?): T? {//可转成任意
inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? { inline fun <reified T> Gson.fromJsonArray(json: String?): List<T>? {
return attempt { return attempt {
val result: List<T>? = fromJson(json, genericType<List<T>>()) val result: List<T>? = fromJson(json, ParameterizedTypeImpl(T::class.java))
result result
}.value }.value
} }
class ParameterizedTypeImpl(val clz: Class<*>) : ParameterizedType {
override fun getRawType(): Type = List::class.java
override fun getOwnerType(): Type? = null
override fun getActualTypeArguments(): Array<Type> = arrayOf(clz)
}

Loading…
Cancel
Save