package link.at17.mid.tushare.data.typehandler;

import java.util.List;

import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import link.at17.mid.tushare.data.models.UpdateMethodInfo;
import lombok.extern.slf4j.Slf4j;

import org.apache.ibatis.type.JdbcType;

@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
@Slf4j
public class UpdateMethodInfoListTypeHandler extends AbstractJsonTypeHandler<List<UpdateMethodInfo>> {
    private static final ObjectMapper mapper = new ObjectMapper();

    @Override
    protected List<UpdateMethodInfo> parse(String json) {
        try {
            return mapper.readValue(json, new TypeReference<List<UpdateMethodInfo>>() {});
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected String toJson(List<UpdateMethodInfo> obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}