fix(接口测试): 修复jsonSchema数据转json顺序发生改变的缺陷

--bug=1031318 --user=王孝刚 【接口测试】请求/响应参数-json开启关闭schema-字段位置错乱
https://www.tapd.cn/55049933/s/1423573
This commit is contained in:
wxg0103 2023-10-09 10:53:13 +08:00 committed by fit2-zhao
parent 7eb6322a7e
commit 9bae4b494f
3 changed files with 38 additions and 7 deletions

View File

@ -287,7 +287,7 @@ public class JSONSchemaBuilder {
JSONSchemaParser.toJsonString(jsonArray, list);
return list.toString();
}
return JSON.toJSONString(root.toMap());
return JSON.toJSONString(JSON.toMap(root));
}
public static String generator(String jsonSchema) {

View File

@ -13,10 +13,7 @@ import org.json.JSONObject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
public class JSONSchemaParser {
@ -241,7 +238,7 @@ public class JSONSchemaParser {
toJsonString(jsonArray, list);
json = list.toString();
} else {
json = JSON.toJSONString(root.toMap());
json = JSON.toJSONString(JSON.toMap(root));
}
if (MapUtils.isNotEmpty(processMap)) {
for (String str : processMap.keySet()) {
@ -254,7 +251,7 @@ public class JSONSchemaParser {
public static void toJsonString(JSONArray jsonArray, List<String> list) {
jsonArray.forEach(element -> {
if (element instanceof JSONObject o) {
list.add(JSON.toJSONString(o.toMap()));
list.add(JSON.toJSONString(JSON.toMap(o)));
} else if (element instanceof JSONArray o) {
List<String> aa = new LinkedList<>();
toJsonString(o, aa);

View File

@ -13,9 +13,13 @@ import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -112,4 +116,34 @@ public class JSON {
throw new RuntimeException(e);
}
}
public static Map<String, Object> toMap(JSONObject json) {
Map<String, Object> map = new LinkedHashMap<>();
Iterator<String> keys = json.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = json.get(key);
if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
} else if (value instanceof JSONArray) {
value = toList((JSONArray) value);
}
map.put(key, value);
}
return map;
}
public static Object toList(JSONArray array) {
Object[] list = new Object[array.length()];
for (int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if (value instanceof JSONObject) {
value = toMap((JSONObject) value);
} else if (value instanceof JSONArray) {
value = toList((JSONArray) value);
}
list[i] = value;
}
return list;
}
}