From cdebe4038580564fc45ef507303c334b94fbb7eb Mon Sep 17 00:00:00 2001 From: Ignat Beresnev Date: Wed, 19 Jan 2022 08:32:49 +0300 Subject: Reuse `ObjectMapper` (#2315) * Reuse `ObjectMapper` --- core/src/main/kotlin/links/DRI.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'core/src/main') diff --git a/core/src/main/kotlin/links/DRI.kt b/core/src/main/kotlin/links/DRI.kt index c646934d..2cbae8cc 100644 --- a/core/src/main/kotlin/links/DRI.kt +++ b/core/src/main/kotlin/links/DRI.kt @@ -33,15 +33,20 @@ abstract class DRIExtraProperty { ?: (this.javaClass.let { it.`package`.name + "." + it.simpleName.ifEmpty { "anonymous" } }) } + class DRIExtraContainer(val data: String? = null) { - val map: MutableMap = if (data != null) ObjectMapper().readValue(data) else mutableMapOf() + val map: MutableMap = if (data != null) OBJECT_MAPPER.readValue(data) else mutableMapOf() inline operator fun get(prop: DRIExtraProperty): T? = map[prop.key]?.let { prop as? T } inline operator fun set(prop: DRIExtraProperty, value: T) = value.also { map[prop.key] = it as Any } - fun encode(): String = ObjectMapper().writeValueAsString(map) + fun encode(): String = OBJECT_MAPPER.writeValueAsString(map) + + private companion object { + private val OBJECT_MAPPER = ObjectMapper() + } } val DriOfUnit = DRI("kotlin", "Unit") -- cgit