示例工程 20-network-storage-fetch
20-network-storage-fetch/06-json-losses/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 06 · JSON 失真
**演示**:`undefined`、函数、Symbol、`Date`、`NaN`/`Infinity`、`-0`、`Map`/`Set`、`BigInt`、循环引用在 JSON 往返中的不同下场。
**怎么看**:`node scripts/serve.mjs` 后打开 <http://localhost:8080/examples/20-network-storage-fetch/06-json-losses/>,开 Console。
**预期输出**:
```text
对象里的 undefined => {"b":1}
数组里的 undefined/函数/Symbol => [null,null,null]
Date => "2026-01-01T00:00:00.000Z"
NaN / Infinity => {"nan":null,"inf":null}
-0 => {"zero":0}
Map / Set => {"map":{},"set":{}}
BigInt => TypeError: Do not know how to serialize a BigInt
循环引用 => TypeError: Converting circular structure to JSON(消息可能跨多行)
自定义 toJSON => {"x":1}
要点:JSON 只支持 null/布尔/数字/字符串/数组/普通对象;其余会被丢弃、转换或抛错。
```
**注意**:对象里的 `undefined` 被**丢弃**,数组里的被**替换成 null**——同样是 `undefined`,两种容器行为不同。`BigInt` 与循环引用会抛错,需要提前转换或 `structuredClone`(后者能处理循环与 Map/Set,但丢函数与原型)。时间通常存 ISO 字符串或时间戳,读回时显式 `new Date(...)`,不要依赖 `reviver` 的隐式魔法。需 `http://` 协议。