Skip to content
Snippets Groups Projects
Verified Commit 40b06769 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

fix: correct gson crash

parent 42b113a5
No related branches found
No related tags found
No related merge requests found
...@@ -21,7 +21,7 @@ package de.kuschku.quasseldroid.defaults ...@@ -21,7 +21,7 @@ package de.kuschku.quasseldroid.defaults
import android.content.Context import android.content.Context
import com.google.gson.Gson import com.google.gson.Gson
import de.kuschku.quasseldroid.util.helper.fromJson import de.kuschku.quasseldroid.util.helper.fromJsonList
import java.io.IOException import java.io.IOException
import javax.inject.Inject import javax.inject.Inject
...@@ -29,7 +29,7 @@ class DefaultNetworks @Inject constructor(context: Context, gson: Gson) { ...@@ -29,7 +29,7 @@ class DefaultNetworks @Inject constructor(context: Context, gson: Gson) {
val networks: List<DefaultNetwork> by lazy { val networks: List<DefaultNetwork> by lazy {
try { try {
context.assets.open("networks.json").use { context.assets.open("networks.json").use {
gson.fromJson(it.bufferedReader(Charsets.UTF_8)) gson.fromJsonList(it.bufferedReader(Charsets.UTF_8))
} }
} catch (e: IOException) { } catch (e: IOException) {
throw IllegalStateException("networks.json missing from assets.", e) throw IllegalStateException("networks.json missing from assets.", e)
......
...@@ -23,13 +23,13 @@ import android.content.Context ...@@ -23,13 +23,13 @@ import android.content.Context
import com.google.gson.Gson import com.google.gson.Gson
import de.kuschku.quasseldroid.util.emoji.EmojiHandler import de.kuschku.quasseldroid.util.emoji.EmojiHandler
import de.kuschku.quasseldroid.util.emoji.EmojiProvider import de.kuschku.quasseldroid.util.emoji.EmojiProvider
import de.kuschku.quasseldroid.util.helper.fromJson import de.kuschku.quasseldroid.util.helper.fromJsonList
import java.io.IOException import java.io.IOException
class AndroidEmojiProvider(context: Context, gson: Gson) : EmojiProvider { class AndroidEmojiProvider(context: Context, gson: Gson) : EmojiProvider {
override val emoji: List<EmojiHandler.Emoji> = try { override val emoji: List<EmojiHandler.Emoji> = try {
context.assets.open("emoji.json").use { context.assets.open("emoji.json").use {
gson.fromJson(it.bufferedReader(Charsets.UTF_8)) gson.fromJsonList(it.bufferedReader(Charsets.UTF_8))
} }
} catch (e: IOException) { } catch (e: IOException) {
throw IllegalStateException("emoji.json missing from assets.", e) throw IllegalStateException("emoji.json missing from assets.", e)
......
...@@ -24,26 +24,20 @@ import com.google.gson.JsonElement ...@@ -24,26 +24,20 @@ import com.google.gson.JsonElement
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import java.io.Reader import java.io.Reader
inline fun <reified T> Gson.fromJsonList(jsonElement: JsonElement): T =
this.fromJson(jsonElement, object : TypeToken<T>() {}.type)
inline fun <reified T> Gson.fromJsonList(reader: Reader): T =
this.fromJson(reader, object : TypeToken<T>() {}.type)
inline fun <reified T> Gson.fromJsonList(text: String): T =
this.fromJson(text, object : TypeToken<T>() {}.type)
inline fun <reified T> Gson.fromJson(jsonElement: JsonElement): T = inline fun <reified T> Gson.fromJson(jsonElement: JsonElement): T =
if (T::class.java.typeParameters.isEmpty()) {
this.fromJson(jsonElement, T::class.java) this.fromJson(jsonElement, T::class.java)
} else {
val type = object : TypeToken<T>() {}.type
this.fromJson(jsonElement, type)
}
inline fun <reified T> Gson.fromJson(reader: Reader): T = inline fun <reified T> Gson.fromJson(reader: Reader): T =
if (T::class.java.typeParameters.isEmpty()) {
this.fromJson(reader, T::class.java) this.fromJson(reader, T::class.java)
} else {
val type = object : TypeToken<T>() {}.type
this.fromJson(reader, type)
}
inline fun <reified T> Gson.fromJson(text: String): T = inline fun <reified T> Gson.fromJson(text: String): T =
if (T::class.java.typeParameters.isEmpty()) {
this.fromJson(text, T::class.java) this.fromJson(text, T::class.java)
} else {
val type = object : TypeToken<T>() {}.type
this.fromJson(text, type)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment