package com.changhu.support.mybatisplus.handler.global.json;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.util.List;

/**
 * author: luozhun
 * desc: 将 mysql json 映射成 list
 * createTime: 2023/10/30 15:50
 */
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({List.class})
public class ListTypeHandler extends AbstractJsonTypeHandler<List<Object>> {

    public ListTypeHandler(Class<?> type) {
        super(type);
    }

    @Override
    public List<Object> parse(String json) {
        if (!JSON.isValid(json)) {
            return null;
        }
        return JSON.parseArray(json);
    }

    @Override
    public String toJson(List<Object> obj) {
        return JSON.toJSONString(obj);
    }

}
