From f4cdd17a635c0a25fc05c16934f865235ce88824 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Wed, 26 Jun 2024 19:39:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0JSON.stringify=20=E5=B8=A6rep?= =?UTF-8?q?lacer=20space=E7=9A=84=E6=B5=8B=E8=AF=95=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- uni_modules/uts-tests/utssdk/JSON.uts | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/uni_modules/uts-tests/utssdk/JSON.uts b/uni_modules/uts-tests/utssdk/JSON.uts index 8539092..1c4ab59 100644 --- a/uni_modules/uts-tests/utssdk/JSON.uts +++ b/uni_modules/uts-tests/utssdk/JSON.uts @@ -181,6 +181,49 @@ export function testJSON() : Result { expect(innerObj instanceof UTSJSONObject).toEqual(true); // #endif }) + test('stringify-with-replacer', () => { + // #ifdef APP-ANDROID + let a = JSON.stringify({"x":111,"y":"aaa"}) + expect(a).toEqual('{"x":111,"y":"aaa"}'); + let a1 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ + if(key == "x"){ + return "x" + } + return value + }) + expect(a1).toEqual('{"x":"x","y":"aaa"}'); + let a2 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ + if(key == "x"){ + return "x" + } + return value + },6) + expect(a2.length).toEqual(36); + let a3 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ + if(key == "x"){ + return "x" + } + return value + },11) + expect(a3.length).toEqual(44); + + let a4 = JSON.stringify({"x":111,"y":"aaa"},function(key:any,value:any|null):any|null{ + if(key == "x"){ + return "x" + } + return value + },-11) + expect(a4.length).toEqual(19); + + let a5 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'z']) + expect(a5).toEqual('{"x":111,"z":{"x":123}}'); + + let a6 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) + expect(a6).toEqual('{"x":111,"y":"aaa","z":{"x":123}}'); + let a7 = JSON.stringify({"x":111,"y":"aaa","z":{"x":123}},["x",'y','z']) + expect(a7.length).toEqual(91); + // #endif + }) test('stringify', () => { // #ifdef APP-ANDROID -- GitLab