diff --git a/ecmascript/base/json_stringifier.cpp b/ecmascript/base/json_stringifier.cpp index 151d76739285801081a82fe6f6589c437d51ccdf..c280b71a463787d2dc41916d3a83d12304f5156f 100644 --- a/ecmascript/base/json_stringifier.cpp +++ b/ecmascript/base/json_stringifier.cpp @@ -894,7 +894,8 @@ bool JsonStringifier::SerializeElements(const JSHandle &obj, const JSH if (!key.IsUndefined() && !key.IsHole()) { PropertyAttributes attr = numberDic->GetAttributes(thread_, hashIndex); if (attr.IsEnumerable()) { - JSTaggedValue numberKey = JSTaggedValue(static_cast(key.GetInt())); + JSTaggedValue numberKey = key.IsInt() ? JSTaggedValue(static_cast(key.GetInt())) : + JSTaggedValue(key.GetDouble()); sortArr.emplace_back(JSHandle(thread_, numberKey)); } } diff --git a/ecmascript/base/json_stringifier_optimized.cpp b/ecmascript/base/json_stringifier_optimized.cpp index 8a2f42c335981cfcca2b9c8bb7fd56a4a10448c9..381fb95fcb529c1ac881c9aef0053b35251fb6ae 100644 --- a/ecmascript/base/json_stringifier_optimized.cpp +++ b/ecmascript/base/json_stringifier_optimized.cpp @@ -872,7 +872,8 @@ bool JsonStringifier::SerializeElements(const JSHandle &obj, const JSH if (!key.IsUndefined() && !key.IsHole()) { PropertyAttributes attr = numberDic->GetAttributes(thread_, hashIndex); if (attr.IsEnumerable()) { - JSTaggedValue numberKey = JSTaggedValue(static_cast(key.GetInt())); + JSTaggedValue numberKey = key.IsInt() ? JSTaggedValue(static_cast(key.GetInt())) : + JSTaggedValue(key.GetDouble()); sortArr.emplace_back(JSHandle(thread_, numberKey)); } } diff --git a/test/moduletest/jsonstringifier/jsonstringifier.js b/test/moduletest/jsonstringifier/jsonstringifier.js index cec5ef74250ee2b75f688efe4739a987846bc8ac..139716367ca51b7279f4ee67a03da7c931395e99 100644 --- a/test/moduletest/jsonstringifier/jsonstringifier.js +++ b/test/moduletest/jsonstringifier/jsonstringifier.js @@ -38,6 +38,11 @@ var obj = { } assert_equal(JSON.stringify(obj),'{"2147483648":2289}'); +var obj = { + 6958012010: 2289 +} +assert_equal(JSON.stringify(obj),'{"6958012010":2289}'); + const a = new Uint32Array(0x10); let b = a.__proto__; b[1073741823] = {} diff --git a/test/sharedtest/sharedJSON/expect_output.txt b/test/sharedtest/sharedJSON/expect_output.txt index 4d3e75f1f2fb4de9e6860e5c20504b7f78941479..5a809b9333032330db5a101f684498b2de6ba5c7 100644 --- a/test/sharedtest/sharedJSON/expect_output.txt +++ b/test/sharedtest/sharedJSON/expect_output.txt @@ -137,3 +137,5 @@ true ["a1",null,1,true] ["a1",null,1,true] ["a1",1,null,true] +{"2300004177":{"number":"2300004177"}} +{"100":{"number":"100"}} diff --git a/test/sharedtest/sharedJSON/sharedJSON.ts b/test/sharedtest/sharedJSON/sharedJSON.ts index de80d84d53abffe15f20c1be7f9ea3968920721c..396b32560379a9408cdd4bb12fbb3af1c9a05fe3 100644 --- a/test/sharedtest/sharedJSON/sharedJSON.ts +++ b/test/sharedtest/sharedJSON/sharedJSON.ts @@ -553,6 +553,17 @@ function testASONStringifyMapSetAddUndefined() { print(str6); } +function testOverInt32Key() { + let jsonText = '{"2300004177":{"number":"2300004177"}}'; + let obj = JSON.parseSendable(jsonText); + let str = JSON.stringify(obj); + print(str) + let jsonText1 = '{"100":{"number":"100"}}'; + let obj1 = JSON.parseSendable(jsonText1); + let str1 = JSON.stringify(obj1); + print(str1) +} + testJSONParseSendable(); jsonRepeatCall(); testASONBigInt(); @@ -567,3 +578,4 @@ testASONStringifyMapAndSet(); testASONStringifyMapAndSetAndObj(); testASONStringifyAfterClearMapAndSet(); testASONStringifyMapSetAddUndefined(); +testOverInt32Key();