com.deque.axe.android.moshi.MoshiConfig.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of axe-devtools-android-data Show documentation
Show all versions of axe-devtools-android-data Show documentation
The Axe Devtools Android Data Library
package com.deque.axe.android.moshi
import com.deque.axe.android.constants.AxeScrollDirection
import com.deque.axe.android.utils.SuppressFBWarnings
import com.squareup.moshi.Moshi
import com.squareup.moshi.adapters.PolymorphicJsonAdapterFactory
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
object MoshiConfig {
private val extraAdapters = mutableSetOf()
val axeMoshiConfig get() = AxeMoshiConfig(extraAdapters)
fun prettyPrint(uglyPrint: T, clazz: Class): String {
return AxeMoshiConfig(extraAdapters).moshi.adapter(clazz).indent(" ").toJson(uglyPrint)
}
@SuppressFBWarnings
fun addAdapters(adapterSet: Set) {
extraAdapters.addAll(adapterSet)
}
}
class AxeMoshiConfig(additionalAdapters: Set = setOf()) {
val moshi: Moshi get() = _moshi
private var _moshi = getMoshiInstance(additionalAdapters)
private fun getMoshiInstance(additionalAdapters: Set): Moshi {
val scrollAdapter = PolymorphicJsonAdapterFactory.of(AxeScrollDirection::class.java, "scrollType")
.withSubtype(AxeScrollDirection.NonScrolling::class.java, "nonScrolling")
.withSubtype(AxeScrollDirection.ScrollVertical::class.java, "verticalScroll")
.withSubtype(AxeScrollDirection.ScrollHorizontal::class.java, "horizontalScroll")
val builder = Moshi.Builder()
additionalAdapters.forEach { builder.add(it) }
builder
.add(AxeColorAdapter())
.add(AxeViewAdapter())
.add(scrollAdapter)
.add(AxeResultAdapter())
.add(AxePropsAdapter())
.add(AccessTokenResponseAdapter())
.add(AuthUrlResponseAdapter())
.addLast(KotlinJsonAdapterFactory())
return builder.build()
}
}