com.mokung.pomegranate.jackson.expand.constant.EnumInterfaceJsonDeserializer Maven / Gradle / Ivy
package com.mokung.pomegranate.jackson.expand.constant;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Objects;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
/**
* 枚举接口反序列化
*
* @author mokung
*/
public class EnumInterfaceJsonDeserializer extends JsonDeserializer> {
@Override
public EnumInterface> deserialize(JsonParser p, DeserializationContext context) throws IOException {
Class> enumClazz = getEnumClass(p);
Method method = getMethod(enumClazz);
Class> valueType = getValueType(method);
Object jsonValue = getValue(valueType, p);
if (jsonValue == null) {
return null;
}
Object[] enums = enumClazz.getEnumConstants();
for (Object enumObject : enums) {
Object enumValue = invokeMethod(method, enumObject);
if (Objects.equals(jsonValue, enumValue)) {
return (EnumInterface>)enumObject;
}
}
return null;
}
private Class> getEnumClass(JsonParser p) throws IOException {
Object obj = p.getCurrentValue();
String name = p.getCurrentName();
if (obj == null || name == null || name.trim().length() <= 0) {
throw new IOException("CurrentValue is Not Enum!");
}
Class> clazz = obj.getClass();
try {
Field field = clazz.getDeclaredField(name);
Class> enumClass = field.getType();
if (!enumClass.isEnum()) {
throw new IOException("CurrentValue is Not Enum!");
}
return enumClass;
} catch (NoSuchFieldException e) {
throw new IOException(e);
}
}
private Class> getValueType(Method getJsonValueMethod) {
return getJsonValueMethod.getReturnType();
}
private Method getMethod(Class> enumClazz) throws IOException {
try {
return enumClazz.getMethod("getJsonValue");
} catch (NoSuchMethodException e) {
throw new IOException(e);
}
}
private Object invokeMethod(Method method, Object enumObject) throws IOException {
try {
return method.invoke(enumObject);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IOException(e);
}
}
private Object getValue(Class> valueType, JsonParser p) throws IOException {
if (valueType == null) {
throw new NullPointerException();
}
if (Objects.equals(valueType, Boolean.class) || Objects.equals(valueType, boolean.class)) {
return p.getBooleanValue();
}
if (Objects.equals(valueType, Byte.class) || Objects.equals(valueType, byte.class)) {
return Integer.valueOf(p.getIntValue()).byteValue();
}
if (Objects.equals(valueType, Short.class) || Objects.equals(valueType, short.class)) {
return p.getShortValue();
}
if (Objects.equals(valueType, Integer.class) || Objects.equals(valueType, int.class)) {
return p.getIntValue();
}
if (Objects.equals(valueType, Float.class) || Objects.equals(valueType, float.class)) {
return p.getFloatValue();
}
if (Objects.equals(valueType, Double.class) || Objects.equals(valueType, double.class)) {
return p.getDoubleValue();
}
if (Objects.equals(valueType, BigInteger.class)) {
return p.getBigIntegerValue();
}
if (Objects.equals(valueType, BigDecimal.class)) {
return p.getDecimalValue();
}
if (Objects.equals(valueType, String.class)) {
return p.getText();
}
return null;
}
}