Skip to main content

JSON.parse source text access

Less than 1 minutefeaturees-proposal

JSON.parse source text accessopen in new window

Modules

Types

interface JSON {
  isRawJSON(O: any): boolean;
  // patched for source support
  parse(
    text: string,
    reviver?: (
      this: any,
      key: string,
      value: any,
      context: { source?: string }
    ) => any
  ): any;
  rawJSON(text: any): { rawJSON: string };
  // patched for `JSON.rawJSON` support
  stringify(
    value: any,
    replacer?:
      | Array<string | number>
      | ((this: any, key: string, value: any) => any),
    space?: string | number
  ): string | void;
}

Entry points

core-js/proposals/json-parse-with-source
core-js(-pure)/actual|full/json/is-raw-json
core-js(-pure)/actual|full/json/parse
core-js(-pure)/actual|full/json/raw-json
core-js(-pure)/actual|full/json/stringify

Example

Exampleopen in new window:

function digitsToBigInt(key, val, { source }) {
  return /^[0-9]+$/.test(source) ? BigInt(source) : val;
}
function bigIntToRawJSON(key, val) {
  return typeof val === "bigint" ? JSON.rawJSON(String(val)) : val;
}
const tooBigForNumber = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
JSON.parse(String(tooBigForNumber), digitsToBigInt) === tooBigForNumber; // true
const wayTooBig = BigInt("1" + "0".repeat(1000));
JSON.parse(String(wayTooBig), digitsToBigInt) === wayTooBig; // true
const embedded = JSON.stringify({ tooBigForNumber }, bigIntToRawJSON);
embedded === '{"tooBigForNumber":9007199254740993}'; // true