com.github.ltsopensource.json.deserializer.EnumDeserializer Maven / Gradle / Ivy
package com.github.ltsopensource.json.deserializer;
import com.github.ltsopensource.json.JSONException;
import java.lang.reflect.Type;
/**
* @author Robert HG ([email protected]) on 12/30/15.
*/
public class EnumDeserializer implements Deserializer {
private Class> enumType;
public EnumDeserializer(Class> enumType) {
this.enumType = enumType;
}
@SuppressWarnings("unchecked")
public T deserialize(Object object, Type type) {
if (object == null) {
return null;
}
if (object.getClass().isEnum()) {
return (T) object;
}
if (!(object instanceof String)) {
throw new JSONException("enum object:[" + object + "] is invalid");
}
return (T) Enum.valueOf((Class) enumType, object.toString());
}
}