From fbb8a68e0d343e2c8f346e902f5658ba14e2ad80 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 22 Jul 2022 00:37:30 +0800 Subject: [PATCH 1/2] Remove fastjson --- gradle/libs.versions.toml | 1 - obsolete/build.gradle.kts | 1 - .../FastJsonConverterFactory.kt | 32 ----------------- .../demoapp/obsolete/util/JsonConverter.kt | 35 ------------------- 4 files changed, 69 deletions(-) delete mode 100644 obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/network/convertfactory/FastJsonConverterFactory.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5a31119bb..0551aff39 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -88,7 +88,6 @@ chucker-release = { module = "com.github.chuckerteam.chucker:library-no-op", ver coil = { module = "io.coil-kt:coil", version.ref = "coil" } coil-gif = { module = "io.coil-kt:coil-gif", version.ref = "coil" } coil-svg = { module = "io.coil-kt:coil-svg", version.ref = "coil" } -fastjson = "com.alibaba:fastjson:1.1.72.android" flycoTabLayout = "com.flyco.tablayout:FlycoTabLayout_Lib:2.2.0" glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" } glide-okhttp3 = { module = "com.github.bumptech.glide:okhttp3-integration", version.ref = "glide" } diff --git a/obsolete/build.gradle.kts b/obsolete/build.gradle.kts index 46e5108db..313eed370 100644 --- a/obsolete/build.gradle.kts +++ b/obsolete/build.gradle.kts @@ -10,7 +10,6 @@ dependencies { implementation(libs.square.okHttp) implementation(libs.square.retrofit.gson) implementation(libs.square.okHttp.logInterceptor) - implementation(libs.fastjson) implementation(libs.google.gson) implementation(libs.bundles.glide) implementation(libs.rxJava3.java) diff --git a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/network/convertfactory/FastJsonConverterFactory.kt b/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/network/convertfactory/FastJsonConverterFactory.kt deleted file mode 100644 index caccebd8a..000000000 --- a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/network/convertfactory/FastJsonConverterFactory.kt +++ /dev/null @@ -1,32 +0,0 @@ -package io.goooler.demoapp.obsolete.network.convertfactory - -import com.alibaba.fastjson.JSON -import com.alibaba.fastjson.serializer.SerializeConfig -import java.lang.reflect.Type -import okhttp3.MediaType.Companion.toMediaType -import okhttp3.RequestBody -import okhttp3.RequestBody.Companion.toRequestBody -import okhttp3.ResponseBody -import retrofit2.Converter -import retrofit2.Retrofit - -object FastJsonConverterFactory : Converter.Factory() { - - override fun requestBodyConverter( - type: Type, - parameterAnnotations: Array, - methodAnnotations: Array, - retrofit: Retrofit - ): Converter = Converter { - JSON.toJSONBytes(it, SerializeConfig.getGlobalInstance()) - .toRequestBody("application/json; charset=UTF-8".toMediaType()) - } - - override fun responseBodyConverter( - type: Type, - annotations: Array, - retrofit: Retrofit - ): Converter = Converter { - JSON.parseObject(it.string(), type) - } -} diff --git a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt b/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt index 45105730f..84f1e6217 100644 --- a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt +++ b/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt @@ -2,8 +2,6 @@ package io.goooler.demoapp.obsolete.util -import com.alibaba.fastjson.JSONObject -import com.alibaba.fastjson.TypeReference import com.google.gson.Gson import java.lang.reflect.Type @@ -43,36 +41,3 @@ interface GsonUtil : JsonConverter { } } } - -interface FastJsonUtil : JsonConverter { - - fun FastJsonUtil.Companion.fromJson(json: String, typeRef: TypeReference): T? = try { - JSONObject.parseObject(json, typeRef) - } catch (e: Exception) { - e.printStackTrace() - null - } - - companion object : FastJsonUtil { - override fun fromJson(json: String, classOfT: Class): T? = try { - JSONObject.parseObject(json, classOfT) - } catch (e: Exception) { - e.printStackTrace() - null - } - - override fun fromJson(json: String, typeOfT: Type): T? = try { - JSONObject.parseObject(json, typeOfT) - } catch (e: Exception) { - e.printStackTrace() - null - } - - override fun toJson(src: Any): String? = try { - JSONObject.toJSONString(src) - } catch (e: Exception) { - e.printStackTrace() - null - } - } -} From a9911ff4a682bb4f1214490398b0bed4b696cf05 Mon Sep 17 00:00:00 2001 From: Goooler Date: Fri, 22 Jul 2022 00:42:06 +0800 Subject: [PATCH 2/2] Cleanup GsonUtil --- .../demoapp/obsolete/util/JsonConverter.kt | 40 ++++--------------- 1 file changed, 8 insertions(+), 32 deletions(-) diff --git a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt b/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt index 84f1e6217..ead8a37b9 100644 --- a/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt +++ b/obsolete/src/main/kotlin/io/goooler/demoapp/obsolete/util/JsonConverter.kt @@ -5,39 +5,15 @@ package io.goooler.demoapp.obsolete.util import com.google.gson.Gson import java.lang.reflect.Type -sealed interface JsonConverter { +object GsonUtil { + @PublishedApi + internal val gson = Gson() - fun fromJson(json: String, classOfT: Class): T? + inline fun fromJson(json: String, classOfT: Class): T? = + runCatching { gson.fromJson(json, classOfT) }.getOrNull() - fun fromJson(json: String, typeOfT: Type): T? + inline fun fromJson(json: String, typeOfT: Type): T? = + runCatching { gson.fromJson(json, typeOfT) }.getOrNull() - fun toJson(src: Any): String? -} - -interface GsonUtil : JsonConverter { - - companion object : GsonUtil { - private val gson = Gson() - - override fun fromJson(json: String, classOfT: Class): T? = try { - gson.fromJson(json, classOfT) - } catch (e: Exception) { - e.printStackTrace() - null - } - - override fun fromJson(json: String, typeOfT: Type): T? = try { - gson.fromJson(json, typeOfT) - } catch (e: Exception) { - e.printStackTrace() - null - } - - override fun toJson(src: Any): String? = try { - gson.toJson(src) - } catch (e: Exception) { - e.printStackTrace() - null - } - } + fun toJson(src: Any): String? = runCatching { gson.toJson(src) }.getOrNull() }