MCPcopy Create free account
hub / github.com/zxlie/FeHelper / parseWithBigInt

Function parseWithBigInt

apps/json-format/index.js:1811–1834  ·  view source on GitHub ↗
(text)

Source from the content-addressed store, hash-verified

1809// 统一的 BigInt 安全解析(与 json-auto-utils/format-lib/worker 思路一致):
1810// 1) 只在引号外修正宽松 key;2) 为可能的超长数字加标记;3) 用 reviver 还原为 BigInt
1811function parseWithBigInt(text) {
1812 if (
1813 typeof window !== 'undefined' &&
1814 window.FHJsonAutoUtils &&
1815 typeof window.FHJsonAutoUtils.parseWithBigInt === 'function'
1816 ) {
1817 return window.FHJsonAutoUtils.parseWithBigInt(text);
1818 }
1819
1820 let fixed = normalizeLooseJSONSource(text);
1821
1822 // 标记 16 位及以上的整数(允许值后有空白,再跟 , ] } 或结尾)
1823 // 使用 offset 检查匹配位置是否在 JSON 字符串内部,避免破坏嵌套转义的 JSON 字符串
1824 fixed = fixed.replace(/([:,\[]\s*)(-?\d{16,})(\s*)(?=(?:,|\]|\}|$))/g, function(m, p1, num, sp, offset) {
1825 if (isInsideQuotedSegment(fixed, offset)) return m;
1826 return p1 + '"__BigInt__' + num + '"' + sp;
1827 });
1828 return JSON.parse(fixed, function(key, value) {
1829 if (typeof value === 'string' && value.indexOf('__BigInt__') === 0) {
1830 try { return BigInt(value.slice(10)); } catch(e) { return value.slice(10); }
1831 }
1832 return value;
1833 });
1834}

Callers 6

json-utils.test.jsFile · 0.90
syncResultActionsFunction · 0.70
applyAiPanelResultFunction · 0.70
index.jsFile · 0.70

Calls 3

normalizeLooseJSONSourceFunction · 0.70
isInsideQuotedSegmentFunction · 0.70
parseMethod · 0.45

Tested by

no test coverage detected