List of usage examples for java.lang Enum ordinal
int ordinal
To view the source code for java.lang Enum ordinal.
Click Source Link
From source file:ca.oson.json.Oson.java
private <E> Enum<?> json2Enum(FieldData objectDTO) { objectDTO.valueToProcess = StringUtil.unquote(objectDTO.valueToProcess, isEscapeHtml()); Object valueToProcess = objectDTO.valueToProcess; Class<E> returnType = objectDTO.returnType; boolean required = objectDTO.required(); Enum defaultValue = (Enum) objectDTO.defaultValue; boolean json2Java = objectDTO.json2Java; if (returnType == null || valueToProcess == null) { if (required) { return defaultValue; }/*from w ww .j a va2s. com*/ return null; } String value = (String) valueToProcess; Class<Enum> enumType = (Class<Enum>) returnType; try { Function function = objectDTO.getDeserializer(); if (function != null) { Object returnedValue = null; if (function instanceof Json2DataMapperFunction) { DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper, objectDTO.level, getPrettyIndentation()); returnedValue = ((Json2DataMapperFunction) function).apply(classData); } else if (function instanceof Json2FieldDataFunction) { Json2FieldDataFunction f = (Json2FieldDataFunction) function; FieldData fieldData = objectDTO.clone(); returnedValue = f.apply(fieldData); } else if (function instanceof Json2EnumFunction) { return (Enum<?>) ((Json2EnumFunction) function).apply(value); } else { returnedValue = function.apply(value); } if (returnedValue instanceof Optional) { returnedValue = ObjectUtil.unwrap(returnedValue); } if (returnedValue == null) { return null; } Class type = returnedValue.getClass(); if (Enum.class.isAssignableFrom(type)) { return (Enum<?>) returnedValue; } else if (Number.class.isAssignableFrom(type)) { int ordinal = ((Number) returnedValue).intValue(); for (Enum enumValue : enumType.getEnumConstants()) { if (enumValue.ordinal() == ordinal) { return enumValue; } } } else { String name = returnedValue.toString(); for (Enum enumValue : enumType.getEnumConstants()) { if (enumValue.toString().equalsIgnoreCase(name) || enumValue.name().equalsIgnoreCase(name)) { return enumValue; } } } } } catch (Exception ex) { } for (Method method : enumType.getDeclaredMethods()) { for (Annotation annotation : method.getDeclaredAnnotations()) { String aname = annotation.annotationType().getName(); switch (aname) { case "com.fasterxml.jackson.annotation.JsonCreator": case "org.codehaus.jackson.annotate.JsonCreator": return ObjectUtil.getMethodValue(null, method, value); case "ca.oson.json.annotation.FieldMapper": ca.oson.json.annotation.FieldMapper fieldMapper = (ca.oson.json.annotation.FieldMapper) annotation; if (fieldMapper.jsonCreator() != null && fieldMapper.jsonCreator() == BOOLEAN.TRUE) { return ObjectUtil.getMethodValue(null, method, value); } } } } String fieldName = null; for (Field field : enumType.getDeclaredFields()) { String name = null; ca.oson.json.annotation.FieldMapper fieldMapper = field .getAnnotation(ca.oson.json.annotation.FieldMapper.class); if (fieldMapper != null) { name = fieldMapper.name(); if (value.equalsIgnoreCase(name)) { fieldName = field.getName(); break; } } else { for (Annotation annotation : field.getAnnotations()) { name = ObjectUtil.getName(annotation); if (value.equalsIgnoreCase(name)) { fieldName = field.getName(); break; } } } } if (fieldName != null) { try { return Enum.valueOf(enumType, fieldName.toUpperCase()); } catch (IllegalArgumentException ex) { } } try { return Enum.valueOf(enumType, value.toUpperCase()); } catch (IllegalArgumentException ex) { } for (Enum enumValue : enumType.getEnumConstants()) { if (enumValue.toString().equalsIgnoreCase(value) || enumValue.name().equalsIgnoreCase(value)) { return enumValue; } } FieldData fieldData = new FieldData(value, Integer.class, true); Integer ordinal = json2Integer(fieldData); if (ordinal != null) { for (Enum enumValue : enumType.getEnumConstants()) { if (enumValue.ordinal() == ordinal) { return enumValue; } } } return null; }
From source file:ca.oson.json.Oson.java
private <E> String enum2Json(FieldData objectDTO) { Object value = objectDTO.valueToProcess; Class<E> valueType = objectDTO.returnType; EnumType enumType = objectDTO.getEnumType(); if (value == null) { return null; }// w w w .j a v a 2s .c o m Enum en = (Enum) value; try { Function function = objectDTO.getSerializer(); if (function != null) { try { if (function instanceof DataMapper2JsonFunction) { DataMapper classData = new DataMapper(objectDTO.returnType, value, objectDTO.classMapper, objectDTO.level, getPrettyIndentation()); return ((DataMapper2JsonFunction) function).apply(classData); } else if (function instanceof Enum2JsonFunction) { return ((Enum2JsonFunction) function).apply(en); } else { Object returnedValue = null; if (function instanceof FieldData2JsonFunction) { FieldData2JsonFunction f = (FieldData2JsonFunction) function; FieldData fieldData = objectDTO.clone(); returnedValue = f.apply(fieldData); } else { returnedValue = function.apply(value); } if (returnedValue instanceof Optional) { returnedValue = ObjectUtil.unwrap(returnedValue); } if (returnedValue == null) { return null; // just ignore it, right? } else if (Enum.class.isAssignableFrom(returnedValue.getClass())) { en = (Enum) returnedValue; } else { objectDTO.valueToProcess = returnedValue; return object2String(objectDTO); } } } catch (Exception e) { } } } catch (Exception ex) { } String name = en.name(); if (enumType == null || enumType == EnumType.STRING) { for (Method method : valueType.getDeclaredMethods()) { for (Annotation annotation : method.getDeclaredAnnotations()) { String aname = annotation.annotationType().getName(); switch (aname) { case "com.fasterxml.jackson.annotation.JsonValue": case "org.codehaus.jackson.annotate.JsonValue": return ObjectUtil.getMethodValue(en, method); case "ca.oson.json.annotation.FieldMapper": ca.oson.json.annotation.FieldMapper fieldMapper = (ca.oson.json.annotation.FieldMapper) annotation; if (fieldMapper.jsonValue() != null && fieldMapper.jsonValue() == BOOLEAN.TRUE) { return ObjectUtil.getMethodValue(en, method); } } } } for (Field field : valueType.getDeclaredFields()) { if (name.equalsIgnoreCase(field.getName())) { ca.oson.json.annotation.FieldMapper fieldMapper = field .getAnnotation(ca.oson.json.annotation.FieldMapper.class); if (fieldMapper != null) { String aname = fieldMapper.name(); if (!StringUtil.isEmpty(aname)) { return aname; } } else { for (Annotation annotation : field.getAnnotations()) { String aname = ObjectUtil.getName(annotation); if (!StringUtil.isEmpty(aname)) { return aname; } } } } } return name; } switch (enumType) { case STRING: return en.name(); case ORDINAL: default: return "" + en.ordinal(); } }