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

net.dankito.utils.serialization.JacksonJsonSerializer.kt Maven / Gradle / Ivy

The newest version!
package net.dankito.utils.serialization

import com.fasterxml.jackson.annotation.JsonAutoDetect
import com.fasterxml.jackson.annotation.PropertyAccessor
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import net.dankito.utils.io.FileUtils
import java.io.File


open class JacksonJsonSerializer(protected val fileUtils: FileUtils = FileUtils(),
                                 configureMapperCallback: ((mapper: ObjectMapper) -> Unit)? = null) : ISerializer {

    protected val objectMapper = ObjectMapper()


    constructor(configureMapperCallback: ((mapper: ObjectMapper) -> Unit)? = null)
            : this(FileUtils(), configureMapperCallback)


    init {
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

        // only serialize fields
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
        objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)

        configureMapperCallback?.invoke(objectMapper)
    }


    override val serializationFormat = ISerializer.SerializationFormatJson


    override fun serializeObject(obj: Any, outputFile: File) {
        val json = serializeObject(obj)
        fileUtils.writeToTextFile(json, outputFile)
    }

    override fun serializeObject(obj: Any): String {
        return objectMapper.writeValueAsString(obj)
    }


    override fun  deserializeObject(serializedObjectFile: File, objectClass: Class, vararg genericParameterTypes: Class<*>): T? {
        if (serializedObjectFile.exists()) {
            fileUtils.readFromTextFile(serializedObjectFile)?.let { json ->
                return deserializeObject(json, objectClass, *genericParameterTypes)
            }
        }

        return null
    }


    override fun  deserializeList(serializedObjectFile: File, genericListParameterType: Class): List? {
        return deserializeObject(serializedObjectFile, List::class.java, genericListParameterType) as? List
    }

    override fun  deserializeListOr(serializedObjectFile: File, genericListParameterType: Class,
                                       defaultValue: List): List {
        return deserializeList(serializedObjectFile, genericListParameterType) ?: defaultValue
    }


    override fun  deserializeSet(serializedObjectFile: File, genericSetParameterType: Class): Set? {
        return deserializeObject(serializedObjectFile, Set::class.java, genericSetParameterType) as? Set
    }

    override fun  deserializeSetOr(serializedObjectFile: File, genericSetParameterType: Class,
                                      defaultValue: Set): Set {

        return deserializeSet(serializedObjectFile, genericSetParameterType) ?: defaultValue
    }


    override fun  deserializeMap(serializedObjectFile: File, keyParameterType: Class,
                                             valueParameterType: Class): Map? {

        return deserializeObject(serializedObjectFile, Map::class.java, keyParameterType, valueParameterType) as? Map
    }

    override fun  deserializeMapOr(serializedObjectFile: File, keyParameterType: Class,
                                   valueParameterType: Class, defaultValue: Map): Map {

        return deserializeMap(serializedObjectFile, keyParameterType, valueParameterType) ?: defaultValue
    }



    override fun  deserializeObject(serializedObject: String, objectClass: Class, vararg genericParameterTypes: Class<*>): T {
        if(genericParameterTypes.isEmpty()) {
            return objectMapper.readValue(serializedObject, objectClass)
        }
        else {
            return objectMapper.readValue(serializedObject, objectMapper.typeFactory.constructParametricType(objectClass, *genericParameterTypes))
        }
    }


    override fun  deserializeList(serializedObject: String, genericListParameterType: Class): List? {
        return deserializeObject(serializedObject, List::class.java, genericListParameterType) as? List
    }

    override fun  deserializeListOr(serializedObject: String, genericListParameterType: Class,
                                       defaultValue: List): List {
        return deserializeList(serializedObject, genericListParameterType) ?: defaultValue
    }


    override fun  deserializeSet(serializedObject: String, genericSetParameterType: Class): Set? {
        return deserializeObject(serializedObject, Set::class.java, genericSetParameterType) as? Set
    }

    override fun  deserializeSetOr(serializedObject: String, genericSetParameterType: Class,
                                      defaultValue: Set): Set {

        return deserializeSet(serializedObject, genericSetParameterType) ?: defaultValue
    }


    override fun  deserializeMap(serializedObject: String, keyParameterType: Class,
                                             valueParameterType: Class): Map? {

        return deserializeObject(serializedObject, Map::class.java, keyParameterType, valueParameterType) as? Map
    }

    override fun  deserializeMapOr(serializedObject: String, keyParameterType: Class,
                                   valueParameterType: Class, defaultValue: Map): Map {

        return deserializeMap(serializedObject, keyParameterType, valueParameterType) ?: defaultValue
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy