Skip to content

Commit cf20de6

Browse files
committed
throw JsonDataException when deserialize fail
1 parent 40f3401 commit cf20de6

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/main/kotlin/com/mazenrashed/moshikotlinextensions/MoshiExtensions.kt

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.mazenrashed.moshikotlinextensions
22

33
import com.squareup.moshi.JsonAdapter
4+
import com.squareup.moshi.JsonDataException
45
import com.squareup.moshi.Moshi
56
import com.squareup.moshi.Types
67
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
8+
import kotlin.jvm.Throws
79

810
object MoshiExtensions {
911
var moshi: Moshi = Moshi.Builder()
@@ -15,11 +17,13 @@ object MoshiExtensions {
1517
}
1618
}
1719

20+
@Throws(JsonDataException::class)
1821
inline fun <reified T> String.deserialize(): T? {
1922
val jsonAdapter = MoshiExtensions.moshi.adapter(T::class.java)
2023
return jsonAdapter.fromJson(this)
2124
}
2225

26+
@Throws(JsonDataException::class)
2327
inline fun <reified T> String.deserializeList(): List<T>? {
2428
val type = Types.newParameterizedType(MutableList::class.java, T::class.java)
2529
val jsonAdapter: JsonAdapter<List<T>> = MoshiExtensions.moshi.adapter(type)

0 commit comments

Comments
 (0)