All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.lq.comment.util.JsonOperate Maven / Gradle / Ivy
package com.lq.comment.util;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JsonOperate {
private static final String
stringType = "java.lang.String",
charType = "char",
listType = "java.util.List",
packArr = "[L",
packStringArr = "[Ljava.lang.String;";
private static final List normalArr = Arrays.asList("[I", "[F", "[Z", "[D", "[C");
private static final String packageName = "com.lq.doc.entity";
public static String objectToJson(Object o) throws Exception {
Class> aClass = o.getClass();
StringBuilder jsonSb = new StringBuilder();
objectToJson(o, aClass, jsonSb);
jsonSb.delete(jsonSb.length() - 1, jsonSb.length());
return jsonSb.toString();
}
private static void objectToJson(Object o, Class> aClass, StringBuilder jsonSb) throws Exception {
jsonSb.append("{");
List declaredFieldList = new ArrayList<>();
ClassUtil.getDeclaredFields(aClass,declaredFieldList);
for (Field field : declaredFieldList) {
field.setAccessible(true);
String type = field.getType().getName();
String name = field.getName();
Object val = field.get(o);
jsonSb.append("\"").append(name).append("\":");
if (type.equals(stringType) || type.equals(charType)) {
jsonSb.append("\"").append(val).append("\",");
} else if (type.equals(listType)) {
String typeName = field.getGenericType().getTypeName();
if (typeName.contains(stringType) || typeName.contains(charType)) {
List vals = (List) val;
if (vals != null && vals.size() > 0) {
jsonSb.append("[");
for (Object o1 : vals) {
jsonSb.append("\"").append(o1).append("\",");
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else if (typeName.contains(packageName)) {
List vals = (List) val;
if (vals != null && vals.size() > 0) {
jsonSb.append("[");
for (Object o1 : vals) {
objectToJson(o1, o1.getClass(), jsonSb);
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else {
jsonSb.append(val);
}
} else if (val != null && val.getClass().isEnum()) {
jsonSb.append("\"").append(val).append("\",");
} else if (val != null && type.contains(packageName)) {
objectToJson(val, val.getClass(), jsonSb);
} else if (type.startsWith(packArr)) {
Object[] objArr = (Object[]) val;
if (objArr != null && objArr.length > 0) {
jsonSb.append("[");
if (type.equals(packStringArr)) {
for (Object v : objArr) {
jsonSb.append("\"").append(v).append("\",");
}
} else {
for (Object v : objArr) {
jsonSb.append(v).append(",");
}
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else if (normalArr.contains(type)) {
if (type.equals(normalArr.get(0))) {
int[] intVal = (int[]) val;
if (intVal != null && intVal.length > 0) {
jsonSb.append("[");
for (int v : intVal) {
jsonSb.append(v).append(",");
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else if (type.equals(normalArr.get(1))) {
float[] intVal = (float[]) val;
if (intVal != null && intVal.length > 0) {
jsonSb.append("[");
for (float v : intVal) {
jsonSb.append(v).append(",");
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else if (type.equals(normalArr.get(2))) {
boolean[] intVal = (boolean[]) val;
if (intVal != null && intVal.length > 0) {
jsonSb.append("[");
for (boolean v : intVal) {
jsonSb.append(v).append(",");
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else if (type.equals(normalArr.get(3))) {
double[] intVal = (double[]) val;
if (intVal != null && intVal.length > 0) {
jsonSb.append("[");
for (double v : intVal) {
jsonSb.append(v).append(",");
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("],");
} else {
jsonSb.append("[],");
}
} else {
jsonSb.append("[],");
}
} else {
jsonSb.append(val).append(",");
}
}
jsonSb.delete(jsonSb.length() - 1, jsonSb.length()).append("},");
}
}