package jnpf.easyexcel;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import jnpf.util.ReflectionUtil;

import java.io.IOException;
import java.lang.reflect.Type;

public class FastJsonEnumConverter implements ObjectSerializer {

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object == null) {
            serializer.write(null);
            return;
        }
        if (object instanceof Enum) {
            try {
                Object resultName = ReflectionUtil.invokeGetterMethod(object, "code");
                serializer.write(resultName);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
