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

Updated decoding of empty or null JSON data



s


s
parent 5d66b10c
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ interface BindingsModel { ...@@ -19,7 +19,7 @@ interface BindingsModel {
inline fun <reified T> decode(data: ByteArray?): T? { inline fun <reified T> decode(data: ByteArray?): T? {
return data?.run { return data?.run {
if (isNotEmpty() && decodeToString() != "null") { if (isValidData()) {
val typeToken = object : TypeToken<T>() {}.type val typeToken = object : TypeToken<T>() {}.type
Gson().fromJson<T>(decodeToString(), typeToken) Gson().fromJson<T>(decodeToString(), typeToken)
} else { } else {
...@@ -31,11 +31,15 @@ interface BindingsModel { ...@@ -31,11 +31,15 @@ interface BindingsModel {
inline fun <reified T> decodeArray(data: ByteArray?): List<T> { inline fun <reified T> decodeArray(data: ByteArray?): List<T> {
return data?.run { return data?.run {
if (isNotEmpty() && decodeToString() != "null") { if (isValidData()) {
val typeToken = object : TypeToken<Array<T>>() {}.type val typeToken = object : TypeToken<Array<T>>() {}.type
Gson().fromJson<Array<T>>(decodeToString(), typeToken).toList() Gson().fromJson<Array<T>>(decodeToString(), typeToken).toList()
} else listOf() } else listOf()
} ?: listOf() } ?: listOf()
} }
fun ByteArray.isValidData(): Boolean {
return isNotEmpty() && decodeToString() != "[]" && decodeToString() != "null"
}
} }
} }
\ 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