Skip to content

Honor multiline JSON for BoxList string input and output - #337

Open
shguild wants to merge 3 commits into
cdgriffith:developfrom
shguild:fix/json-lines-string
Open

shguild wants to merge 3 commits into
cdgriffith:developfrom
shguild:fix/json-lines-string

Conversation

@shguild

@shguild shguild commented Oct 10, 2026

Copy link
Copy Markdown

When BoxList.to_json(multiline=True) is called without a filename, it currently returns a JSON array instead of one JSON value per line. Likewise, BoxList.from_json(json_string, multiline=True) ignores the flag and raises JSONDecodeError: Extra data for multiple JSON lines. Both operations already work with files.

This change honors multiline=True for string input and output, matching the existing file behavior, including blank/comment lines and universal newlines. The default JSON array behavior stays unchanged.

from box import BoxList

values = BoxList([{"a": 1}, {"b": 2}])
text = values.to_json(multiline=True)
assert text == '{"a": 1}\n{"b": 2}'
assert BoxList.from_json(text, multiline=True) == values

Added 15 regression cases covering empty input, mixed values, Unicode line separators, CR/LF/CRLF, comments, JSON options, invalid JSON, and default array behavior. On the original code, 11 fail and 4 pass.

Validation on Windows / Python 3.12.14:

  • python -X utf8 -m pytest --cov=box -q: 174 passed, 92% total coverage. UTF-8 mode is needed for an existing TOML fixture on this Windows installation.
  • Black 24.10.0 checks pass for the three changed Python files.
  • mypy box: success (8 source files).
  • Cython translation passes for both changed modules; native compiled extensions were not built locally.
  • git diff --check passes.

Includes changelog and contributor credit as requested by CONTRIBUTING.rst. Targets develop.

Prepared with Codex (GPT-6) assistance.

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