package com.izouma.nineth.converter;

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

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

public class SurpriseBoxContentListConverter implements AttributeConverter<List<SurpriseBoxContent>, String> {

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

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