1/* 2 * Copyright (c) 2025 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 16import {json} from "@ohos.util.json"; 17 18function main() { 19 testStringifyParseBigInt() 20 testStringifyParseOptOne() 21 testStringifyParseOptZero() 22 testStringifyParseBigIntWithReviver() 23 testParseNoBigInt() 24 testParseNoBigIntParseAsBigint() 25 testParseNoBigIntAlwaysParseAsBigint() 26} 27 28export class User {} 29 30class UserWithFields extends User { 31 id: BigInt = new BigInt() 32 g: Boolean = false 33 name: String = "" 34} 35 36class SuperUser extends UserWithFields { 37 users: FixedArray<UserWithFields> = [] 38} 39 40function testStringifyParseBigInt(): void { 41 let classType = Type.of(new SuperUser() as Object) 42 let str = "{\"id\":10,\"g\":true,\"name\":\"Name\",\"users\":[{\"id\":12,\"g\":true,\"name\":\"Name1\"},{\"id\":13,\"g\":false,\"name\":\"Name2\"}]}"; 43 let x = json.stringify(json.parse<SuperUser>(str, classType, undefined, {bigIntMode: json.BigIntMode.ALWAYS_PARSE_AS_BIGINT} as json.ParseOptions) as SuperUser) 44 assertEQ(x, str) 45} 46 47function testStringifyParseOptOne(): void { 48 let classType = Type.of(new SuperUser() as Object) 49 let str = "{\"id\":10,\"g\":true,\"name\":\"Name\",\"users\":[{\"id\":12,\"g\":true,\"name\":\"Name1\"},{\"id\":13,\"g\":false,\"name\":\"Name2\"}]}"; 50 expectThrow(() => {json.parse<SuperUser>(str, classType, undefined, {bigIntMode: json.BigIntMode.PARSE_AS_BIGINT} as json.ParseOptions)}) 51} 52 53function testStringifyParseOptZero(): void { 54 let classType = Type.of(new SuperUser() as Object) 55 let str = "{\"id\":10,\"g\":true,\"name\":\"Name\",\"users\":[{\"id\":12,\"g\":true,\"name\":\"Name1\"},{\"id\":13,\"g\":false,\"name\":\"Name2\"}]}"; 56 expectThrow(() => {json.parse<SuperUser>(str, classType, undefined, {bigIntMode: json.BigIntMode.DEFAULT} as json.ParseOptions)}) 57} 58 59function testStringifyParseBigIntWithReviver(): void { 60 let classType = Type.of(new SuperUser() as Object) 61 let reviver = (k: string, v: NullishType) => { 62 if (k === "id") { 63 return new BigInt(1) 64 } 65 return v 66 } 67 let str = "{\"id\":10,\"g\":true,\"name\":\"Name\",\"users\":[{\"id\":12,\"g\":true,\"name\":\"Name1\"},{\"id\":13,\"g\":false,\"name\":\"Name2\"}]}"; 68 let expected_str = "{\"id\":1,\"g\":true,\"name\":\"Name\",\"users\":[{\"id\":1,\"g\":true,\"name\":\"Name1\"},{\"id\":1,\"g\":false,\"name\":\"Name2\"}]}"; 69 let x = json.stringify(json.parse<SuperUser>(str, classType, reviver, {bigIntMode: json.BigIntMode.ALWAYS_PARSE_AS_BIGINT} as json.ParseOptions) as SuperUser) 70 assertEQ(x, expected_str) 71} 72 73 74class NumbersTest extends User { 75 val: Number = 1 76 str: String = "" 77 bul: Boolean = false 78} 79 80function testParseNoBigInt() { 81 let classType = Type.of(new NumbersTest()) 82 let str = "{\"val\":123444444421,\"str\":\"true\",\"bul\":false}" 83 let x = json.stringify(json.parse<NumbersTest>(str, classType, undefined, {bigIntMode: json.BigIntMode.DEFAULT} as json.ParseOptions) as NumbersTest) 84 assertEQ(x, str) 85} 86 87function testParseNoBigIntParseAsBigint() { 88 let classType = Type.of(new NumbersTest()) 89 let str = "{\"val\":123444444421,\"str\":\"true\",\"bul\":false}" 90 let x = json.stringify(json.parse<NumbersTest>(str, classType, undefined, {bigIntMode: json.BigIntMode.PARSE_AS_BIGINT} as json.ParseOptions) as NumbersTest) 91 assertEQ(x, str) 92} 93 94function testParseNoBigIntAlwaysParseAsBigint() { 95 let classType = Type.of(new NumbersTest()) 96 let str = "{\"val\":123444444421,\"str\":\"true\",\"bul\":false}" 97 expectThrow(() => {json.parse<NumbersTest>(str, classType, undefined, {bigIntMode: json.BigIntMode.ALWAYS_PARSE_AS_BIGINT} as json.ParseOptions) as NumbersTest}) 98} 99