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

orbit.client.serializer.Serializer.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0a19
Show newest version
/*
 Copyright (C) 2015 - 2019 Electronic Arts Inc.  All rights reserved.
 This file is part of the Orbit Project .
 See license in LICENSE.
 */

package orbit.client.serializer

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.jsontype.BasicPolymorphicTypeValidator
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator
import com.fasterxml.jackson.module.kotlin.registerKotlinModule

internal class Serializer {
    private val validator: PolymorphicTypeValidator = BasicPolymorphicTypeValidator.builder()
        .allowIfBaseType(Any::class.java)
        .build()

    private val mapper = ObjectMapper()
        .activateDefaultTyping(validator, ObjectMapper.DefaultTyping.EVERYTHING)
        .registerKotlinModule()

    fun  serialize(obj: T?): String = mapper.writeValueAsString(obj)
    fun  deserialize(str: String, clazz: Class): T = mapper.readValue(str, clazz)
    inline fun  deserialize(str: String) = deserialize(str, T::class.java)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy