Skip to content
Snippets Groups Projects
Commit 87f3440f authored by Kamal Bramwell's avatar Kamal Bramwell
Browse files

Check for empty data before deserializing

parent 85fd9dbf
No related branches found
No related tags found
No related merge requests found
package io.elixxir.xxclient.models
import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import io.elixxir.xxclient.utils.toBase64String
interface BindingsModel {
......@@ -16,13 +18,20 @@ interface BindingsModel {
}
inline fun <reified T> decode(data: ByteArray): T {
return if (data.isNotEmpty()) {
val typeToken = object : TypeToken<T>() {}.type
return Gson().fromJson(data.decodeToString(), typeToken)
Gson().fromJson(data.decodeToString(), typeToken)
} else {
Log.d("Decode", "Failed to decode data: ${data.decodeToString()}")
throw InvalidDataException()
}
}
inline fun <reified T> decodeArray(data: ByteArray): List<T> {
return if (data.isNotEmpty()) {
val typeToken = object : TypeToken<Array<T>>() {}.type
return Gson().fromJson<Array<T>>(data.decodeToString(), typeToken).toList()
Gson().fromJson<Array<T>>(data.decodeToString(), typeToken).toList()
} else listOf()
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment