Skip to content

Fix comparison false negatives and ignore added fields - #2

Merged
Master-626 merged 1 commit into
mainfrom
cursor/json-diff-health-7e58
Nov 11, 2026
Merged

Master-626 merged 1 commit into
mainfrom
cursor/json-diff-health-7e58

Conversation

@Master-626

Copy link
Copy Markdown
Owner

English

Improves day-to-day use of this JSON response checker without changing default comparison results, except where the old result was wrong.

Bugs fixed

  1. Bare-string ignore lists hid real changes. ignore_fields="id" was iterated as characters, so keys i and d were ignored and id was not. ignore_paths="//" was treated as two copies of the empty-key pointer /, which could hide a difference at that key. Both arguments must now be lists of strings.
  2. A leading UTF-8 BOM worked for files and failed on stdin. load_json_stream (and therefore compare expected.json -) rejected \ufeff at the start of the text. One leading BOM is now skipped. A BOM after other characters is still invalid, and a BOM character inside a JSON string is kept.
  3. NaN and infinities were not rejected during comparison. NaN is not equal to itself, so two identical NaN values were reported as a change. Infinities compared equal. Either result can fail later when the diff is serialized. Non-finite floats and Decimal values that would be compared or included in a result now raise ValueError. Ignored fields are not inspected.
  4. HTML reports could not be saved when a key or file name contained a lone surrogate. JSON accepts \uD800 in keys, and --json-output already escaped it, but the report inserted the raw character and UTF-8 encoding failed (exit 2). Those characters are now written as \uXXXX. Ordinary text, including non-ASCII keys, is unchanged and still HTML-escaped.

Feature

--ignore-added / ignore_added=True does not report object keys or array elements that exist only in the second file. Removals, edited values, and type changes are still reported. Arrays stay index-based: inserting an element in the middle still changes later indexes. The flag is off by default and is available on both compare and report.

Also in this change

  • README now starts with a short Chinese summary and has an options table. Install, examples, exit codes, and tests stay in the English body. README.zh-CN.md mentions the new flag and BOM handling.
  • Version is 0.2.1.
  • The existing GitHub Actions workflow already runs ruff and pytest on Python 3.8, 3.10, 3.12, and 3.14 (plus Windows and macOS on 3.12). It was left in place. Classifiers now include 3.12–3.14.
  • 175 tests passed here, including coverage above the 90% gate. ruff check and ruff format --check passed. The installed-wheel smoke test passed.

Suggested GitHub repository description:

Compare saved JSON API responses in CI, with scoped ignores, exact decimals, and offline HTML reports.

中文

这次改动让仓库更好用,默认比较结果不变;只有原先结果错误的情况被修正。

发现并修复的问题

  1. 把忽略规则当成一个字符串会漏报差异。 ignore_fields="id" 会按字符遍历,于是键 i 和 d 被忽略,而 id 本身不会。ignore_paths="//" 会被当成两个空键指针 /,从而藏掉该位置的真实差异。现在这两个参数都必须是字符串列表。
  2. 文件接受 UTF-8 BOM,标准输入不接受。 从 stdin 比较带 BOM 的 JSON 会直接失败。现在开头的一个 BOM 会被跳过;出现在其他字符之后的 BOM 仍然非法,字符串内容里的 BOM 字符会保留。
  3. 比较阶段没有拒绝 NaN 和无穷大。 NaN 不等于自身,所以两个相同的 NaN 会被报成“已改变”;无穷大则会被当成相等。这两种结果都无法稳定序列化。现在,真正参与比较或写入结果的非有限 float / Decimal 会抛出 ValueError。被忽略的字段不会检查。
  4. 键名或文件名里的孤立代理项会导致 HTML 报告写不出来。 JSON 允许键里出现 \uD800,--json-output 能转义它,但报告把原字符写进 HTML,UTF-8 编码失败并返回退出码 2。现在这些字符写成 \uXXXX。普通非 ASCII 文本保持不变,并且仍然会做 HTML 转义。

新功能

--ignore-added(Python API 为 ignore_added=True)不报告只存在于第二个文件中的对象键和数组元素。删除、改值和类型变化仍会报告。数组仍按下标比较,在中间插入元素时,后面的下标仍会变化。默认关闭,compare 和 report 都支持。

其他

  • README.md 顶部增加简短中文说明,并补充选项表。README.zh-CN.md 同步了新选项和 BOM 行为。
  • 版本号为 0.2.1。
  • 现有 GitHub Actions 已在 Python 3.8、3.10、3.12、3.14 上运行 ruff 和 pytest(另有 Windows 与 macOS 的 3.12)。工作流保持不变,并补上 3.12–3.14 的分类标记。
  • 本地 175 项测试通过,覆盖率高于 90% 门槛;ruff check 与 ruff format --check 通过;安装 wheel 后的冒烟测试通过。

建议的 GitHub 仓库一句话描述:

Compare saved JSON API responses in CI, with scoped ignores, exact decimals, and offline HTML reports.

Open in Web Open in Cursor 

Reject bare-string ignore lists, non-finite numbers that cannot be
serialized, and lone surrogates that made HTML reports unwritable.
Accept a leading UTF-8 BOM on stdin. Optionally ignore keys and array
tails that exist only in the second file.

Co-authored-by: Master-626 <Master-626@users.noreply.github.com>
@Master-626
Master-626 merged commit 63f09be into main Nov 11, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants