package com.izouma.awesomeAdmin.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.AttributeConverter;

@Slf4j
public class JSONObjectConverter implements AttributeConverter<JSONObject, String> {
    @Override
    public String convertToDatabaseColumn(JSONObject jsonObject) {
        if (jsonObject != null) {
            return jsonObject.toJSONString();
        }
        return null;
    }

    @Override
    public JSONObject convertToEntityAttribute(String s) {
        try {
            return JSON.parseObject(s);
        } catch (Exception e) {
            log.error("parse json error", e);
        }
        return null;
    }
}
