From 1d93529aef20f7c8103f2be48660b343c6a476d0 Mon Sep 17 00:00:00 2001 From: GKF Date: Wed, 12 Jun 2019 13:43:54 +0800 Subject: [PATCH] up --- .../io/legado/app/utils/GsonExtensions.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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 7a5b315c9..f539d7738 100644 --- a/app/src/main/java/io/legado/app/utils/GsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/GsonExtensions.kt @@ -1,5 +1,34 @@ package io.legado.app.utils +import android.text.TextUtils import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.google.gson.JsonParser -inline fun Gson.fromJson(json: String) = fromJson(json, T::class.java) \ No newline at end of file +inline fun Gson.fromJson(json: String): T = fromJson(json, T::class.java) + +inline fun Gson.arrayFromJson(json: String): ArrayList? = kotlin.run { + var result: ArrayList? = null + if (!TextUtils.isEmpty(json)) { + val gson = GsonBuilder().create() + try { + val parser = JsonParser() + val jArray = parser.parse(json).asJsonArray + jArray?.let { + result = java.util.ArrayList() + for (obj in it) { + try { + val cse = gson.fromJson(obj, T::class.java) + result?.add(cse) + } catch (e: Exception) { + e.printStackTrace() + } + } + } + } catch (e: Exception) { + e.printStackTrace() + } + + } + return result +} \ No newline at end of file