com.autonomousapps.internal.utils.moshi.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dependency-analysis-gradle-plugin Show documentation
Show all versions of dependency-analysis-gradle-plugin Show documentation
Analyzes dependency usage in Android and JVM projects
@file:JvmName("MoshiUtils")
package com.autonomousapps.internal.utils
import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.ToJson
import com.squareup.moshi.Types.newParameterizedType
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import java.io.File
val MOSHI: Moshi by lazy {
Moshi.Builder()
.add(DependencyGraphAdapter())
.add(KotlinJsonAdapterFactory())
.add(TypeAdapters())
.build()
}
inline fun getJsonAdapter(withNulls: Boolean = false): JsonAdapter {
val adapter = MOSHI.adapter(T::class.java)
return if (withNulls) {
adapter.serializeNulls()
} else {
adapter
}
}
inline fun getJsonListAdapter(withNulls: Boolean = false): JsonAdapter> {
val type = newParameterizedType(List::class.java, T::class.java)
val adapter = MOSHI.adapter>(type)
return if (withNulls) {
adapter.serializeNulls()
} else {
adapter
}
}
inline fun getJsonSetAdapter(withNulls: Boolean = false): JsonAdapter> {
val type = newParameterizedType(Set::class.java, T::class.java)
val adapter = MOSHI.adapter>(type)
return if (withNulls) {
adapter.serializeNulls()
} else {
adapter
}
}
inline fun getJsonMapAdapter(withNulls: Boolean = false): JsonAdapter