From 243b16d65d2af694116f4be30ea2a6be6eb08587 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Fri, 27 Sep 2024 17:33:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20UTSJSONObject.toJSONString?= =?UTF-8?q?=20=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/KeyWord.uts | 2 +- .../uts-tests/utssdk/UTSJSONObject.uts | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/uni_modules/uts-tests/utssdk/KeyWord.uts b/uni_modules/uts-tests/utssdk/KeyWord.uts index 5ea2bb6..c294fab 100644 --- a/uni_modules/uts-tests/utssdk/KeyWord.uts +++ b/uni_modules/uts-tests/utssdk/KeyWord.uts @@ -82,7 +82,7 @@ export function testKeyWord() : Result { test('typeof', () => { let new1 = new User() expect(typeof (new1)).toEqual('object') - + //expect(typeof null).toEqual('object') // #ifdef APP-ANDROID || APP-IOS expect(typeof (123456.789)).toEqual('Double') //expect(typeof(789778979798797987979)).toEqual('number') diff --git a/uni_modules/uts-tests/utssdk/UTSJSONObject.uts b/uni_modules/uts-tests/utssdk/UTSJSONObject.uts index b1a8962..4b41a23 100644 --- a/uni_modules/uts-tests/utssdk/UTSJSONObject.uts +++ b/uni_modules/uts-tests/utssdk/UTSJSONObject.uts @@ -43,6 +43,56 @@ export function testUTSJSONObject() : Result { expect(result.toJSONObject().toJSONString()).toEqual('{"opt":"xxxx","service":{"array":[{"name":"xxx","type":0}],"name":"0xxxb34fb","type":0}}'); + + type TestData = { + num:number; + num2:number; + str:string; + } + + let data1:TestData = { + num:1, + num2:2.369, + str:'test' + } + let data2 = JSON.parse(JSON.stringify(data1)) + + expect(data2!.toJSONString()).toEqual('{"str":"test","num":1.0,"num2":2.369}') + + class TestData2 implements IJsonStringify{ + toJSON():any|null{ + return "122" + } + } + + let testData2 = TestData2() + let dataClass = UTSAndroid.getJavaClass(data2) + let data3 = { + strA:"stringA", + dataClass:dataClass, + booleanB:false, + testData:testData2 + } + + expect(data3!.toJSONString()).toEqual('{"testData":"122","strA":"stringA","dataClass":"io.dcloud.uts.UTSJSONObject","booleanB":false}') + + + type TestData4 = { + name:string, + data:any, + } + + let data4 : TestData4 = { + name:"张三", + data:data1 + } + + let data5 = { + data4 :data4 + } + + expect(data5!.toJSONString()).toEqual('{"data4":{"data":{"num":1,"num2":2.369,"str":"test"},"name":"张三"}}') + // #endif }) -- GitLab