package com.izouma.nineth.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.izouma.nineth.domain.MintMaterial;
import org.apache.commons.lang3.StringUtils;

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

@Converter
public class MintMaterialListConverter implements AttributeConverter<List<MintMaterial>, String> {

    @Override
    public String convertToDatabaseColumn(List<MintMaterial> materials) {
        if (materials == null) {
            return null;
        }
        return JSON.toJSONString(materials);
    }

    @Override
    public List<MintMaterial> convertToEntityAttribute(String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        try {
            return JSONArray.parseArray(s, MintMaterial.class);
        } catch (Exception ignored) {
        }
        return null;
    }
}