diff --git a/src/main/kotlin/com/mazenrashed/moshikotlinextensions/MoshiExtensions.kt b/src/main/kotlin/com/mazenrashed/moshikotlinextensions/MoshiExtensions.kt index b71d265..f36e8b2 100644 --- a/src/main/kotlin/com/mazenrashed/moshikotlinextensions/MoshiExtensions.kt +++ b/src/main/kotlin/com/mazenrashed/moshikotlinextensions/MoshiExtensions.kt @@ -18,22 +18,22 @@ object MoshiExtensions { } @Throws(JsonDataException::class) -inline fun String.deserialize(): T? { - val jsonAdapter = MoshiExtensions.moshi.adapter(T::class.java) +inline fun String.deserialize(moshi: Moshi? = null): T? { + val jsonAdapter = (moshi ?: MoshiExtensions.moshi).adapter(T::class.java) return jsonAdapter.fromJson(this) } @Throws(JsonDataException::class) -inline fun String.deserializeList(): List? { +inline fun String.deserializeList(moshi: Moshi? = null): List? { val type = Types.newParameterizedType(MutableList::class.java, T::class.java) - val jsonAdapter: JsonAdapter> = MoshiExtensions.moshi.adapter(type) + val jsonAdapter: JsonAdapter> = (moshi ?: MoshiExtensions.moshi).adapter(type) return jsonAdapter.fromJson(this) } @Suppress("CheckResult") -fun String.canConvertTo(type: Class<*>): Boolean { +fun String.canConvertTo(type: Class<*>, moshi: Moshi? = null): Boolean { return try { - val jsonAdapter = MoshiExtensions.moshi.adapter(type) + val jsonAdapter = (moshi ?: MoshiExtensions.moshi).adapter(type) jsonAdapter.fromJson(this) true } catch (exception: Exception) { @@ -42,7 +42,7 @@ fun String.canConvertTo(type: Class<*>): Boolean { } } -inline fun T.serialize(): String { - val jsonAdapter = MoshiExtensions.moshi.adapter(T::class.java) +inline fun T.serialize(moshi: Moshi? = null): String { + val jsonAdapter = (moshi ?: MoshiExtensions.moshi).adapter(T::class.java) return jsonAdapter.toJson(this) }