refactor(接口定义): 操作历史代码优化

This commit is contained in:
fit2-zhao 2021-10-20 11:01:43 +08:00 committed by fit2-zhao
parent 66abe978fe
commit 60f41171b2
43 changed files with 126 additions and 499 deletions

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.dff; package io.metersphere.log.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
@ -7,9 +7,8 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler; import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler; import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Body;
import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.utils.diff.json.JacksonDiff;
import io.metersphere.log.utils.json.diff.JacksonDiff; import io.metersphere.log.utils.diff.json.JsonDiff;
import io.metersphere.log.utils.json.diff.JsonDiff;
import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.api.DefinitionReference; import io.metersphere.log.vo.api.DefinitionReference;
@ -21,8 +20,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
public class ApiDefinitionDiffUtil { public class ApiDefinitionDiffUtil {
static final String JSON_START = "{\"root\":"; public static final String JSON_START = "{\"root\":";
static final String JSON_END = "}"; public static final String JSON_END = "}";
public static String diffResponse(String newValue, String oldValue) { public static String diffResponse(String newValue, String oldValue) {
Map<String, String> diffMap = new LinkedHashMap<>(); Map<String, String> diffMap = new LinkedHashMap<>();

View File

@ -7,8 +7,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.log.utils.dff.ApiDefinitionDiffUtil; import io.metersphere.log.utils.diff.json.GsonDiff;
import io.metersphere.log.utils.json.diff.GsonDiff;
import io.metersphere.log.vo.DetailColumn; import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.StatusReference; import io.metersphere.log.vo.StatusReference;
@ -19,6 +18,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ReflexObjectUtil { public class ReflexObjectUtil {
static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
public static List<DetailColumn> getColumns(Object obj, Map<String, String> columns) { public static List<DetailColumn> getColumns(Object obj, Map<String, String> columns) {
List<DetailColumn> columnList = new LinkedList<>(); List<DetailColumn> columnList = new LinkedList<>();
@ -30,7 +30,45 @@ public class ReflexObjectUtil {
if (StringUtils.isNotEmpty(dffValue)) { if (StringUtils.isNotEmpty(dffValue)) {
dffColumns = Arrays.asList(dffValue.split(",")); dffColumns = Arrays.asList(dffValue.split(","));
} }
// 得到类对象 // 得到类中的所有属性集合
List<Field[]> fields = getFields(obj);
try {
for (Field[] fs : fields) {
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true);
if (columns.containsKey(f.getName())) {
Object val = f.get(obj);
if (StatusReference.statusMap.containsKey(String.valueOf(val))) {
val = StatusReference.statusMap.get(String.valueOf(val));
}
DetailColumn column = new DetailColumn(columns.get(f.getName()), f.getName(), val, "");
if (dffColumns.contains(f.getName())) {
column.setDepthDff(true);
column.setOriginalValue(formatJson(val));
}
columnList.add(column);
}
}
}
} catch (Exception e) {
LogUtil.error(e);
}
List<String> keys = columns.keySet().stream().collect(Collectors.toList());
ReflexObjectUtil.order(keys, columnList);
return columnList;
}
static Object formatJson(Object val) {
try {
JsonObject jsonObject = gson.fromJson(String.valueOf(val), JsonObject.class);
return gson.toJson(jsonObject);
} catch (Exception e) {
return val;
}
}
static List<Field[]> getFields(Object obj) {
Class clazz = obj.getClass(); Class clazz = obj.getClass();
// 得到类中的所有属性集合 // 得到类中的所有属性集合
List<Field[]> fields = new LinkedList<>(); List<Field[]> fields = new LinkedList<>();
@ -42,40 +80,7 @@ public class ReflexObjectUtil {
// 获得父类的字节码对象 // 获得父类的字节码对象
clazz = clazz.getSuperclass(); clazz = clazz.getSuperclass();
} }
for (Field[] fs : fields) { return fields;
for (int i = 0; i < fs.length; i++) {
Field f = fs[i];
f.setAccessible(true);
try {
if (columns.containsKey(f.getName())) {
Object val = f.get(obj);
if (val != null && StatusReference.statusMap.containsKey(val.toString())) {
val = StatusReference.statusMap.get(val.toString());
}
DetailColumn column = new DetailColumn(columns.get(f.getName()), f.getName(), val, "");
if (dffColumns.contains(f.getName())) {
column.setDepthDff(true);
if (val != null) {
try {
// 格式化
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonObject jsonObject = gson.fromJson(val.toString(), JsonObject.class);
column.setOriginalValue(gson.toJson(jsonObject));
} catch (Exception e) {
}
}
}
columnList.add(column);
}
} catch (Exception e) {
LogUtil.error(e);
}
}
}
List<String> keys = columns.keySet().stream().collect(Collectors.toList());
ReflexObjectUtil.order(keys, columnList);
return columnList;
} }
public static List<DetailColumn> getColumns(Object obj) { public static List<DetailColumn> getColumns(Object obj) {
@ -83,18 +88,8 @@ public class ReflexObjectUtil {
if (obj == null) { if (obj == null) {
return columnList; return columnList;
} }
// 得到类对象
Class clazz = obj.getClass();
// 得到类中的所有属性集合 // 得到类中的所有属性集合
List<Field[]> fields = new LinkedList<>(); List<Field[]> fields = getFields(obj);
// 遍历所有父类字节码对象
while (clazz != null) {
// 获取字节码对象的属性对象数组
Field[] declaredFields = clazz.getDeclaredFields();
fields.add(declaredFields);
// 获得父类的字节码对象
clazz = clazz.getSuperclass();
}
for (Field[] fs : fields) { for (Field[] fs : fields) {
for (int i = 0; i < fs.length; i++) { for (int i = 0; i < fs.length; i++) {
Field f = fs[i]; Field f = fs[i];
@ -156,8 +151,8 @@ public class ReflexObjectUtil {
column.setNewValue(newColumns.get(i).getOriginalValue()); column.setNewValue(newColumns.get(i).getOriginalValue());
if (StringUtils.isNotEmpty(originalColumns.get(i).getColumnName()) && originalColumns.get(i).getColumnName().equals("tags")) { if (StringUtils.isNotEmpty(originalColumns.get(i).getColumnName()) && originalColumns.get(i).getColumnName().equals("tags")) {
GsonDiff diff = new GsonDiff(); GsonDiff diff = new GsonDiff();
String oldTags = "{\"root\":" + ((originalColumns.get(i) != null && originalColumns.get(i).getOriginalValue() != null) ? originalColumns.get(i).getOriginalValue().toString() : "\"\"") + "}"; String oldTags = ApiDefinitionDiffUtil.JSON_START + ((originalColumns.get(i) != null && originalColumns.get(i).getOriginalValue() != null) ? originalColumns.get(i).getOriginalValue().toString() : "\"\"") + ApiDefinitionDiffUtil.JSON_END;
String newTags = "{\"root\":" + ((newColumns.get(i) != null && newColumns.get(i).getOriginalValue() != null) ? newColumns.get(i).getOriginalValue().toString() : "\"\"") + "}"; String newTags = ApiDefinitionDiffUtil.JSON_START + ((newColumns.get(i) != null && newColumns.get(i).getOriginalValue() != null) ? newColumns.get(i).getOriginalValue().toString() : "\"\"") + ApiDefinitionDiffUtil.JSON_END;
String diffStr = diff.diff(oldTags, newTags); String diffStr = diff.diff(oldTags, newTags);
String diffValue = diff.apply(newTags, diffStr); String diffValue = diff.apply(newTags, diffStr);
column.setDiffValue(diffValue); column.setDiffValue(diffValue);
@ -174,17 +169,13 @@ public class ReflexObjectUtil {
column.setDiffValue(ApiDefinitionDiffUtil.diffResponse(newValue, oldValue)); column.setDiffValue(ApiDefinitionDiffUtil.diffResponse(newValue, oldValue));
} }
} else { } else {
try { String newValue = column.getNewValue().toString();
String newValue = column.getNewValue().toString(); if (StringUtils.isNotEmpty(newValue)) {
if (StringUtils.isNotEmpty(newValue)) { column.setNewValue(newValue.replaceAll("\\n", " "));
column.setNewValue(newValue.replaceAll("\\n", " ")); }
} String oldValue = column.getOriginalValue().toString();
String oldValue = column.getOriginalValue().toString(); if (StringUtils.isNotEmpty(oldValue)) {
if (StringUtils.isNotEmpty(oldValue)) { column.setOriginalValue(oldValue.replaceAll("\\n", " "));
column.setOriginalValue(oldValue.replaceAll("\\n", " "));
}
} catch (Exception e) {
} }
} }
comparedColumns.add(column); comparedColumns.add(column);

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
class ArrNode extends Node { class ArrNode extends Node {

View File

@ -1,9 +1,9 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.gson.GsonWrapper; import io.metersphere.log.utils.diff.json.jsonwrap.gson.GsonWrapper;
public class GsonDiff extends JsonDiff { public class GsonDiff extends JsonDiff {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
import java.util.*; import java.util.*;

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
/* /*
Copyright (c) 2009, incava.org Copyright (c) 2009, incava.org

View File

@ -1,6 +1,6 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
import io.metersphere.log.utils.json.diff.jsonwrap.jackson.JacksonWrapper; import io.metersphere.log.utils.diff.json.jsonwrap.jackson.JacksonWrapper;
public class JacksonDiff extends JsonDiff { public class JacksonDiff extends JsonDiff {

View File

@ -1,12 +1,12 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.Wrapper; import io.metersphere.log.utils.diff.json.jsonwrap.Wrapper;
import io.metersphere.log.utils.json.diff.jsonwrap.gson.GsonJsonPrimitive; import io.metersphere.log.utils.diff.json.jsonwrap.gson.GsonJsonPrimitive;
import io.metersphere.log.utils.json.diff.jsonwrap.jackson.JacksonJsonObject; import io.metersphere.log.utils.diff.json.jsonwrap.jackson.JacksonJsonObject;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.*; import java.util.*;

View File

@ -1,9 +1,9 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.Wrapper; import io.metersphere.log.utils.diff.json.jsonwrap.Wrapper;
import java.util.*; import java.util.*;
import java.util.logging.Level; import java.util.logging.Level;

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
abstract class Node implements Cloneable { abstract class Node implements Cloneable {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
class ObjNode extends Node { class ObjNode extends Node {

View File

@ -0,0 +1,5 @@
package io.metersphere.log.utils.diff.json;
enum Oper {
INSERT, DELETE, SET
}

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
class Root extends Node { class Root extends Node {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff; package io.metersphere.log.utils.diff.json;
/** /**

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
/** /**
* Common abstaraction for a json array. * Common abstaraction for a json array.

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
/** /**
* Common abstraction for json elements. * Common abstraction for json elements.

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
/** /**
* Common abstraction for json null. * Common abstraction for json null.

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
import java.util.Collection; import java.util.Collection;
import java.util.Map.Entry; import java.util.Map.Entry;

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
/** /**
* Common abstraction for json primitive values. * Common abstraction for json primitive values.

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap; package io.metersphere.log.utils.diff.json.jsonwrap;
/** /**
* Factory wrapper interface for multiple json implementations. * Factory wrapper interface for multiple json implementations.

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@ -6,8 +6,8 @@ import java.util.ArrayList;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
public class GsonJsonArray extends GsonJsonElement implements JzonArray { public class GsonJsonArray extends GsonJsonElement implements JzonArray {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
public class GsonJsonElement implements JzonElement { public class GsonJsonElement implements JzonElement {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import com.google.gson.JsonNull; import com.google.gson.JsonNull;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonNull; import io.metersphere.log.utils.diff.json.jsonwrap.JzonNull;
public class GsonJsonNull extends GsonJsonElement implements JzonNull { public class GsonJsonNull extends GsonJsonElement implements JzonNull {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -8,8 +8,8 @@ import java.util.Set;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
public class GsonJsonObject extends GsonJsonElement implements JzonObject { public class GsonJsonObject extends GsonJsonElement implements JzonObject {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonPrimitive; import io.metersphere.log.utils.diff.json.jsonwrap.JzonPrimitive;
public class GsonJsonPrimitive extends GsonJsonElement implements JzonPrimitive { public class GsonJsonPrimitive extends GsonJsonElement implements JzonPrimitive {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap.gson; package io.metersphere.log.utils.diff.json.jsonwrap.gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -6,10 +6,10 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.Wrapper;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.Wrapper; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
public class GsonWrapper implements Wrapper { public class GsonWrapper implements Wrapper {

View File

@ -1,10 +1,10 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode; import org.codehaus.jackson.node.ArrayNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
public class JacksonJsonArray extends JacksonJsonElement implements JzonArray { public class JacksonJsonArray extends JacksonJsonElement implements JzonArray {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
public class JacksonJsonElement implements JzonElement { public class JacksonJsonElement implements JzonElement {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import org.codehaus.jackson.node.NullNode; import org.codehaus.jackson.node.NullNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonNull; import io.metersphere.log.utils.diff.json.jsonwrap.JzonNull;
public class JacksonJsonNull extends JacksonJsonElement implements JzonNull { public class JacksonJsonNull extends JacksonJsonElement implements JzonNull {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -8,8 +8,8 @@ import java.util.Map.Entry;
import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode; import org.codehaus.jackson.node.ObjectNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
public class JacksonJsonObject extends JacksonJsonElement implements JzonObject { public class JacksonJsonObject extends JacksonJsonElement implements JzonObject {

View File

@ -1,8 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import org.codehaus.jackson.node.ValueNode; import org.codehaus.jackson.node.ValueNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonPrimitive; import io.metersphere.log.utils.diff.json.jsonwrap.JzonPrimitive;
public class JacksonJsonPrimitive extends JacksonJsonElement implements JzonPrimitive { public class JacksonJsonPrimitive extends JacksonJsonElement implements JzonPrimitive {

View File

@ -1,7 +1,8 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
import java.io.IOException; import java.io.IOException;
import io.metersphere.log.utils.diff.json.jsonwrap.Wrapper;
import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException; import org.codehaus.jackson.JsonProcessingException;
@ -10,10 +11,9 @@ import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode; import org.codehaus.jackson.node.ObjectNode;
import org.codehaus.jackson.node.ValueNode; import org.codehaus.jackson.node.ValueNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray; import io.metersphere.log.utils.diff.json.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement; import io.metersphere.log.utils.diff.json.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject; import io.metersphere.log.utils.diff.json.jsonwrap.JzonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.Wrapper;
public class JacksonWrapper implements Wrapper { public class JacksonWrapper implements Wrapper {

View File

@ -1,4 +1,4 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson; package io.metersphere.log.utils.diff.json.jsonwrap.jackson;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class JacksonWrapperException extends RuntimeException { public class JacksonWrapperException extends RuntimeException {

View File

@ -1,23 +0,0 @@
package io.metersphere.log.utils.json.diff;
public class DiffTest {
public static void main(String[] args) {
GsonDiff diff = new GsonDiff();
String newValue= "{\n" +
" \"username\": \"zyy\",\n" +
" \"username2\": \"zyy\",\n" +
" \"password\": \"Calong@2015\"\n" +
"}";
String oldValue = "{\n" +
" \"username\": \"zyy\",\n" +
" \"username1\": \"zyy\",\n" +
" \"password\": \"Calong@201512\"\n" +
"}";
String d = diff.diff(oldValue,newValue);
System.out.println(d);
System.out.println(diff.apply(newValue,d));
}
}

View File

@ -1,11 +0,0 @@
package io.metersphere.log.utils.json.diff;
import io.metersphere.log.utils.json.diff.jsonwrap.jackson2.Jackson2Wrapper;
public class Jackson2Diff extends JsonDiff {
public Jackson2Diff() {
super(new Jackson2Wrapper());
}
}

View File

@ -1,5 +0,0 @@
package io.metersphere.log.utils.json.diff;
enum Oper {
INSERT, DELETE, SET
}

View File

@ -1,47 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement;
public class Jackson2JsonArray extends Jackson2JsonElement implements JzonArray {
private final ArrayNode wrapped;
public Jackson2JsonArray(ArrayNode wrapped) {
super(wrapped);
this.wrapped = wrapped;
}
@Override
public int size() {
return wrapped.size();
}
@Override
public JzonElement get(int index) {
return Jackson2Wrapper.wrap(wrapped.get(index));
}
@Override
public void insert(int index, JzonElement el) {
wrapped.insert(index, (JsonNode) el.unwrap());
}
@Override
public void set(int index, JzonElement el) {
wrapped.set(index, (JsonNode) el.unwrap());
}
@Override
public void remove(int index) {
wrapped.remove(index);
}
@Override
public String toString() {
return wrapped.toString();
}
}

View File

@ -1,65 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.databind.JsonNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement;
public class Jackson2JsonElement implements JzonElement {
final JsonNode wrapped;
protected Jackson2JsonElement(JsonNode wrapped) {
this.wrapped = wrapped;
}
@Override
public boolean isJsonObject() {
return wrapped.isObject();
}
@Override
public boolean isJsonArray() {
return wrapped.isArray();
}
@Override
public boolean isJsonPrimitive() {
return wrapped.isValueNode();
}
@Override
public boolean isJsonNull() {
return wrapped.isNull();
}
@Override
public Object unwrap() {
return wrapped;
}
@Override
public String toString() {
return wrapped.toString();
}
@Override
public boolean equals(Object obj) {
return wrapped.equals(obj);
}
@Override
public int hashCode() {
return wrapped.hashCode();
}
}

View File

@ -1,20 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.databind.node.NullNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonNull;
public class Jackson2JsonNull extends Jackson2JsonElement implements JzonNull {
static final NullNode JNULL = NullNode.getInstance();
public final static Jackson2JsonNull INSTANCE = new Jackson2JsonNull();
public Jackson2JsonNull() {
super(JNULL);
}
}

View File

@ -1,98 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
public class Jackson2JsonObject extends Jackson2JsonElement implements JzonObject {
private final ObjectNode wrapped;
public Jackson2JsonObject(ObjectNode wrapped) {
super(wrapped);
this.wrapped = wrapped;
}
@Override
public boolean has(String key) {
return wrapped.has(key);
}
@Override
public void add(String key, JzonElement prop) {
wrapped.put(key, (JsonNode) prop.unwrap());
}
@Override
public void addProperty(String key, int prop) {
wrapped.put(key, prop);
}
@Override
public Collection<? extends Entry<String, JzonElement>> entrySet() {
HashSet<Entry<String, JzonElement>> jset = new HashSet<Entry<String, JzonElement>>();
for (Iterator<Entry<String, JsonNode>> i = wrapped.fields() ; i.hasNext();) {
final Entry<String, JsonNode> e = i.next();
final JzonElement el = Jackson2Wrapper.wrap(e.getValue());
jset.add(new Entry<String, JzonElement>() {
@Override
public String getKey() {
return e.getKey();
}
@Override
public JzonElement getValue() {
return el;
}
@Override
public JzonElement setValue(JzonElement value) {
throw new UnsupportedOperationException();
}
});
}
return jset;
}
@Override
public JzonElement get(String key) {
return Jackson2Wrapper.wrap(wrapped.get(key));
}
@Override
public void remove(String key) {
wrapped.remove(key);
}
@Override
public String toString() {
return wrapped.toString();
}
}

View File

@ -1,13 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.databind.node.ValueNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonPrimitive;
public class Jackson2JsonPrimitive extends Jackson2JsonElement implements JzonPrimitive {
public Jackson2JsonPrimitive(ValueNode wrapped) {
super(wrapped);
}
}

View File

@ -1,64 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ValueNode;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonArray;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonElement;
import io.metersphere.log.utils.json.diff.jsonwrap.JzonObject;
import io.metersphere.log.utils.json.diff.jsonwrap.Wrapper;
import java.io.IOException;
public class Jackson2Wrapper implements Wrapper {
private final static ObjectMapper JSON = new ObjectMapper();
public static JzonElement wrap(JsonNode el) {
if (el == null || el.isNull()) {
return Jackson2JsonNull.INSTANCE;
} else if (el.isArray()) {
return new Jackson2JsonArray((ArrayNode) el);
} else if (el.isObject()) {
return new Jackson2JsonObject((ObjectNode) el);
} else if (el.isValueNode()) {
return new Jackson2JsonPrimitive((ValueNode) el);
} else {
throw new IllegalStateException();
}
}
@Override
public JzonElement parse(String json) {
try {
JsonParser parser = JSON.getJsonFactory().createJsonParser(json);
parser.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
parser.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
return wrap(parser.readValueAsTree());
} catch (JsonProcessingException e) {
throw new Jackson2WrapperException("Failed to parse JSON", e);
} catch (IOException e) {
throw new Jackson2WrapperException("IOException parsing a String?", e);
}
}
@Override
public JzonElement wrap(Object o) {
return wrap((JsonNode) o);
}
@Override
public JzonObject createJsonObject() {
return (JzonObject) wrap(JSON.createObjectNode());
}
@Override
public JzonArray createJsonArray() {
return (JzonArray) wrap(JSON.createArrayNode());
}
}

View File

@ -1,22 +0,0 @@
package io.metersphere.log.utils.json.diff.jsonwrap.jackson2;
@SuppressWarnings("serial")
public class Jackson2WrapperException extends RuntimeException {
public Jackson2WrapperException() {
super();
}
public Jackson2WrapperException(String message, Throwable cause) {
super(message, cause);
}
public Jackson2WrapperException(String message) {
super(message);
}
public Jackson2WrapperException(Throwable cause) {
super(cause);
}
}