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

dk.nodes.nstack.kotlin.provider.GsonProvider.kt Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
package dk.nodes.nstack.kotlin.provider

import com.google.gson.FieldNamingPolicy
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dk.nodes.nstack.kotlin.util.DateDeserializer
import dk.nodes.nstack.kotlin.util.LocaleDeserializer
import java.util.Date
import java.util.Locale
import kotlin.reflect.KClass

object GsonProvider {

    fun provideGson(): Gson {
        return getLazyDependency(Gson::class) {
            GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .registerTypeAdapter(Date::class.java, DateDeserializer())
                .registerTypeAdapter(Locale::class.java, LocaleDeserializer())
                .setDateFormat(DateDeserializer.DATE_FORMAT)
                .create()
        }
    }

    @Suppress("UNCHECKED_CAST")
    private inline fun  getLazyDependency(
        clazz: KClass,
        crossinline block: () -> T
    ): T {
        if (!dependenciesMap.containsKey(clazz)) {
            dependenciesMap[clazz] = block()
        }
        return dependenciesMap[clazz] as T
    }

    private val dependenciesMap = mutableMapOf, Any>()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy