Для конвертеров из/в jsonb-поля можно завести базовые классы, которые соберут в себе всю логику:
@WritingConverter
interface ObjectToJsonbWriter<T : Any> : Converter<T, PGobject>
@ReadingConverter
interface JsonbToObjectReader<T : Any> : Converter<PGobject, T>
class JacksonObjectToJsonbWriter<T : Any>(
private val objectMapper: ObjectMapper
) : ObjectToJsonbWriter<T> {
override fun convert(source: T): PGobject =
source.let {
PGobject().apply {
type = "jsonb"
value = objectMapper.writeValueAsString(source)
}
}
}
@ReadingConverter
class JacksonJsonbToObjectReader<T : Any>(
private val objectMapper: ObjectMapper,
private val type: KClass<T>
) : JsonbToObjectReader<T> {
override fun convert(source: PGobject): T? =
if (source.isNull) {
null
} else {
objectMapper.readValue(source.value!!, type.java)
}
}
Затем, конвертеры конкретных типов можно будет делать однострочниками:
class CustomFieldValueWriter(
objectMapper: ObjectMapper
) : ObjectToJsonbWriter<BaseCustomFieldValue<*>> by JacksonObjectToJsonbWriter(objectMapper)
class CustomFieldValueReader(
objectMapper: ObjectMapper,
) : JsonbToObjectReader<BaseCustomFieldValue<*>> by JacksonJsonbToObjectReader(
objectMapper,
BaseCustomFieldValue::class
)