-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--bug=1026169 --user=王孝刚 [接口测试]github#24378接口测试json请求体用json-schema转换后再关闭json-schema,数据内容发生了变化 https://www.tapd.cn/55049933/s/1371684
- Loading branch information
Showing
4 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
frontend/src/business/components/common/json-schema/convert/customNum.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CustomNum{ | ||
constructor(value) { | ||
this.value = value | ||
} | ||
get() { | ||
return this.value | ||
} | ||
} | ||
export default CustomNum; |
91 changes: 91 additions & 0 deletions
91
frontend/src/business/components/common/json-schema/convert/jsonParse.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import CustomNum from './customNum'; | ||
|
||
export const jsonParse = (jsonStr) => { | ||
let index = 0; | ||
function parseValue() { | ||
let char = jsonStr[index]; | ||
if (char === '{') { | ||
return parseObject(); | ||
} else if (char === '[') { | ||
return parseArray(); | ||
} else if (char === '"') { | ||
return parseString(); | ||
} else if (char === 't' || char === 'f') { | ||
return parseBoolean(); | ||
} else if (char === 'n') { | ||
return parseNull(); | ||
} else { | ||
return parseNumber(); | ||
} | ||
} | ||
function parseObject() { | ||
let obj = {}; | ||
index++; | ||
while (jsonStr[index] !== '}') { | ||
let key = parseString(); | ||
index++; | ||
let value = parseValue(); | ||
obj[key] = value; | ||
if (jsonStr[index] === ',') { | ||
index++; | ||
} | ||
} | ||
index++; | ||
return obj; | ||
} | ||
function parseArray() { | ||
let arr = []; | ||
index++; | ||
while (jsonStr[index] !== ']') { | ||
arr.push(parseValue()); | ||
if (jsonStr[index] === ',') { | ||
index++; | ||
} | ||
} | ||
index++; | ||
return arr; | ||
} | ||
function parseString() { | ||
let str = ''; | ||
index++; | ||
while (jsonStr[index] !== '"') { | ||
str += jsonStr[index]; | ||
index++; | ||
} | ||
index++; | ||
return str; | ||
} | ||
function parseNumber() { | ||
let numStr = ''; | ||
while (/[0-9.+-]/.test(jsonStr[index])) { | ||
numStr += jsonStr[index]; | ||
index++; | ||
} | ||
if (!isInteger(numStr) || numStr.length > 15) { | ||
return new CustomNum(numStr); | ||
} | ||
return parseFloat(numStr); | ||
} | ||
function parseBoolean() { | ||
if (jsonStr[index] === 't') { | ||
index += 4; | ||
return true; | ||
} else { | ||
index += 5; | ||
return false; | ||
} | ||
} | ||
function parseNull() { | ||
index += 4; | ||
return null; | ||
} | ||
return parseValue(); | ||
}; | ||
export const isInteger = (num) => { | ||
return /^\d+$/.test(num); | ||
}; | ||
export const trimAll = (ele) => { | ||
if (typeof ele === 'string') { | ||
return ele.split(/[\t\r\f\n\s]*/g).join(''); | ||
} | ||
}; |