JsonNodeReadingConverter.kt

/*
  Copyright (C) 2026 Sber

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package org.archtool.configuration.jdbc

import org.postgresql.util.PGobject
import org.slf4j.LoggerFactory
import org.springframework.core.convert.converter.Converter
import org.springframework.data.convert.ReadingConverter
import tools.jackson.databind.JsonNode
import tools.jackson.databind.json.JsonMapper
import tools.jackson.databind.node.MissingNode

/**
 * Конвертер для чтения JsonNode из колонки jsonb.
 * Структура данных в этом поле может быть произвольной.
 */
@ReadingConverter
class JsonNodeReadingConverter(private val jsonMapper: JsonMapper) : Converter<PGobject, JsonNode> {

    val log = LoggerFactory.getLogger(javaClass)

    override fun convert(source: PGobject): JsonNode =
        try {
            jsonMapper.readTree(source.value)
        } catch (e: Exception) {
            log.error("Не удалось прочитать JSONB поле", e)
            MissingNode.getInstance()
        }
}