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

import cn.hutool.core.lang.Dict;
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;


@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes({Dict.class})
public class DictTypeHandler extends AbstractJsonTypeHandler<Dict> {

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

    @Override
    public Dict parse(String json) {
        if (!JSON.isValid(json)) {
            return null;
        }
        return JSON.parseObject(json, Dict.class);
    }

    @Override
    public String toJson(Dict obj) {
        return JSON.toJSONString(obj);
    }
}