use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project uavstack by uavorg.
the class TypeUtils method castToEnum.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
try {
if (obj instanceof String) {
String name = (String) obj;
if (name.length() == 0) {
return null;
}
if (mapping == null) {
mapping = ParserConfig.getGlobalInstance();
}
ObjectDeserializer derializer = mapping.getDeserializer(clazz);
if (derializer instanceof EnumDeserializer) {
EnumDeserializer enumDeserializer = (EnumDeserializer) derializer;
return (T) enumDeserializer.getEnumByHashCode(TypeUtils.fnv1a_64(name));
}
return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
}
if (obj instanceof BigDecimal) {
int ordinal = intValue((BigDecimal) obj);
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
return (T) values[ordinal];
}
}
if (obj instanceof Number) {
int ordinal = ((Number) obj).intValue();
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
return (T) values[ordinal];
}
}
} catch (Exception ex) {
throw new JSONException("can not cast to : " + clazz.getName(), ex);
}
throw new JSONException("can not cast to : " + clazz.getName());
}
use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.
the class TypeUtils method castToEnum.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
try {
if (obj instanceof String) {
String name = (String) obj;
if (name.length() == 0) {
return null;
}
if (mapping == null) {
mapping = ParserConfig.getGlobalInstance();
}
ObjectDeserializer deserializer = mapping.getDeserializer(clazz);
if (deserializer instanceof EnumDeserializer) {
EnumDeserializer enumDeserializer = (EnumDeserializer) deserializer;
return (T) enumDeserializer.getEnumByHashCode(TypeUtils.fnv1a_64(name));
}
return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
}
if (obj instanceof BigDecimal) {
int ordinal = intValue((BigDecimal) obj);
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
return (T) values[ordinal];
}
}
if (obj instanceof Number) {
int ordinal = ((Number) obj).intValue();
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
return (T) values[ordinal];
}
}
} catch (Exception ex) {
throw new JSONException("can not cast to : " + clazz.getName(), ex);
}
throw new JSONException("can not cast to : " + clazz.getName());
}
use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.
the class EnumParserTest method test_error_3.
public void test_error_3() throws Exception {
Exception error = null;
try {
String text = "4";
DefaultJSONParser parser = new DefaultJSONParser(text);
new EnumDeserializer(Object.class).deserialze(parser, Object.class, null);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
use of com.alibaba.fastjson.parser.deserializer.EnumDeserializer in project fastjson by alibaba.
the class EnumParserTest method test_error_4.
public void test_error_4() throws Exception {
Exception error = null;
try {
String text = "true";
DefaultJSONParser parser = new DefaultJSONParser(text);
new EnumDeserializer(Object.class).deserialze(parser, Object.class, null);
} catch (Exception ex) {
error = ex;
}
Assert.assertNotNull(error);
}
Aggregations