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