JSON 中的 Long Int

Tags
javascript
Created
Feb 5, 2020 2:06 AM

序列化 bigint 以及解析:

JSON.stringify({n: 11n}, (key, value) => typeof value === 'bigint' ? `${value}n` : value)

JSON.parse("{\"n\":\"11n\"}", (key, value) => typeof value === 'string' && /\d+n/.test(value) ? BigInt(value.replace('n', '')) : value)

这个提案可以解决问题:

序列化优化使用 JSON.rawJSON(value.toString)

https://github.com/tc39/proposal-json-parse-with-source

SuperMade with Super