package com.izouma.nineth.converter;

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

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class PreEmptionPrivilegeConverter implements AttributeConverter<PreEmptionPrivilege, String> {

    @Override
    public String convertToDatabaseColumn(PreEmptionPrivilege privileges) {
        if (privileges == null) {
            return null;
        }
        return JSON.toJSONString(privileges);
    }

    @Override
    public PreEmptionPrivilege convertToEntityAttribute(String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        try {
            return JSON.parseObject(s, PreEmptionPrivilege.class);
        } catch (Exception ignored) {
        }
        return null;
    }
}