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

com.tairitsu.ignotus.support.util.JSON.kt Maven / Gradle / Ivy

package com.tairitsu.ignotus.support.util

import com.fasterxml.jackson.core.type.TypeReference
import com.tairitsu.ignotus.support.service.JSONMapperRegister

object JSON {
    private val mapper
        get() = JSONMapperRegister.objectMapperProvider.objectMapper

    @JvmStatic
    fun  parseObject(content: String, clazz: Class): T? {
        return mapper.readValue(content, clazz)
    }

    @JvmStatic
    fun  parseObject(content: String, type: TypeReference): T? {
        return mapper.readValue(content, type)
    }

    @JvmStatic
    fun parseObjectToMap(content: String): Map {
        return mapper.readValue(content, object : TypeReference>() {})
    }

    fun  String.jsonToObject(clazz: Class): T? {
        return try {
            mapper.readValue(this, clazz)
        } catch (e: Exception) {
            null
        }
    }

    fun  String.jsonToObject(type: TypeReference): T? {
        return try {
            mapper.readValue(this, type)
        } catch (e: Exception) {
            null
        }
    }

    fun String.jsonToMap(): Map? {
        return try {
            mapper.readValue(this, object : TypeReference>() {})
        } catch (e: Exception) {
            null
        }
    }

    @JvmStatic
    fun  toJSONString(value: T): String {
        return mapper.writeValueAsString(value)
    }

    fun Any.toJson(): String {
        return mapper.writeValueAsString(this)
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy