Json转实体类
问题:现项目中已有的json转换依赖版本低,无法处理大小写敏感映射问题,自实现一个忽略大小写敏感映射实体类的方法。
具体使用各自优化。
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
public class JsonToEntityConverter {
public static <U> U convert(JSONObject jsonObject, Class<U> clazz) throws Exception {
U instance = clazz.getDeclaredConstructor().newInstance();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String jsonFieldName = getMatchingJsonFieldName(jsonObject, field.getName());
if (jsonFieldName != null) {
field.setAccessible(true);
Class<?> fieldType = field.getType();
Object value = jsonObject.get(jsonFieldName);
if (value == null) {
continue;
}
if (fieldType.isPrimitive() || fieldType == String.class || Number.class.isAssignableFrom(fieldType)) {
if (fieldType == int.class) {
field.setInt(instance, ((Number) value).intValue());
} else if (fieldType == boolean.class) {
field.setBoolean(instance, (boolean) value);
} else if (fieldType == long.class) {
field.setLong(instance, ((Number) value).longValue());
} else if (fieldType == double.class) {
field.setDouble(instance, ((Number) value).doubleValue());
} else if (fieldType == Integer.class) {
if (value instanceof String) {
field.set(instance, Integer.parseInt((String) value));
} else {
field.set(instance, (Integer) value);
}
} else if (fieldType == Long.class) {
if (value instanceof String) {
field.set(instance, Long.parseLong((String) value));
} else {
field.set(instance, (Long) value);
}
} else if (fieldType == Double.class) {
if (value instanceof String) {
field.set(instance, Double.parseDouble((String) value));
} else {
field.set(instance, (Double) value);
}
} else {
field.set(instance, value);
}
} else if (Collection.class.isAssignableFrom(fieldType)) {
if (value instanceof JSONArray) {
ParameterizedType listType = (ParameterizedType) field.getGenericType();
Type actualType = listType.getActualTypeArguments()[0];
if (actualType instanceof Class) {
Class<?> listClass = (Class<?>) actualType;
Collection<Object> collection = convertCollection((JSONArray) value, listClass);
field.set(instance, collection.isEmpty() ? null : collection);
}
}
} else {
if (value instanceof JSONObject) {
JSONObject jsonValue = (JSONObject) value;
if (jsonValue.isEmpty()) {
field.set(instance, null);
} else {
Object nestedObject = convert((JSONObject) value, fieldType);
field.set(instance, nestedObject);
}
}
}
}
}
return instance;
}
private static String getMatchingJsonFieldName(JSONObject jsonObject, String fieldName) {
for (String jsonFieldName : jsonObject.keySet()) {
if (jsonFieldName.equalsIgnoreCase(fieldName)) {
return jsonFieldName;
}
}
return null;
}
private static <U> Collection<U> convertCollection(JSONArray jsonArray, Class<U> elementType) throws Exception {
Collection<U> collection = new ArrayList<>();
for (int i = 0; i < jsonArray.size(); i++) {
Object item = jsonArray.get(i);
if (item instanceof JSONObject) {
JSONObject jsonItem = (JSONObject) item;
if (!jsonItem.isEmpty()) {
U element = convert(jsonItem, elementType);
collection.add(element);
}
} else {
collection.add((U) item);
}
}
return collection;
}
}