1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/* 17 * @tc.name:sharedJSON 18 * @tc.desc:test sharedJSON 19 * @tc.type: FUNC 20 * @tc.require: issueI9K9KB 21 */ 22 23// @ts-nocheck 24declare function print(str: any): string; 25 26const enum BigIntMode { 27 DEFAULT = 0, 28 PARSE_AS_BIGINT = 1, 29 ALWAYS_PARSE_AS_BIGINT = 2, 30}; 31 32const enum ParseReturnType { 33 OBJECT = 0, 34 MAP = 1, 35}; 36 37let input = '{"big":1122334455667788999,"small":123,"deci":1234567890.0123456,"shortExp":1.79e+308,"longExp":1.7976931348623157e+308}'; 38let input2 = '{"zerodeci1":0.0000123,"zerodeci2":0.4799123,"zerodeci3":0.7777334477383838389929292922,"zerodeci4":0.0000000000000123}'; 39 40let obj = { 41 "innerEntry": { 42 "x": 1, 43 "y": "abc", 44 "str": "innerStr", 45 }, 46 "arr": [1, 2, 3, 4, 5] 47} 48 49let arr = [1, 3, 5, 7, 9]; 50 51let obj1 = { 52 "x": 1, 53 "y": "你好" 54} 55 56function testJSONParseSendable() { 57 print("Start testJSONParseSendable") 58 let str = JSON.stringify(obj); 59 let sharedObj = JSON.parseSendable(str); 60 print("sharedObj.arr: " + sharedObj.arr); 61 print("sharedObj.innerEntry: " + sharedObj.innerEntry); 62 print("sharedObj.innerEntry.x: " + sharedObj.innerEntry.x); 63 print("sharedObj.innerEntry.y: " + sharedObj.innerEntry.y); 64 print("sharedObj.innerEntry.str: " + sharedObj.innerEntry.str); 65 66 let str1 = JSON.stringify(arr); 67 let sharedArr = JSON.parseSendable(str1); 68 print("sharedArr: " + sharedArr); 69 70 let str2 = JSON.stringify(obj1); 71 let sharedObj1 = JSON.parseSendable(str2); 72 print("sharedObj1.x: " + sharedObj1.x); 73 print("sharedObj1.y: " + sharedObj1.y); 74} 75 76function jsonRepeatCall() { 77 print("Start jsonRepeatCall") 78 let stringify1 = JSON.stringify(obj); 79 print("stringify1: " + stringify1); 80 let sharedObj = JSON.parseSendable(stringify1); 81 let stringify2 = JSON.stringify(sharedObj); 82 print("stringify2: " + stringify2); 83 let normalObj = JSON.parse(stringify2); 84 let stringify3 = JSON.stringify(normalObj); 85 print("stringify3: " + stringify3); 86 87 let stringify4 = JSON.stringify(arr); 88 print("stringify4: " + stringify4); 89 let sharedArr = JSON.parseSendable(stringify4); 90 let stringify5 = JSON.stringify(sharedArr); 91 print("stringify5: " + stringify5); 92 let normalArr = JSON.parse(stringify5); 93 let stringify6 = JSON.stringify(normalArr); 94 print("stringify6: " + stringify6); 95 96 let stringify7 = JSON.stringify(obj1); 97 print("stringify7: " + stringify7); 98 let sharedObj1 = JSON.parseSendable(stringify7); 99 let stringify8 = JSON.stringify(sharedObj1); 100 print("stringify8: " + stringify8); 101 let normalObj1 = JSON.parse(stringify8); 102 let stringify9 = JSON.stringify(normalObj1); 103 print("stringify9: " + stringify9); 104 105 let strSendable = '{"x":1, "x":2, "x":3, "x":"你好", "a":4, "x":"你好", "a":2}'; 106 let sharedObj2 = JSON.parseSendable(strSendable); 107 print("sharedObj2.x: " + sharedObj2.x); 108 print("sharedObj2.a: " + sharedObj2.a); 109 110 let stringify10 = JSON.stringify(sharedObj2); 111 print("stringify10: " + stringify10); 112} 113 114function testASONBigInt() { 115 var options1 = { 116 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 117 } 118 var obj = JSON.parseSendable(input, undefined, options1); 119 120 print(obj.small); 121 print(obj.big); 122 print((typeof obj.big === "bigint")); 123 124 var options2 = { 125 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 126 } 127 var obj2 = JSON.parseSendable(input, undefined, options2); 128 print(obj2.small); 129 print(obj2.big); 130 print((typeof obj2.small === "bigint")); 131 132 var options3 = { 133 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 134 } 135 var obj3 = JSON.parseSendable(input, undefined, options3); 136 print(obj3.deci); 137 print(obj3.shortExp); 138 print(obj3.longExp); 139 140 var options4 = { 141 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 142 } 143 var obj4 = JSON.parseSendable(input, undefined, options4); 144 var output = JSON.stringifySendable(obj4); 145 print(output); 146 147 var options5 = { 148 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 149 } 150 var obj5 = JSON.parseSendable(input, undefined, options5); 151 var output2 = JSON.stringifySendable(obj5); 152 print(output2); 153} 154 155function testJSONBigInt() { 156 var options1 = { 157 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 158 } 159 var obj = JSON.parseBigInt(input, undefined, options1); 160 161 print(obj.small); 162 print(obj.big); 163 print((typeof obj.big === "bigint")); 164 165 var options2 = { 166 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 167 } 168 var obj2 = JSON.parseBigInt(input, undefined, options2); 169 print(obj2.small); 170 print(obj2.big); 171 print((typeof obj2.small === "bigint")); 172 173 var options3 = { 174 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 175 } 176 var obj3 = JSON.parseBigInt(input, undefined, options3); 177 print(obj3.deci); 178 print(obj3.shortExp); 179 print(obj3.longExp); 180 181 var options4 = { 182 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 183 } 184 var obj4 = JSON.parseBigInt(input, undefined, options4); 185 var output = JSON.stringifyBigInt(obj4); 186 print(output); 187 188 var options5 = { 189 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 190 } 191 var obj5 = JSON.parseBigInt(input, undefined, options5); 192 var output2 = JSON.stringifyBigInt(obj5); 193 print(output2); 194 195} 196 197function testJSONNormal() { 198 var obj = JSON.parseBigInt(input, undefined, undefined); 199 print(obj.small); 200 print(obj.big); 201 print(obj.deci); 202 print(obj.shortExp); 203 print(obj.longExp); 204 print((typeof obj.small === "number")); 205 print((typeof obj.big === "number")); 206 print((typeof obj.deci === "number")); 207 print((typeof obj.shortExp === "number")); 208 print((typeof obj.longExp === "number")); 209} 210 211function testJSONreviver() { 212 let opt = { 213 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 214 } 215 try { 216 JSON.parseSendable(input, function (k, v) {return v;}); 217 } catch (error) { 218 print(error); 219 } 220 try { 221 JSON.parseSendable(input, function (k, v) {return v;}, opt); 222 } catch (error) { 223 print(error); 224 } 225} 226 227function testJSONZeroDeci() { 228 let opt1 = { 229 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 230 } 231 let opt2 = { 232 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 233 } 234 let obj1 = JSON.parseSendable(input2, undefined, opt1); 235 print(obj1.zerodeci1); 236 print(obj1.zerodeci2); 237 print(obj1.zerodeci3); 238 print(obj1.zerodeci4); 239 let obj2 = JSON.parseSendable(input2, undefined, opt2); 240 print(obj2.zerodeci1); 241 print(obj2.zerodeci2); 242 print(obj2.zerodeci3); 243 print(obj2.zerodeci4); 244 let obj3 = JSON.parseBigInt(input2, undefined, opt1); 245 print(obj3.zerodeci1); 246 print(obj3.zerodeci2); 247 print(obj3.zerodeci3); 248 print(obj3.zerodeci4); 249 let obj4 = JSON.parseBigInt(input2, undefined, opt2); 250 print(obj4.zerodeci1); 251 print(obj4.zerodeci2); 252 print(obj4.zerodeci3); 253 print(obj4.zerodeci4); 254} 255 256function testASONMap() { 257 let jsonText1 = '{"text":"ASON support MAP Test Start","largeNumber":112233445566778899,"people":{"name":"Mary","sex":"1","height":"165"}}'; 258 let options1 = { 259 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 260 parseReturnType: ParseReturnType.MAP, 261 } 262 let map = JSON.parseSendable(jsonText1, undefined, options1) as Map<string, object>; 263 264 print(map.get("text")); 265 print(map.get("largeNumber")); 266 print((typeof map.get("largeNumber") === "bigint")); 267 print(map.get("people")); 268 269 let options2 = { 270 bigIntMode: BigIntMode.DEFAULT, 271 parseReturnType: ParseReturnType.MAP, 272 } 273 274 let jsonText2 = '{'; 275 try { 276 let map2 = JSON.parseSendable(jsonText2, undefined, options2) as Map<string, object>; 277 } catch (error) { 278 print(error); 279 } 280 281 let jsonText3 = '{"city"}'; 282 try { 283 let map3 = JSON.parseSendable(jsonText3, undefined, options2) as Map<string, object>; 284 } catch (error) { 285 print(error); 286 } 287 288 let jsonText4 = '{}'; 289 let map4 = JSON.parseSendable(jsonText4, undefined, options2) as Map<string, object>; 290 print(map4); 291 292 let jsonText5 = '{"x":1, "x":2, "x":3, "x":"你好", "a":4, "x":"你好", "a":2}'; 293 let map5 = JSON.parseSendable(jsonText5, undefined, options2) as Map<string, object>; 294 print("sendableMap5 size: " + map5.size); 295 print("sendableMap5 x: " + map5.get("x")); 296 print("sendableMap5 a: " + map5.get("a")); 297 298 let jsonText6 = '{"arr": [1,2,3], "boolA":true, "boolB":false, "nullText":null}'; 299 let map6 = JSON.parseSendable(jsonText6, undefined, options2) as Map<string, object>; 300 print("sendableMap6 arr: " + map6.get("arr")); 301 print("sendableMap6 boolA: " + map6.get("boolA")); 302 print("sendableMap6 boolB: " + map6.get("boolB")); 303 print("sendableMap6 nullText: " + map6.get("nullText")); 304} 305 306function testIndexASON() 307{ 308 let asonstr1 = '{"12":"45", "67":"89"}'; 309 let asonstr2 = '{"12":"45", "67":"89", "a":"b"}'; 310 let a = JSON.parseSendable(asonstr1); 311 let b = JSON.parseSendable(asonstr2); 312 print("ASON parse asonstr1: " + a["12"]); 313 print("ASON parse asonstr2: " + b["67"]); 314 let asonstr3 = 315 '{"123":"aa", "xx":"yy", "aaa":"es", "1234":"bb", "aaa":"ee", "124":"123", "success":"true", "123":"1"}'; 316 let c = JSON.parseSendable(asonstr3); 317 let outstr = c.aaa + c.success + c[123] + c[124]; 318 print("ASON parse asonstr3: " + outstr); 319 let out3 = JSON.stringifySendable(c) 320 print(out3); 321} 322 323function testJSONBigIntZero() { 324 var opt = { 325 bigIntMode: BigIntMode.ALWAYS_PARSE_AS_BIGINT, 326 } 327 const str = '{"id":0, "bid":999}'; 328 var obj = JSON.parseBigInt(str, undefined, opt); 329 print(obj.id); 330} 331 332function testASONStringifyMapAndSet() { 333 let jsonText1 = '{"text":"ASON support MAP Test Start","largeNumber":112233445566778899,"people":{"name":"Mary","sex":"1","height":"165"}}'; 334 let options1 = { 335 bigIntMode: BigIntMode.PARSE_AS_BIGINT, 336 parseReturnType: ParseReturnType.MAP, 337 } 338 let map1 = JSON.parseSendable(jsonText1, undefined, options1); 339 let output1 = JSON.stringifySendable(map1); 340 print(output1); 341 342 let jsonText2 = '{"args1":true,"args2":false,"args3":null}'; 343 let map2 = JSON.parseSendable(jsonText2, undefined, options1); 344 let output2 = JSON.stringifySendable(map2); 345 print(output2); 346 347 let jsonText3 = '{"people":{"name":"Mary","sex":"1","height":165,"args":{"arr":[1,2,3],"check":true,"num":null}}}'; 348 let map3 = JSON.parseSendable(jsonText3, undefined, options1); 349 let output3 = JSON.stringifySendable(map3); 350 print(output3); 351 352 let jsonText4 = '{"features":{"ut":{"args":{"bizType":"SQYK","isChecked":false,"packageId":9223372036854775806}}}}'; 353 let map4 = JSON.parseSendable(jsonText4, undefined, options1); 354 let output4 = JSON.stringifySendable(map4); 355 print(output4); 356 357 let set = new Set(['foo', 'bar', 'baz']); 358 let output5 = JSON.stringifySendable(set); 359 print(output5); 360 361 let sharedSet = new SendableSet(['foo', 'bar', 'baz']); 362 let output6 = JSON.stringifySendable(sharedSet); 363 print(output6); 364 365 let map5 = new Map([['foo', 1], ['bar', 2], ['baz', 3]]); 366 let output7 = JSON.stringifySendable(map5); 367 print(output7); 368 369 let arkPrivate = globalThis.ArkPrivate; 370 var HashMap = arkPrivate.Load(arkPrivate.HashMap); 371 let hashMap = new HashMap(); 372 hashMap.set('foo', 1); 373 hashMap.set('bar', 2); 374 hashMap.set('baz', 3); 375 let output8 = JSON.stringifySendable(hashMap); 376 print(output8 != '{}'); 377 378 var HashSet = arkPrivate.Load(arkPrivate.HashSet); 379 let hashSet = new HashSet(); 380 hashSet.add('foo'); 381 hashSet.add('bar'); 382 hashSet.add('baz'); 383 let output9 = JSON.stringifySendable(hashSet); 384 print(output9 != '{}' && output9 != '[]'); 385} 386 387class SharedA { 388 v: number; 389 map: SendableMap<string, SendableArray<number>>; 390 constructor() { 391 'use sendable' 392 } 393} 394 395class A { 396 v: number; 397 map: Map<string, Array<number>>; 398 constructor() { 399 } 400} 401 402function testASONStringifyMapAndSetAndObj() { 403 let array1 = new SendableArray<number>(1, 2, 3, 4); 404 let array2 = new SendableArray<number>(3, 5, 7, 9); 405 let sharedMap = new SendableMap(); 406 sharedMap.set('arr1', array1); 407 sharedMap.set('arr2', array2); 408 let output1 = JSON.stringifySendable(sharedMap); 409 let output2 = JSON.stringify(sharedMap); 410 print(output1); 411 print(output2); 412 413 let sharedA = new SharedA(); 414 sharedA.v = 100; 415 sharedA.map = sharedMap; 416 let output3 = JSON.stringifySendable(sharedA); 417 print(output3); 418 let output4 = JSON.stringify(sharedA); 419 print(output4); 420 421 let sharedSet = new SendableSet([sharedA]); 422 let output5 = JSON.stringifySendable(sharedSet); 423 print(output5); 424 let output6 = JSON.stringify(sharedSet); 425 print(output6); 426 427 let array3 = new Array<number>(1, 2, 3, 4); 428 let array4 = new Array<number>(3, 5, 7, 9); 429 let set = new Set(); 430 let map = new Map(); 431 map.set('arr3', array3); 432 map.set('arr4', array4); 433 let output7 = JSON.stringifySendable(map); 434 print(output7); 435 let output8 = JSON.stringify(map); 436 print(output8); 437 438 let a = new A(); 439 a.v = 200; 440 a.map = map; 441 let output9 = JSON.stringifySendable(a); 442 print(output9); 443 let output10 = JSON.stringify(a); 444 print(output10); 445 set.add(a); 446 let output11 = JSON.stringifySendable(set); 447 print(output11); 448 let output12 = JSON.stringify(set); 449 print(output12); 450 451 let arkPrivate = globalThis.ArkPrivate; 452 var HashMap = arkPrivate.Load(arkPrivate.HashMap); 453 let hashMap = new HashMap(); 454 hashMap.set('set', set); 455 let output13 = JSON.stringifySendable(hashMap); 456 print(output13); 457 let output14 = JSON.stringify(hashMap); 458 print(output14); 459 460 var HashSet = arkPrivate.Load(arkPrivate.HashSet); 461 let hashSet = new HashSet(); 462 hashSet.add(hashMap); 463 let output15 = JSON.stringifySendable(hashSet); 464 print(output15); 465 let output16 = JSON.stringify(hashSet); 466 print(output16); 467} 468 469function testASONStringifyAfterClearMapAndSet() { 470 let map1 = new Map<string | null | number | boolean, string | null | number | boolean>(); 471 map1.set("a1", "A1"); 472 map1.clear(); 473 map1.set(1, 1); 474 map1.set(true, true); 475 let str1 = JSON.stringifySendable(map1); 476 print(str1); 477 478 let map2 = new SendableMap<string | null | number | boolean, string | null | number | boolean>(); 479 map2.set("a1", "A1"); 480 map2.clear(); 481 map2.set(1, 1); 482 map2.set(true, true); 483 let str2 = JSON.stringifySendable(map2); 484 print(str2); 485 486 let set1 = new Set<string | null | number | boolean>(); 487 set1.add("a1"); 488 set1.clear(); 489 set1.add(1); 490 set1.add(true); 491 let str3 = JSON.stringifySendable(set1); 492 print(str3); 493 494 let set2 = new SendableSet<string | null | number | boolean>(); 495 set2.add("a1"); 496 set2.clear(); 497 set2.add(1); 498 set2.add(true); 499 let str4 = JSON.stringifySendable(set2); 500 print(str4); 501} 502 503function testASONStringifyMapSetAddUndefined() { 504 let map1 = new Map<string | null | number | boolean | undefined, string | null | number | boolean | undefined>(); 505 map1.set("a1", 1); 506 map1.set(undefined, "a2"); 507 map1.set(null, undefined); 508 map1.set(undefined, undefined); 509 map1.set(undefined, null); 510 let str1 = JSON.stringifySendable(map1); 511 print(str1); 512 513 let map2 = new SendableMap<string | null | number | boolean | undefined, string | null | number | boolean | undefined>(); 514 map2.set("a1", 1); 515 map2.set(undefined, "a2"); 516 map2.set(null, undefined); 517 map2.set(undefined, undefined); 518 map2.set(undefined, null); 519 let str2 = JSON.stringifySendable(map2); 520 print(str2); 521 522 let arkPrivate = globalThis.ArkPrivate; 523 var HashMap = arkPrivate.Load(arkPrivate.HashMap); 524 let hashMap = new HashMap<string | null | number | boolean | undefined, string | null | number | boolean | undefined>(); 525 hashMap.set("a1", 1); 526 hashMap.set(null, undefined); 527 let str3 = JSON.stringifySendable(hashMap); 528 print(str3); 529 530 let set1 = new Set<string | null | number | boolean | undefined>(); 531 set1.add("a1"); 532 set1.add(undefined); 533 set1.add(1); 534 set1.add(true); 535 let str4 = JSON.stringifySendable(set1); 536 print(str4); 537 538 let set2 = new SendableSet<string | null | number | boolean | undefined>(); 539 set2.add("a1"); 540 set2.add(undefined); 541 set2.add(1); 542 set2.add(true); 543 let str5 = JSON.stringifySendable(set2); 544 print(str5); 545 546 var HashSet = arkPrivate.Load(arkPrivate.HashSet); 547 let hashSet = new HashSet(); 548 hashSet.add("a1"); 549 hashSet.add(undefined); 550 hashSet.add(1); 551 hashSet.add(true); 552 let str6 = JSON.stringifySendable(hashSet); 553 print(str6); 554} 555 556testJSONParseSendable(); 557jsonRepeatCall(); 558testASONBigInt(); 559testJSONBigInt(); 560testJSONNormal(); 561testJSONreviver(); 562testJSONZeroDeci(); 563testASONMap(); 564testIndexASON(); 565testJSONBigIntZero(); 566testASONStringifyMapAndSet(); 567testASONStringifyMapAndSetAndObj(); 568testASONStringifyAfterClearMapAndSet(); 569testASONStringifyMapSetAddUndefined();