目标
递归比较两个 JSON:对象按键差集与递归、数组按下标逐位加尾部增删、类型不匹配(对象对数组)报一条 type-changed,全部以指针路径输出。
要点
demo 对新旧配置报告 /replicas: 2 -> 4、/labels/tier 变化、/tags/2 与 /owner 新增;格式化输出一行一条可读差异。
提示
数组按下标比较无移动检测——重排长数组会报每个位移元素为 changed。
验证
node –test 五个用例覆盖标量、键增删、数组尾增与类型变化;CLI 对示例文件输出差异行。 复现命令:
node --test jsondiff.test.mjs
node jsondiff.mjs old.json new.json
预期输出要点:Given an old and new JSON document the CLI prints one diff entry per line with a JSON Pointer path and the nature of the change.
实测证据:Node 22.23.2: node –test -> 5 passed; CLI reported /replicas: 2 -> 4, /labels/tier change, added /tags/2 and /owner.