package com.izouma.nineth.converter;


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

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.List;

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

    @Override
    public List<JSONObject> convertToEntityAttribute(String s) {
        try {
            return JSONArray.parseArray(s, JSONObject.class);
        } catch (Exception e) {
            log.error("parse json error", e);
        }
        return null;
    }
}
