From 4f24381623dc882d4eda49289fb02fbbdc62bba9 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Wed, 20 Sep 2023 19:30:43 +0800 Subject: [PATCH] add json stringify --- uni_modules/uts-tests/utssdk/JSON.uts | 44 ++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/uni_modules/uts-tests/utssdk/JSON.uts b/uni_modules/uts-tests/utssdk/JSON.uts index 18773e0..bcf4384 100644 --- a/uni_modules/uts-tests/utssdk/JSON.uts +++ b/uni_modules/uts-tests/utssdk/JSON.uts @@ -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, + }, { + "$method": "add", + "$param": [ + [{ + "num": 2, + "tag": "default-tag", + } as UTSJSONObject, { + "num": 3, + "tag": "default-tag", + } as UTSJSONObject] as Array, + ] as Array, + }] as Array + 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)! + + const paramsStr = JSON.stringify(jsonRet2.params) + expect(paramsStr).toEqual('{"args":[{"url":"/pages/index/index"}],"method":"reLaunch"}') + const params = JSON.parse(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(str) expect(p?._class).toEqual(true) expect(p?.a_b).toEqual("test1") -- GitLab