bigint-raw-json-can-be-stringified.js (1143B)
1 // Copyright (C) 2023 the V8 project authors. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-json.rawjson 6 description: BigInt rawJSON can be stringified. 7 info: | 8 JSON.rawJSON ( text ) 9 10 1. Let jsonString be ? ToString(text). 11 ... 12 4. Let internalSlotsList be « [[IsRawJSON]] ». 13 5. Let obj be OrdinaryObjectCreate(null, internalSlotsList). 14 6. Perform ! CreateDataPropertyOrThrow(obj, "rawJSON", jsonString). 15 7. Perform ! SetIntegrityLevel(obj, frozen). 16 8. Return obj. 17 18 features: [BigInt, json-parse-with-source] 19 ---*/ 20 21 const tooBigForNumber = BigInt(Number.MAX_SAFE_INTEGER) + 2n; 22 const intToBigInt = (key, val, { source }) => 23 typeof val === 'number' && val % 1 === 0 ? BigInt(source) : val; 24 const roundTripped = JSON.parse(String(tooBigForNumber), intToBigInt); 25 assert.sameValue(roundTripped, tooBigForNumber); 26 27 const bigIntToRawJSON = (key, val) => 28 typeof val === 'bigint' ? JSON.rawJSON(val) : val; 29 const embedded = JSON.stringify({ tooBigForNumber }, bigIntToRawJSON); 30 assert.sameValue(embedded, '{"tooBigForNumber":9007199254740993}'); 31 32 reportCompare(0, 0);