1/* 2 * Copyright (c) 2023 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:jsonparser 18 * @tc.desc:test Json.parse 19 * @tc.type: FUNC 20 * @tc.require: issue#I6BFOC 21 */ 22 23let json = JSON.parse("[ 1, 2, 3]"); 24print(json); 25let json2 = JSON.parse("[ 1 ]"); 26print(json2); 27let json3 = JSON.parse("[ ]"); 28print(json3); 29let data = { 30 "11111111" : "https://www.a.com", 31 "22222222" : "https://www.b.com", 32 "00000000" : "https://www.c.com" 33} 34let strData = JSON.stringify(data); 35let res = JSON.parse(strData); 36print(res["11111111"]); 37print(res["22222222"]); 38print(res["00000000"]); 39 40var a = `{"code": 0, "msg": "ok"}` 41function reviver(k, v) { return v; } 42var o = JSON.parse(a, reviver); 43print(o); 44 45let strData2 = "1.7976971348623157e+308"; 46let res2 = JSON.parse(strData2); 47print(res2); 48 49let strData3 = "-1.7976971348623157e+308"; 50let res3 = JSON.parse(strData3); 51print(res3); 52 53let strData4 = "123"; 54let res4 = JSON.parse(strData4); 55print(res4); 56 57try { 58 print(JSON.parse(`{"object": 42, "test":{}`)) 59} catch (error) { 60 print(error.name) 61} 62 63let strData5 = "\"\\uDC00\""; 64let res5 = JSON.parse(strData5); 65print(res5.codePointAt(0)) 66 67let strData6 = '{"a": "{\\"width\\": 18}"}' 68print(JSON.stringify(JSON.parse(strData6))) 69 70let strData7 = '{"a": "{\\"name\\": \\"张三\\"}"}' 71print(JSON.stringify(JSON.parse(strData7))) 72 73let strData8 = '{"1\\u0000":"name"}' 74print(JSON.stringify(JSON.parse(strData8))) 75 76print(JSON.parse('123.456e-789')); 77print(1 / JSON.parse('-0')); 78 79print("JSON.parse with backslash") 80print(JSON.parse('"\\"\\""')); // utf8 -> utf8 81print(JSON.parse('"\\u0055\\u0066"')); // utf8 -> utf8 82print(JSON.parse('"\\u5555\\u6666"')); // utf8 -> utf16 83print(JSON.parse('["\\"\\"","中文"]')[0]); // utf16 -> utf8 84print(JSON.parse('["\\u0055\\u0066","中文"]')[0]); // utf16 -> utf8 85print(JSON.parse('["\\u5555\\u6666","中文"]')[0]); // utf16 -> utf16 86 87const strData9 = `{"k1":"hello","k2":3}`; 88const strErr = strData9.substring(0, strData9.length - 2); 89try { 90 JSON.parse(strErr); 91} catch (err) { 92 print(err.name) 93} 94 95const strData10 = `{"k1":"hello","k2": 3}`; 96const strErr2 = strData10.substring(0, strData10.length - 2); 97try { 98 JSON.parse(strErr2); 99} catch (err) { 100 print(err.name) 101} 102 103const strData11 = `{"k1":"hello","k2":311111}`; 104const strErr3 = strData11.substring(0, strData11.length - 2); 105try { 106 JSON.parse(strErr3); 107} catch (err) { 108 print(err.name) 109}