102 lines
3.4 KiB
Java
102 lines
3.4 KiB
Java
|
|
package com.engine.salary.util;
|
||
|
|
|
||
|
|
import com.alibaba.fastjson.JSON;
|
||
|
|
import com.alibaba.fastjson.JSONObject;
|
||
|
|
import com.alibaba.fastjson.parser.Feature;
|
||
|
|
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||
|
|
import com.alibaba.fastjson.util.TypeUtils;
|
||
|
|
|
||
|
|
import java.lang.reflect.Type;
|
||
|
|
import java.util.Iterator;
|
||
|
|
import java.util.LinkedHashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Map.Entry;
|
||
|
|
|
||
|
|
public class JsonUtil {
|
||
|
|
private static final SerializerFeature[] DEFAULT_S_FEATURES;
|
||
|
|
private static final SerializerFeature[] PRETTY_S_FEATURES;
|
||
|
|
private static final Feature[] DEFAULT_P_FEATURES;
|
||
|
|
|
||
|
|
public JsonUtil() {
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String toJsonString(Object obj) {
|
||
|
|
return JSON.toJSONString(obj, DEFAULT_S_FEATURES);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String toJsonString(Object obj, SerializerFeature... features) {
|
||
|
|
return JSON.toJSONString(obj, features);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static String toPrettyJson(Object object) {
|
||
|
|
return JSON.toJSONString(object, PRETTY_S_FEATURES);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static JSONObject parseJsonObject(String jsonStr) {
|
||
|
|
return JSON.parseObject(jsonStr, DEFAULT_P_FEATURES);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> T parseValue(JSONObject jsonObject, String key, Class<T> clazz) {
|
||
|
|
if (jsonObject != null) {
|
||
|
|
T value = jsonObject.getObject(key, (Type) clazz);
|
||
|
|
return value;
|
||
|
|
} else {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> T parseObject(String jsonStr, Class<T> clazz) {
|
||
|
|
return JSON.parseObject(jsonStr, (Type) clazz, DEFAULT_P_FEATURES);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> List<T> parseList(String jsonStr, Class<T> clazz) {
|
||
|
|
return JSON.parseArray(jsonStr, clazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> List<T> parseList(Object jsonObject, Class<T> clazz) {
|
||
|
|
String jsonStr = toJsonString(jsonObject);
|
||
|
|
return parseList(jsonStr, clazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <V> Map<String, V> parseMap(String jsonStr, Class<V> valueCls) {
|
||
|
|
Map<String, V> result = new LinkedHashMap();
|
||
|
|
Map<String, Object> map = JSON.parseObject(jsonStr, DEFAULT_P_FEATURES);
|
||
|
|
if (map != null && map.size() > 0) {
|
||
|
|
Iterator var4 = map.entrySet().iterator();
|
||
|
|
|
||
|
|
while(var4.hasNext()) {
|
||
|
|
Entry<String, Object> entry = (Entry)var4.next();
|
||
|
|
Object obj = entry.getValue();
|
||
|
|
|
||
|
|
try {
|
||
|
|
V value = JSON.parseObject(JSON.toJSONString(obj), valueCls);
|
||
|
|
result.put(entry.getKey(), value);
|
||
|
|
} catch (Exception var8) {
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <V> Map<String, V> parseMap(Object jsonObject, Class<V> valueCls) {
|
||
|
|
String jsonStr = toJsonString(jsonObject);
|
||
|
|
return parseMap(jsonStr, valueCls);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> T parseBean(String jsonString, Class<T> beanClazz) {
|
||
|
|
return parseBean(parseJsonObject(jsonString), beanClazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static <T> T parseBean(JSONObject jsonObject, Class<T> beanClazz) {
|
||
|
|
return TypeUtils.castToJavaBean(jsonObject, beanClazz);
|
||
|
|
}
|
||
|
|
|
||
|
|
static {
|
||
|
|
DEFAULT_S_FEATURES = new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat, SerializerFeature.SortField};
|
||
|
|
PRETTY_S_FEATURES = new SerializerFeature[]{SerializerFeature.WriteDateUseDateFormat, SerializerFeature.SortField, SerializerFeature.PrettyFormat};
|
||
|
|
DEFAULT_P_FEATURES = new Feature[]{Feature.OrderedField};
|
||
|
|
}
|
||
|
|
}
|