This document distills the key facts an automated agent should know when
working with the format-json project. It mirrors the canonical sources
already in the repository (README.md, tests, and configuration files) so
agents can act independently without re-deriving project context.
- Purpose: Provide a JSON formatter shipped both as a CLI (
format-json) and a pre-commit hook. - Unique selling point: Unlike
pretty-format-json, this tool lets callers suppress the trailing newline via the--no-eof-newlineflag while retaining all other upstream options. - Distribution: Configured as a Python package with an entry point in
format_json/main.py.
format_json/main.pyexposes the CLI logic, including flag parsing and the formatter implementation._get_pretty_formatwrapsjson.dumps, enforcing indentation, key sorting, optional top-level key prioritisation, UTF-8 handling, and the optional trailing newline.main()iterates through provided filenames, formats them, emits diffs when not autofixing, and returns0on success,1when changes are needed, or when input JSON is invalid.
tests/test_main.pycovers:- Autofix default behaviour (adds newline).
- The
--no-eof-newlineflag. - Diff output when
--autofixis omitted.
pip install format-json
format-json --autofix --no-eof-newline path/to/file.jsonImportant flags:
--autofix: Writes formatted output back to the file.--indent: Accepts either an integer or a string (e.g.,"\\t").--no-ensure-ascii: Keeps non-ASCII characters intact.--no-sort-keys: Preserves original key order.--no-eof-newline: Omits the trailing newline (new in this project).--top-keys key1,key2: Keeps the listed keys at the top when sorting.
- repo: https://github.com/jsh9/format-json
rev: <LATEST_VERSION>
hooks:
- id: format-json
args: [--autofix, --no-eof-newline]Agents may adjust args as needed; the repository mirrors the upstream hook
layout so migration requires minimal changes.
- Install dev dependencies:
pip install -e .[dev]. - Run tests:
pytest(configured viapyproject.tomlto discover undertests/). - Full suite:
toxorchestrates the test matrix plus linting/formatting. - Type checking:
mypyruns in strict mode (python_version = 3.10). - Additional tooling is configured in
muff.toml,.pre-commit-config.yaml, andtox.inias needed by CI.
- Packaging uses Hatch (
build-backend = "hatchling.build"). - Source distributions bundle code, tests, README, LICENSE, changelog, tox
config, and hook metadata (see
pyproject.tomlfor the authoritative list). - Metadata points to the GitHub repository (
jsh9/format-json) for homepage, issues, and source.
- Treat an exit code of
1from the CLI as “changes required” rather than a hard failure; rerun with--autofixwhen appropriate. - Preserve UTF-8 encoding when reading or writing JSON files.
- When modifying formatting logic, update or expand tests in
tests/test_main.pyto cover new scenarios. - Keep documentation (
README.md,AGENTS.md, changelog) in sync whenever user visible behaviour changes.