1.fastJson序列化工具
1.1maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
1.2工具类
package com.mysiteforme.admin.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
/**
* 依赖fastJson
*/
public class JsonUtils {
private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").serializeNulls().create();
public static Map<String, Object> bean2Map(Object obj) {
if (null == obj) {
return null;
}
Map<String, Object> map = new HashMap<String, Object>();
String json = gson.toJson(obj);
if (StringUtils.isEmpty(json)) {
map.put("errorCode", "sys_data_serialize_error");
map.put("errorMessage", "数据序列化异常!");
return map;
}
JSONObject jsonObj = JSON.parseObject(json);
return jsonObj;
}
public static Map<String, String> getStrMap(JSONObject jsonObj) {
Map<String, String> map = new HashMap<String, String>();
for (String key : jsonObj.keySet()) {
if (null == jsonObj.get(key)) {
map.put(key, "");
} else {
map.put(key, jsonObj.get(key).toString());
}
}
return map;
}
public static Map<String, Object> getMap(JSONObject jsonObj) {
Map<String, Object> map = new HashMap<String, Object>();
for (String key : jsonObj.keySet()) {
if (null == jsonObj.get(key)) {
map.put(key, "");
} else {
map.put(key, jsonObj.get(key));
}
}
return map;
}
public static Map<String, String> bean2StrMap(Object obj) {
if (null == obj) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
String json = gson.toJson(obj);
if (StringUtils.isEmpty(json)) {
map.put("errorCode", "sys_data_serialize_error");
map.put("errorMessage", "数据序列化异常!");
return map;
}
JSONObject jsonObj = JSON.parseObject(json);
map = getStrMap(jsonObj);
return map;
}
public static Object map2Bean(Class<?> clazz, Map<String, String> map) {
String json = gson.toJson(map);
return JSON.parseObject(json, clazz);
}
public static JSONObject bean2JSON(Object obj) {
return JSON.parseObject(gson.toJson(obj));
}
public static <T> T beanCopy(Object obj, Class<T> clazz) {
return JSON.parseObject(gson.toJson(obj), clazz);
}
public static String bean2JsonStr(Object obj) {
return gson.toJson(obj);
}
public static JSONArray sortJsonArrayByDate(JSONArray jsonArr, String dateName) {
int size = jsonArr.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size - i - 1; j++) {
JSONObject jObj = (JSONObject) jsonArr.get(j);
JSONObject j_Obj = (JSONObject) jsonArr.get(j + 1);
if (jObj.getString(dateName).compareTo(j_Obj.getString(dateName)) < 0) {
jsonArr.set(j, j_Obj);
jsonArr.set(j + 1, jObj);
}
}
}
return jsonArr;
}
@SuppressWarnings("unchecked")
public static Map<String, Object> string2Map(String str) {
Map<String, Object> map = new HashMap<String, Object>();
map = gson.fromJson(str, map.getClass());
return map;
}
public static void removeAttrs(JSONObject json, String[] removeElements) {
if (json == null || removeElements == null || removeElements.length == 0) {
return;
}
for (String element : removeElements) {
json.remove(element);
}
}
public static BigDecimal getBigDecimalNotNull(JSONObject dstJson, String jsonKeyName) {
if (dstJson == null) {
return BigDecimal.ZERO;
} else {
BigDecimal bigDecimal = dstJson.getBigDecimal(jsonKeyName);
return bigDecimal == null ? BigDecimal.ZERO : bigDecimal;
}
}
}