From da38c839933d6e04b6a96ddbc556e30d58085cb1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 21 Jul 2022 23:11:22 +0200 Subject: [PATCH] get rid of gson --- app/build.gradle | 1 - .../libretube/preferences/PreferenceHelper.kt | 50 +++++++++++-------- .../github/libretube/update/UpdateChecker.kt | 6 +-- gradle/libs.versions.toml | 4 +- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9af9476df..3dbb1a7a7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -90,5 +90,4 @@ dependencies { implementation libs.cronet.embedded implementation libs.cronet.okhttp implementation libs.coil - implementation libs.gson } diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index dfb53f198..c1d327535 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -3,13 +3,11 @@ package com.github.libretube.preferences import android.content.Context import android.content.SharedPreferences import androidx.preference.PreferenceManager +import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.obj.CustomInstance import com.github.libretube.obj.Streams import com.github.libretube.obj.WatchHistoryItem import com.github.libretube.obj.WatchPosition -import com.google.common.reflect.TypeToken -import com.google.gson.Gson -import java.lang.reflect.Type object PreferenceHelper { private val TAG = "PreferenceHelper" @@ -92,21 +90,25 @@ object PreferenceHelper { } fun saveCustomInstance(customInstance: CustomInstance) { - val gson = Gson() + val mapper = ObjectMapper() val customInstancesList = getCustomInstances() customInstancesList += customInstance - val json = gson.toJson(customInstancesList) + val json = mapper.writeValueAsString(customInstancesList) editor.putString("customInstances", json).apply() } fun getCustomInstances(): ArrayList { - val gson = Gson() + val mapper = ObjectMapper() + val json: String = settings.getString("customInstances", "")!! - val type: Type = object : TypeToken?>() {}.type + val type = mapper.typeFactory.constructCollectionType( + List::class.java, + CustomInstance::class.java + ) return try { - gson.fromJson(json, type) + mapper.readValue(json, type) } catch (e: Exception) { arrayListOf() } @@ -127,7 +129,7 @@ object PreferenceHelper { } fun addToWatchHistory(videoId: String, streams: Streams) { - val gson = Gson() + val mapper = ObjectMapper() val watchHistoryItem = WatchHistoryItem( videoId, @@ -151,16 +153,21 @@ object PreferenceHelper { watchHistory += watchHistoryItem - val json = gson.toJson(watchHistory) + val json = mapper.writeValueAsString(watchHistory) editor.putString("watch_history", json).apply() } fun getWatchHistory(): ArrayList { - val gson = Gson() + val mapper = ObjectMapper() + val json: String = settings.getString("watch_history", "")!! - val type: Type = object : TypeToken?>() {}.type + val type = mapper.typeFactory.constructCollectionType( + List::class.java, + WatchHistoryItem::class.java + ) + return try { - gson.fromJson(json, type) + mapper.readValue(json, type) } catch (e: Exception) { arrayListOf() } @@ -179,8 +186,8 @@ object PreferenceHelper { watchPositions += watchPositionItem - val gson = Gson() - val json = gson.toJson(watchPositions) + val mapper = ObjectMapper() + val json = mapper.writeValueAsString(watchPositions) editor.putString("watch_positions", json).commit() } @@ -194,19 +201,22 @@ object PreferenceHelper { if (indexToRemove != null) watchPositions.removeAt(indexToRemove!!) - val gson = Gson() - val json = gson.toJson(watchPositions) + val mapper = ObjectMapper() + val json = mapper.writeValueAsString(watchPositions) editor.putString("watch_positions", json).commit() } fun getWatchPositions(): ArrayList { - val gson = Gson() + val mapper = ObjectMapper() val json: String = settings.getString("watch_positions", "")!! - val type: Type = object : TypeToken?>() {}.type + val type = mapper.typeFactory.constructCollectionType( + List::class.java, + WatchPosition::class.java + ) return try { - gson.fromJson(json, type) + mapper.readValue(json, type) } catch (e: Exception) { arrayListOf() } diff --git a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt b/app/src/main/java/com/github/libretube/update/UpdateChecker.kt index 9cb8c47ed..50d236385 100644 --- a/app/src/main/java/com/github/libretube/update/UpdateChecker.kt +++ b/app/src/main/java/com/github/libretube/update/UpdateChecker.kt @@ -1,7 +1,7 @@ package com.github.libretube.update +import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.GITHUB_API_URL -import com.google.gson.Gson import java.net.URL object UpdateChecker { @@ -29,7 +29,7 @@ object UpdateChecker { val json = latestVersionApiUrl.readText() // Parse and return the json data - val gson = Gson() - return gson.fromJson(json, UpdateInfo::class.java) + val mapper = ObjectMapper() + return mapper.readValue(json, UpdateInfo::class.java) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 63959de56..5fa84c2dc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -18,7 +18,6 @@ cronetEmbedded = "101.4951.41" cronetOkHttp = "0.1.0" coil = "2.1.0" leakcanary = "2.8.1" -gson = "2.9.0" [libraries] androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } @@ -43,5 +42,4 @@ exoplayer-extension-cronet = { group = "com.google.android.exoplayer", name = "e cronet-embedded = { group = "org.chromium.net", name = "cronet-embedded", version.ref = "cronetEmbedded" } cronet-okhttp = { group = "com.google.net.cronet", name = "cronet-okhttp", version.ref = "cronetOkHttp" } coil = { group = "io.coil-kt", name = "coil", version.ref="coil" } -square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" } -gson = { group = "com.google.code.gson", name="gson", version.ref = "gson"} \ No newline at end of file +square-leakcanary = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary" } \ No newline at end of file