Skip to content

Commit

Permalink
allow user to pass his own moshi object
Browse files Browse the repository at this point in the history
  • Loading branch information
mazenrashed committed Jan 23, 2023
1 parent cf20de6 commit 25a310e
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ object MoshiExtensions {
}

@Throws(JsonDataException::class)
inline fun <reified T> String.deserialize(): T? {
val jsonAdapter = MoshiExtensions.moshi.adapter(T::class.java)
inline fun <reified T> String.deserialize(moshi: Moshi? = null): T? {
val jsonAdapter = (moshi ?: MoshiExtensions.moshi).adapter(T::class.java)
return jsonAdapter.fromJson(this)
}

@Throws(JsonDataException::class)
inline fun <reified T> String.deserializeList(): List<T>? {
inline fun <reified T> String.deserializeList(moshi: Moshi? = null): List<T>? {
val type = Types.newParameterizedType(MutableList::class.java, T::class.java)
val jsonAdapter: JsonAdapter<List<T>> = MoshiExtensions.moshi.adapter(type)
val jsonAdapter: JsonAdapter<List<T>> = (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) {
Expand All @@ -42,7 +42,7 @@ fun String.canConvertTo(type: Class<*>): Boolean {
}
}

inline fun <reified T> T.serialize(): String {
val jsonAdapter = MoshiExtensions.moshi.adapter(T::class.java)
inline fun <reified T> T.serialize(moshi: Moshi? = null): String {
val jsonAdapter = (moshi ?: MoshiExtensions.moshi).adapter(T::class.java)
return jsonAdapter.toJson(this)
}

0 comments on commit 25a310e

Please sign in to comment.