public static JsonObject convertToJson(final Object obj) {
final JsonObjectBuilder b = Json.createObjectBuilder();
try {
final Class<?> objClass = obj.getClass();
for (final Field field : objClass.getDeclaredFields()) {
String name = field.getName();
if (field.getAnnotation(XmlTransient.class) != null) {
continue;
}
final XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null && xmlElement.name() != null) {
name = xmlElement.name();
}
field.setAccessible(true);
final Object value = field.get(obj);
if (value == null) {
continue;
}
if (field.getType()
.isEnum()) {
final String enumValue = ((Enum<?>) value).name();
final XmlEnumValue xmlEnumValue = ((Enum<?>) value).getDeclaringClass()
.getField(enumValue)
.getAnnotation(XmlEnumValue.class);
if (xmlEnumValue != null && xmlEnumValue.value() != null) {
b.add(name, xmlEnumValue.value());
} else {
b.add(name, enumValue);
}
} else if (field.getType() == String.class) {
b.add(name, (String) value);
} else if (field.getType() == Integer.class) {
b.add(name, (Integer) value);
} else if (field.getType() == BigInteger.class) {
b.add(name, (BigInteger) value);
}
}
return b.build();
} catch (IllegalAccessException
| SecurityException
| NoSuchFieldException e1) {
throw new RuntimeException(e1);
}
}
Util.java 文件源码
java
阅读 28
收藏 0
点赞 0
评论 0
项目:openid-connect
作者:
评论列表
文章目录