提交 4f243816 编写于 作者: 杜庆泉's avatar 杜庆泉

add json stringify

上级 7f15cddb
......@@ -196,11 +196,53 @@ export function testJSON() : Result {
expect(JSON.stringify(Math.PI)).toEqual('3.141592653589793');
expect(JSON.stringify(Math.E)).toEqual('2.718281828459045');
/**
* add since 2023-09-23
* 部分出错过的示例场景
*/
const arr = [{
"$method": "collection",
"$param": ["type"] as Array<any>,
}, {
"$method": "add",
"$param": [
[{
"num": 2,
"tag": "default-tag",
} as UTSJSONObject, {
"num": 3,
"tag": "default-tag",
} as UTSJSONObject] as Array<UTSJSONObject>,
] as Array<any>,
}] as Array<UTSJSONObject>
let ret = JSON.stringify({
$db: arr
})
expect(ret).toEqual('{"$db":[{"$method":"collection","$param":["type"]},{"$method":"add","$param":[[{"num":2,"tag":"default-tag"},{"num":3,"tag":"default-tag"}]]}]}')
type Msg = {
id: string,
method: string,
params: any
}
type CallUniMethodParams = {
method : string
args : com.alibaba.fastjson.JSONArray
}
const msg = `{"id":"6fd6ca73-c313-48ac-ad30-87ff4eba2be8","method":"App.callUniMethod","params":{"method":"reLaunch","args":[{"url":"/pages/index/index"}]}}`
const jsonRet2 = JSON.parse<Msg>(msg)!
const paramsStr = JSON.stringify(jsonRet2.params)
expect(paramsStr).toEqual('{"args":[{"url":"/pages/index/index"}],"method":"reLaunch"}')
const params = JSON.parse<CallUniMethodParams>(paramsStr)!
console.warn('params', JSON.stringify(params))
expect(JSON.stringify(params)).toEqual('{"args":[{"url":"/pages/index/index"}],"method":"reLaunch"}')
})
test('invalidField', () => {
let str = "{\"a+b\":\"test1\",\"a-b\":2,\"class\":true}"
let str = "{\"a+b\" :\"test1\",\"a-b\":2,\"class\":true}"
let p = JSON.parseObject<PersonJSON>(str)
expect(p?._class).toEqual(true)
expect(p?.a_b).toEqual("test1")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册