fix(接口测试): 修复case保存不正常的json数据后,不显示的缺陷

--bug=1033211 --user=王孝刚 【接口测试】github#27710,接口CASE中请求体Json数据如果有红叉,保存后重新打开该CASE json格式请求体数据不显示 https://www.tapd.cn/55049933/s/1444855
This commit is contained in:
wxg0103 2023-12-18 17:08:03 +08:00 committed by 刘瑞斌
parent e1452d380e
commit 53353d3ed7
1 changed files with 92 additions and 68 deletions

View File

@ -1,4 +1,9 @@
export function formatJson (json) { export function formatJson(json) {
try {
JSON.parse(json);
} catch (e) {
return json;
}
let i = 0, let i = 0,
il = 0, il = 0,
tab = " ", tab = " ",
@ -10,133 +15,150 @@ export function formatJson (json) {
for (i = 0, il = json.length; i < il; i += 1) { for (i = 0, il = json.length; i < il; i += 1) {
currentChar = json.charAt(i); currentChar = json.charAt(i);
switch (currentChar) { switch (currentChar) {
case '{': case "{":
if (i != 0 && json.charAt(i - 1) === '$') { if (i != 0 && json.charAt(i - 1) === "$") {
newJson += currentChar; newJson += currentChar;
flag = true; flag = true;
} else if (!inString) { } else if (!inString) {
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1); newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
indentLevel += 1 indentLevel += 1;
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case '[': case "[":
if (!inString) { if (!inString) {
newJson += currentChar + "\n" + repeat(tab, indentLevel + 1); newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
indentLevel += 1 indentLevel += 1;
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case '}': case "}":
if (flag) { if (flag) {
newJson += currentChar; newJson += currentChar;
flag = false; flag = false;
} else if (!inString) { } else if (!inString) {
indentLevel -= 1; indentLevel -= 1;
newJson += "\n" + repeat(tab, indentLevel) + currentChar newJson += "\n" + repeat(tab, indentLevel) + currentChar;
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case ']': case "]":
if (!inString) { if (!inString) {
indentLevel -= 1; indentLevel -= 1;
newJson += "\n" + repeat(tab, indentLevel) + currentChar newJson += "\n" + repeat(tab, indentLevel) + currentChar;
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case ',': case ",":
if (!inString) { if (!inString) {
newJson += ",\n" + repeat(tab, indentLevel) newJson += ",\n" + repeat(tab, indentLevel);
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case ':': case ":":
if (!inString) { if (!inString) {
newJson += ": " newJson += ": ";
} else { } else {
newJson += currentChar newJson += currentChar;
} }
break; break;
case ' ': case " ":
case "\n": case "\n":
case "\t": case "\t":
if (inString) { if (inString) {
newJson += currentChar newJson += currentChar;
} }
break; break;
case '"': case '"':
if (i > 0 && json.charAt(i - 1) !== '\\') { if (i > 0 && json.charAt(i - 1) !== "\\") {
inString = !inString inString = !inString;
} }
newJson += currentChar; newJson += currentChar;
break; break;
default: default:
newJson += currentChar; newJson += currentChar;
break break;
} }
} }
return newJson; return newJson;
} }
function repeat(s, count) { function repeat(s, count) {
return new Array(count + 1).join(s) return new Array(count + 1).join(s);
} }
export function formatXml(text) { export function formatXml(text) {
//去掉多余的空格 //去掉多余的空格
text = '\n' + text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) { text =
return name + ' ' + props.replace(/\s+(\w+=)/g, " $1"); "\n" +
}); text.replace(/(<\w+)(\s.*?>)/g, function ($0, name, props) {
return name + " " + props.replace(/\s+(\w+=)/g, " $1");
});
//把注释编码 //把注释编码
text = text.replace(/<!--(.+?)-->/g, function ($0, text) { text = text.replace(/<!--(.+?)-->/g, function ($0, text) {
var ret = '<!--' + escape(text) + '-->'; var ret = "<!--" + escape(text) + "-->";
//alert(ret); //alert(ret);
return ret; return ret;
}); });
//调整格式 //调整格式
var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg; var rgx =
/\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/gm;
var nodeStack = []; var nodeStack = [];
var output = text.replace(rgx, function ($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) { var output = text.replace(
var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/'); rgx,
//alert([all,isClosed].join('=')); function (
var prefix = ''; $0,
if (isBegin == '!') { all,
prefix = getPrefix(nodeStack.length); name,
} isBegin,
else { isCloseFull1,
if (isBegin != '/') { isCloseFull2,
isFull1,
isFull2
) {
var isClosed =
isCloseFull1 == "/" ||
isCloseFull2 == "/" ||
isFull1 == "/" ||
isFull2 == "/";
//alert([all,isClosed].join('='));
var prefix = "";
if (isBegin == "!") {
prefix = getPrefix(nodeStack.length); prefix = getPrefix(nodeStack.length);
if (!isClosed) { } else {
nodeStack.push(name); if (isBegin != "/") {
prefix = getPrefix(nodeStack.length);
if (!isClosed) {
nodeStack.push(name);
}
} else {
nodeStack.pop();
prefix = getPrefix(nodeStack.length);
} }
} }
else { var ret = "\n" + prefix + all;
nodeStack.pop(); return ret;
prefix = getPrefix(nodeStack.length);
}
} }
var ret = '\n' + prefix + all; );
return ret;
});
var prefixSpace = -1; var prefixSpace = -1;
var outputText = output.substring(1); var outputText = output.substring(1);
//把注释还原并解码,调格式 //把注释还原并解码,调格式
outputText = outputText.replace(/(\s*)<!--(.+?)-->/g, function ($0, prefix, text) { outputText = outputText.replace(
if (prefix.charAt(0) == '\r') /(\s*)<!--(.+?)-->/g,
prefix = prefix.substring(1); function ($0, prefix, text) {
text = unescape(text).replace(/\r/g, '\n'); if (prefix.charAt(0) == "\r") prefix = prefix.substring(1);
var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix) + '-->'; text = unescape(text).replace(/\r/g, "\n");
//alert(ret); var ret = "\n" + prefix + "<!--" + text.replace(/^\s*/gm, prefix) + "-->";
return ret; //alert(ret);
}); return ret;
return outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n'); }
);
return outputText.replace(/\s+$/g, "").replace(/\r/g, "\r\n");
} }
/** /**
@ -147,11 +169,12 @@ export function formatXml(text) {
*/ */
export function formatTime(time, cFormat) { export function formatTime(time, cFormat) {
if (arguments.length === 0) return null; if (arguments.length === 0) return null;
if ((time + '').length === 10) { if ((time + "").length === 10) {
time = +time * 1000; time = +time * 1000;
} }
let format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}', date; let format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}",
if (typeof time === 'object') { date;
if (typeof time === "object") {
date = time; date = time;
} else { } else {
date = new Date(time); date = new Date(time);
@ -163,23 +186,24 @@ export function formatTime(time, cFormat) {
h: date.getHours(), h: date.getHours(),
i: date.getMinutes(), i: date.getMinutes(),
s: date.getSeconds(), s: date.getSeconds(),
a: date.getDay() a: date.getDay(),
}; };
return format.replace(/{([ymdhisa])+}/g, (result, key) => { return format.replace(/{([ymdhisa])+}/g, (result, key) => {
let value = formatObj[key]; let value = formatObj[key];
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]; if (key === "a")
return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
if (result.length > 0 && value < 10) { if (result.length > 0 && value < 10) {
value = '0' + value; value = "0" + value;
} }
return value || 0; return value || 0;
}); });
} }
function getPrefix(prefixIndex) { function getPrefix(prefixIndex) {
var span = ' '; var span = " ";
var output = []; var output = [];
for (var i = 0; i < prefixIndex; ++i) { for (var i = 0; i < prefixIndex; ++i) {
output.push(span); output.push(span);
} }
return output.join(''); return output.join("");
} }