All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deque.axe.android.moshi.MoshiConfig.kt Maven / Gradle / Ivy

There is a newer version: 5.5.2
Show newest version
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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy