比较两个json串,即实体修改前后的差异性记录。调用方法直接使用
campareJsonObject(String oldJsonStr, String newJsonStr1)
package com.hq.base.hqscm.log.utils;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
@Component
public class CampareJson {
private static String replaceFirst(String ret, String strToReplace, String replaceWithThis) {
return ret.replaceFirst(strToReplace, replaceWithThis);
}
public static String replaceLast(String text, String strToReplace, String replaceWithThis) {
return text.replaceFirst("(?s)" + strToReplace + "(?!.*?" + strToReplace + ")", replaceWithThis);
}
public String campareJsonObject(String oldJsonStr, String newJsonStr1) {
StringBuffer sb = new StringBuffer();
oldJsonStr = StringEscapeUtils.unescapeJava(oldJsonStr);
oldJsonStr = StringEscapeUtils.unescapeJava(oldJsonStr);
JSON oldJson = JSON.parseObject(oldJsonStr);
JSON newJson = JSON.parseObject(newJsonStr1);
Map<String, Object> oldMap = new LinkedHashMap<>();
Map<String, Object> newMap = new LinkedHashMap<>();
convertJsonToMap(oldJson, "", oldMap);
convertJsonToMap(newJson, "", newMap);
Map<String, Object> differenceMap = campareMap(oldMap, newMap, 1);
Iterator<Map.Entry<String, Object>> iterator = differenceMap.entrySet().iterator();
while (iterator.hasNext()) {
LinkedList list = new LinkedList();
Map.Entry<String, Object> entry = iterator.next();
Object value = entry.getValue();
if (value instanceof Map) {
Map<String, Object> map = (Map) value;
for (Object value2 : map.values()) {
list.add(value2.toString());
}
}
String message = null;
try {
message = ConfigInfo.getValue(entry.getKey());
} catch (Exception e) {
log.error("读取配置异常,{}", e);
}
Object o = list.removeLast();
Object o1 = list.removeLast();
try {
if (entry.getKey().contains(".id")) {
continue;
}
try {
String ignore = ConfigInfo.getValue("ignore." + entry.getKey());
if (StringUtils.isNotEmpty(ignore) && ignore.equals(ignore)) {
continue;
}
}catch (Exception e){
}
if (ObjectUtil.isNotEmpty(o)) {
if (StringUtils.isNotEmpty(o.toString())) {
o = seto1(o, entry);
}
}
if (ObjectUtil.isNotEmpty(o1)) {
if (StringUtils.isNotEmpty(o1.toString())) {
o1 = seto1(o1, entry);
}
}
if((o != null && StringUtils.isEmpty(o.toString()) && (o1 != null && StringUtils.isEmpty(o1.toString())))){
continue;
}
if (o != null && StringUtils.isEmpty(o.toString())) {
o = "空";
}
if (o1 != null && StringUtils.isEmpty(o1.toString())) {
o1 = "空";
}
} catch (Exception e) {
}
if (message == null) {
sb.append("【" + entry.getKey() + "】:" + "由: 【" + o + "】" + " 修改为" + "【" + o1 + "】").append("rn");
} else {
sb.append("【" + message + "】:" + "由: 【" + o + "】" + " 修改为" + "【" + o1 + "】").append("rn");
}
}
return sb.toString();
}
public String timestampToDate(long time) throws ParseException {
String dateTime;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
long timeLong = Long.valueOf(time);
dateTime = simpleDateFormat.format(new Date(timeLong));
return dateTime;
}
private Object seto1(Object o1, Map.Entry<String, Object> entry) {
if (entry.getKey().equals("orderGoodsVo.[1].expectDate") || entry.getKey().equals("orderGoodsVo.[0].expectDate")) {
try {
o1 = timestampToDate(Long.valueOf(o1.toString()));
} catch (Exception e) {
o1 = o1.toString();
log.error("处理日志异常,{}", e);
}
}
String message = ConfigInfo.getValue(entry.getKey() + o1.toString());
if (StringUtils.isNotEmpty(message)) {
return message;
}
return o1;
}
private void convertJsonToMap(Object json, String root, Map<String, Object> resultMap) {
if (json instanceof JSONObject) {
JSONObject jsonObject = ((JSONObject) json);
Iterator iterator = jsonObject.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = jsonObject.get(key);
String newRoot = "".equals(root) ? key + "" : root + "." + key;
if (value instanceof JSONObject || value instanceof JSONArray) {
convertJsonToMap(value, newRoot, resultMap);
} else {
resultMap.put(newRoot, value);
}
}
} else if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
for (int i = 0; i < jsonArray.size(); i++) {
Object vaule = jsonArray.get(i);
String newRoot = "".equals(root) ? "[" + i + "]" : root + ".[" + i + "]";
if (vaule instanceof JSONObject || vaule instanceof JSONArray) {
convertJsonToMap(vaule, newRoot, resultMap);
} else {
resultMap.put(newRoot, vaule);
}
}
}
}
private Map<String, Object> campareMap(Map<String, Object> oldMap, Map<String, Object> newMap, Integer status) {
campareNewToOld(oldMap, newMap, status);
campareOldToNew(oldMap);
return oldMap;
}
private void campareOldToNew(Map<String, Object> oldMap) {
for (Iterator<Map.Entry<String, Object>> it = oldMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Object> item = it.next();
String key = item.getKey();
Object value = item.getValue();
int lastIndex = key.lastIndexOf(".");
if (!(value instanceof Map)) {
Map<String, Object> differenceMap = new HashMap<>();
differenceMap.put("oldValue", value);
differenceMap.put("newValue", "");
oldMap.put(key, differenceMap);
}
}
}
private void campareNewToOld(Map<String, Object> oldMap, Map<String, Object> newMap, Integer status) {
if (status.equals(1)) {
Iterator<String> iterator = oldMap.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
if (!newMap.containsKey(key)) {
iterator.remove();
}
}
}
for (Iterator<Map.Entry<String, Object>> it = newMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Object> item = it.next();
String key = item.getKey();
Object newValue = item.getValue();
Map<String, Object> differenceMap = new HashMap<>();
int lastIndex = key.lastIndexOf(".");
String lastPath = key.substring(lastIndex + 1).toLowerCase();
if (oldMap.containsKey(key)) {
Object oldValue = oldMap.get(key);
if (newValue.equals(oldValue)) {
oldMap.remove(key);
continue;
} else {
differenceMap.put("oldValue", oldValue);
differenceMap.put("newValue", newValue);
oldMap.put(key, differenceMap);
}
} else {
differenceMap.put("oldValue", "");
differenceMap.put("newValue", newValue);
oldMap.put(key, differenceMap);
}
}
}
}