orbit.client.serializer.Serializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of orbit-client Show documentation
Show all versions of orbit-client Show documentation
Orbit is a system to make building highly scalable realtime services easier.
/*
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