Skip to content

jsondiff/jsonpatch crash with a raw traceback on UTF-8 BOM files - #259

Open
Qrytics wants to merge 1 commit into
stefankoegl:masterfrom
Qrytics:pilot/slot-41-fix
Open

Qrytics wants to merge 1 commit into
stefankoegl:masterfrom
Qrytics:pilot/slot-41-fix

Conversation

@Qrytics

@Qrytics Qrytics commented Nov 10, 2026 •

Copy link
Copy Markdown

Summary

jsondiff/jsonpatch crash with a raw traceback on UTF-8 BOM files (e.g. saved by Windows Notepad).

Repro

python -c "open('"'"'bom1.json'"'"','"'"'w'"'"',encoding='"'"'utf-8-sig'"'"').write('"'"'{\"a\": 1}'"'"')"
python -c "open('"'"'bom2.json'"'"','"'"'w'"'"',encoding='"'"'utf-8-sig'"'"').write('"'"'{\"a\": 2}'"'"')"
jsondiff bom1.json bom2.json

Expected: prints the patch [{"op": "replace", "path": "/a", "value": 2}] and exits 1 (documented diff-like contract). Actual before fix: json.decoder.JSONDecodeError: Unexpected UTF-8 BOM (decode using utf-8-sig), exit 1 with traceback. Same for jsonpatch with BOM inputs.

Root cause

jsonpatch_cli.py lines 16, 17 (jsondiff FILE1/FILE2), 54, 56 (jsonpatch ORIGINAL/PATCH): all four CLI inputs use argparse.FileType('"'"'r'"'"', encoding='"'"'utf-8'"'"'), which preserves the BOM as U+FEFF and makes json.load raise. utf-8-sig decodes BOM-less files byte-identically.

Fix

encoding='"'"'utf-8'"'"' -> encoding='"'"'utf-8-sig'"'"' in the four argparse.FileType declarations in jsonpatch_cli.py. No other code touched.

Tests

  • New: tests.py::CommandLineTests::test_jsondiff_utf8_bom_files, ::test_jsonpatch_utf8_bom_files (existing ScriptRunner + temp-dir unittest style).
  • Run: python -m pytest tests.py -k "utf8_bom or CommandLineTests" -q -> 27 passed, 1 skipped, 22 subtests passed (skip is pre-existing POSIX-only permissions test).
  • Full tests.py: 336 passed, 1 skipped, plus 1 pre-existing failure in DocumentationTests::test_tutorial that fails identically on the untouched tree (verified via git stash).
  • Lint: flake8 --select=E9,F63,F7,F82 clean on both touched files.

@Qrytics Qrytics changed the title placeholder jsondiff/jsonpatch crash with a raw traceback on UTF-8 BOM files Nov 10, 2026

This branch has not been deployed

No deployments
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.

1 participant