From f09168ca98ee6748166bc57066892b29a159716b Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 21 Aug 2019 11:18:02 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/help/ReadBookConfig.kt | 5 +++-- .../java/io/legado/app/utils/GsonExtensions.kt | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt index 266b83731..72906baf8 100644 --- a/app/src/main/java/io/legado/app/help/ReadBookConfig.kt +++ b/app/src/main/java/io/legado/app/help/ReadBookConfig.kt @@ -6,6 +6,7 @@ import android.graphics.drawable.Drawable import com.google.gson.reflect.TypeToken import io.legado.app.App import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonArray import io.legado.app.utils.getPrefInt import io.legado.app.utils.putPrefInt import java.io.BufferedWriter @@ -23,8 +24,8 @@ object ReadBookConfig { String(App.INSTANCE.assets.open("readConfig.json").readBytes()) } try { - val listType = object : TypeToken>() {}.type - GSON.fromJson>(json, listType)?.let { +// val listType = object : TypeToken>() {}.type + GSON.fromJsonArray(json)?.let { list.addAll(it) } } catch (e: Exception) { diff --git a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt index e1a099eba..1f3627873 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -4,6 +4,8 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken import org.jetbrains.anko.attempt +import java.lang.reflect.ParameterizedType +import java.lang.reflect.Type val GSON: Gson by lazy { GsonBuilder() @@ -23,7 +25,15 @@ inline fun Gson.fromJsonObject(json: String?): T? {//可转成任意 inline fun Gson.fromJsonArray(json: String?): List? { return attempt { - val result: List? = fromJson(json, genericType>()) + val result: List? = fromJson(json, ParameterizedTypeImpl(T::class.java)) result }.value -} \ No newline at end of file +} + +class ParameterizedTypeImpl(val clz: Class<*>) : ParameterizedType { + override fun getRawType(): Type = List::class.java + + override fun getOwnerType(): Type? = null + + override fun getActualTypeArguments(): Array = arrayOf(clz) +}