package com.njcn.relational.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;

/**
 * Clob 字段 JSON 序列化器
 * 将数据库 Clob 类型转换为字符串
 */
public class ClobSerializer extends JsonSerializer<Clob> {

    @Override
    public void serialize(Clob clob, JsonGenerator gen, SerializerProvider serializers)
            throws IOException {

        if (clob == null) {
            gen.writeNull();
            return;
        }

        try {
            // 获取 Clob 字符串内容
            String content = clob.getSubString(1, (int) clob.length());
            gen.writeString(content);
        } catch (SQLException e) {
            gen.writeNull();
        }
    }
}
