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

Added null input support to decode function


s
parent dce9d7d4
No related branches found
No related tags found
No related merge requests found
......@@ -17,21 +17,25 @@ interface BindingsModel {
return Gson().toJson(list.toTypedArray(), typeToken).encodeToByteArray()
}
inline fun <reified T> decode(data: ByteArray): T? {
return if (data.isNotEmpty()) {
inline fun <reified T> decode(data: ByteArray?): T? {
return data?.run {
if (isNotEmpty()) {
val typeToken = object : TypeToken<T>() {}.type
Gson().fromJson(data.decodeToString(), typeToken)
Gson().fromJson<T>(decodeToString(), typeToken)
} else {
Log.d("Decode", "Failed to decode data: ${data.decodeToString()}")
Log.d("Decode", "Failed to decode data: ${decodeToString()}")
null
}
}
}
inline fun <reified T> decodeArray(data: ByteArray): List<T> {
return if (data.isNotEmpty()) {
inline fun <reified T> decodeArray(data: ByteArray?): List<T> {
return data?.run {
if (isNotEmpty()) {
val typeToken = object : TypeToken<Array<T>>() {}.type
Gson().fromJson<Array<T>>(data.decodeToString(), typeToken).toList()
Gson().fromJson<Array<T>>(decodeToString(), typeToken).toList()
} else listOf()
} ?: 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