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 16 17let json = JSON.parse("[ 1, 2, 3]"); 18print(json,[1,2,3]); 19let json2 = JSON.parse("[ 1 ]"); 20print(json2,[1]); 21let json3 = JSON.parse("[ ]"); 22print(json3,[]); 23let data = { 24 "11111111" : "https://www.a.com", 25 "22222222" : "https://www.b.com", 26 "00000000" : "https://www.c.com" 27} 28let strData = JSON.stringify(data); 29let res = JSON.parse(strData); 30print(res["11111111"],'https://www.a.com'); 31print(res["22222222"],'https://www.b.com'); 32print(res["00000000"],'https://www.c.com'); 33 34var a = `{"code": 0, "msg": "ok"}` 35function reviver(k, v) { return v; } 36var o = JSON.parse(a, reviver); 37print(o.toString(),'[object Object]'); 38 39let strData2 = "1.7976971348623157e+308"; 40let res2 = JSON.parse(strData2); 41print(res2,Infinity); 42 43let strData3 = "-1.7976971348623157e+308"; 44let res3 = JSON.parse(strData3); 45print(res3,-Infinity); 46 47let strData4 = "123"; 48let res4 = JSON.parse(strData4); 49print(res4,123); 50 51try { 52 JSON.parse(`{"object": 42, "test":{}`) 53} catch (error) { 54 print(error.name,'SyntaxError') 55 56} 57 58let strData5 = "\"\\uDC00\""; 59let res5 = JSON.parse(strData5); 60print(res5.codePointAt(0),56320) 61 62let strData6 = '{"a": "{\\"width\\": 18}"}' 63print(JSON.stringify(JSON.parse(strData6)),'{"a":"{\\"width\\": 18}"}') 64 65let strData7 = '{"a": "{\\"name\\": \\"张三\\"}"}' 66print(JSON.stringify(JSON.parse(strData7)),'{"a":"{\\"name\\": \\"张三\\"}"}') 67 68let strData8 = '{"1\\u0000":"name"}' 69print(JSON.stringify(JSON.parse(strData8)),'{"1\\u0000":"name"}') 70 71print(JSON.parse('123.456e-789'),0); 72print(1 / JSON.parse('-0'),-Infinity); 73 74var string = "JSON.parse with backslash"; 75print(string,"JSON.parse with backslash"); 76print(JSON.parse('"\\"\\""'),'""'); // utf8 -> utf8 77print(JSON.parse('"\\u0055\\u0066"'),'Uf'); // utf8 -> utf8 78print(JSON.parse('"\\u5555\\u6666"'),'啕晦'); // utf8 -> utf16 79print(JSON.parse('["\\"\\"","中文"]')[0],'""'); // utf16 -> utf8 80print(JSON.parse('["\\u0055\\u0066","中文"]')[0],'Uf'); // utf16 -> utf8 81print(JSON.parse('["\\u5555\\u6666","中文"]')[0],'啕晦'); // utf16 -> utf16 82 83const strData9 = `{"k1":"hello","k2":3}`; 84const strErr = strData9.substring(0, strData9.length - 2); 85try { 86 JSON.parse(strErr); 87} catch (err) { 88 print(err.name,'SyntaxError'); 89} 90 91const strData10 = `{"k1":"hello","k2": 3}`; 92const strErr2 = strData10.substring(0, strData10.length - 2); 93try { 94 JSON.parse(strErr2); 95} catch (err) { 96 print(err.name,'SyntaxError'); 97} 98 99const strData11 = `{"k1":"hello","k2":311111}`; 100const strErr3 = strData11.substring(0, strData11.length - 2); 101try { 102 JSON.parse(strErr3); 103} catch (err) { 104 print(err.name,'SyntaxError'); 105} 106 107let jsonSingleStr = `{ 108 "a": 10, 109 "b": "hello", 110 "c": true, 111 "d": null, 112 "e": 5, 113 "f": 6, 114 "g": 7, 115 "h": 8, 116 "i": 9, 117 "j": 10, 118 "k": 11, 119 "l": 12, 120 "m": 13, 121 "n": 14, 122 "o": 15, 123 "p": 16, 124 "q": 17, 125 "r": 18, 126 "s": 19, 127 "t": 20, 128 "u": 21, 129 "v": 22, 130 "w": 23, 131 "x": 24, 132 "y": 25, 133 "z": 26, 134 "A": 27, 135 "B": 28, 136 "C": 29, 137 "D": 30, 138 "E": 31, 139 "F": 32, 140 "G": 33, 141 "H": 34, 142 "I": 35, 143 "J": 36, 144 "K": 37, 145 "L": 38, 146 "M": 39, 147 "N": 40, 148 "O": 41, 149 "P": 42, 150 "Q": 43, 151 "R": 44, 152 "S": 45, 153 "T": 46, 154 "U": 47, 155 "V": 48, 156 "W": 49, 157 "X": 50, 158 "Y": 51, 159 "Z": 52 160}`; 161 162let parsedObj = JSON.parse(jsonSingleStr); 163print(parsedObj.a, 10); 164print(parsedObj.b, 'hello'); 165print(parsedObj.c, true); 166print(parsedObj.n, 14); 167print(parsedObj.x, 24); 168print(parsedObj.C, 29); 169print(parsedObj.J, 36); 170print(parsedObj.T, 46); 171 172let numStr = `{ 173 "numberval1": 1.79e+308, 174 "numberval2": 1.7976931348623158e+309, 175 "numberval3": 5e+320, 176 "numberval4": 2.225e-308, 177 "numberval5": 2.225e-309, 178 "numberval6": 3e-320, 179 "numberval7": 5e-324, 180 "numberval8": 5e-325, 181 "numberval9": 7e-350 182}`; 183let numParsedObj = JSON.parse(numStr); 184print(numParsedObj.numberval1, 1.79e+308);// DBL_MAX 185print(numParsedObj.numberval2, Infinity);// greater than DBL_MAX, expect Infinity 186print(numParsedObj.numberval3, Infinity);// greater than DBL_MAX, expect Infinity 187print(numParsedObj.numberval4, 2.225e-308);// DBL_MIN 188print(numParsedObj.numberval5, 2.225e-309);// less than DBL_MIN 189print(numParsedObj.numberval6, 3e-320);// less than DBL_MIN 190print(numParsedObj.numberval7, 5e-324);// Number.MIN_VALUE 191print(numParsedObj.numberval8, 0);// less than Number.MIN_VALUE, expect 0 192print(numParsedObj.numberval9, 0);// less than Number.MIN_VALUE, expect 0 193 194print(2.225e-308); // 2.225e-308 195print(2.225e-309); // 2.225e-309 196print(3e-320); // 3e-320 197print(5e-324); // 5e-324 198 199{ 200 let err = {}; 201 try { 202 JSON.parse(`{"object哈哈": 42, "test":{}`) 203 } catch (error) { 204 err = error; 205 } 206 print(err.name, 'SyntaxError'); 207}