const intRegex=/^-?\d+$/,noiseValue=/^-?\d+n+$/,originalStringify=JSON.stringify,originalParse=JSON.parse,customFormat=/^-?\d+n$/,bigIntsStringify=/([\[:])?"(-?\d+)n"($|([\\n]|\s)*(\s|[\\n])*[,\}\]])/g,noiseStringify=/([\[:])?("-?\d+n+)n("$|"([\\n]|\s)*(\s|[\\n])*[,\}\]])/g,JSONStringify=(e,r,t)=>{if("rawJSON"in JSON)return originalStringify(e,((e,t)=>"bigint"==typeof t?JSON.rawJSON(t.toString()):"function"==typeof r?r(e,t):(Array.isArray(r)&&r.includes(e),t)),t);if(!e)return originalStringify(e,r,t);const n=originalStringify(e,((e,t)=>"string"==typeof t&&noiseValue.test(t)||"bigint"==typeof t?t.toString()+"n":"function"==typeof r?r(e,t):(Array.isArray(r)&&r.includes(e),t)),t);return n.replace(bigIntsStringify,"$1$2$3").replace(noiseStringify,"$1$2$3")},featureCache=new Map,isContextSourceSupported=()=>{const e=JSON.parse.toString();if(featureCache.has(e))return featureCache.get(e);try{const r=JSON.parse("1",((e,r,t)=>!!t?.source&&"1"===t.source));return featureCache.set(e,r),r}catch{return featureCache.set(e,!1),!1}},convertMarkedBigIntsReviver=(e,r,t,n)=>{if("string"==typeof r&&customFormat.test(r))return BigInt(r.slice(0,-1));return"string"==typeof r&&noiseValue.test(r)?r.slice(0,-1):"function"!=typeof n?r:n(e,r,t)},JSONParseV2=(e,r)=>JSON.parse(e,((e,t,n)=>{const i="number"==typeof t&&(t>Number.MAX_SAFE_INTEGER||t<Number.MIN_SAFE_INTEGER),s=n&&intRegex.test(n.source);return i&&s?BigInt(n.source):"function"!=typeof r?t:r(e,t,n)})),MAX_INT=Number.MAX_SAFE_INTEGER.toString(),MAX_DIGITS=MAX_INT.length,stringsOrLargeNumbers=/"(?:\\.|[^"])*"|-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?/g,noiseValueWithQuotes=/^"-?\d+n+"$/,JSONParse=(e,r)=>{if(!e)return originalParse(e,r);if(isContextSourceSupported())return JSONParseV2(e,r);const t=e.replace(stringsOrLargeNumbers,((e,r,t,n)=>{const i='"'===e[0];if(i&&noiseValueWithQuotes.test(e))return e.substring(0,e.length-1)+'n"';const s=t||n,o=r&&(r.length<MAX_DIGITS||r.length===MAX_DIGITS&&r<=MAX_INT);return i||s||o?e:'"'+e+'n"'}));return originalParse(t,((e,t,n)=>convertMarkedBigIntsReviver(e,t,n,r)))};export{JSONStringify,JSONParse};