Skip to content

Do not emit BJData Draft-3 byte marker in Draft 2 - #5437

Open
22elix3r wants to merge 1 commit into
nlohmann:developfrom
22elix3r:fix/5404-bjdata-byte-draft2
Open

Do not emit BJData Draft-3 byte marker in Draft 2#5437
22elix3r wants to merge 1 commit into
nlohmann:developfrom
22elix3r:fix/5404-bjdata-byte-draft2

Conversation

@22elix3r

Copy link
Copy Markdown
Contributor

Summary

`to_bjdata()` mapped ndarray `ArrayType = "byte"` to marker `B` even with the default Draft-2 version. `B` is Draft-3-only, so that stream is invalid for Draft-2 readers and does not round-trip as an annotated array.

Related Issue

Fixes #5404

Changes Made

  • If the ndarray type is `byte` and the version is not Draft 3, fall back to plain object encoding (`write_bjdata_ndarray` returns true)
  • Test that Draft 2 output contains no `B` and parses as an object; Draft 3 still emits `B`

Testing

Commands executed:

  • `python3 tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json.json -s .`
  • `cmake -S . -B build -DJSON_BuildTests=ON -DCMAKE_BUILD_TYPE=Debug`
  • `cmake --build build --target test-bjdata_cpp11`
  • `./tests/test-bjdata_cpp11 --no-skip -tce='downloaded' -tc='5404'`

Results:

  • 1 passed, 3 assertions

Notes

uint8 remains the Draft-2 way to encode 8-bit values. Draft 3 is unchanged.

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running `make amalgamate`.

_ArrayType_ byte mapped to B even when to_bjdata used the default
Draft-2 version. B is Draft-3 only, so fall back to a plain object
unless draft3 is requested.

Signed-off-by: elix3r <157088510+22elix3r@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 27, 2026 02:44
@22elix3r
22elix3r requested a review from nlohmann as a code owner August 27, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copy link
Copy Markdown
Owner

The Draft-2 gate works — I checked the bytes on this branch:

draft2: 7b ... 5f 41 72 72 61 79 54 79 70 65 5f ...   -> plain object, no 'B'
draft3: 5b 24 42 23 5b 69 02 5d 01 02                 -> '[$B#[i2]..' as before

Two things from #5404 are still open, though.

1. The second half of the issue. Its title is "and the value does not round-trip", and the body raises it as a separate defect:

Separately, unlike every other _ArrayType_ (e.g. uint8, which round-trips back to the annotated object), the result parses back as a binary value, so to_bjdata/from_bjdata changes the value.

That is unchanged in Draft 3 — json::from_bjdata(json::to_bjdata(j, false, false, draft3)) still gives {"bytes":[1,2],"subtype":null} rather than the original annotation object. The PR's own Draft-3 assertion only greps for a B byte and doesn't check the round trip. Fine to declare that inherent and out of scope, but then it should be said so on the issue rather than closed by this PR.

2. Docs. docs/mkdocs/docs/features/binary_formats/bjdata.md:127 still lists the accepted _ArrayType_ values as

double, char, or byte,

with no Draft-3 caveat, even though byte now silently falls back to object encoding under the default version. Compare #5438, which does update this same paragraph for its change.


Generated by Claude Code

@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated and/or formatted correctly.

📎 A ready-to-apply patch is attached to the failed workflow run as the amalgamation-patch artifact. Download it, then apply it locally from the repository root with:

git apply amalgamation.patch

This does not require installing astyle yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

to_bjdata() emits the Draft-3-only 'B' (byte) marker for _ArrayType_:"byte" even in default Draft-2 mode, and the value does not round-trip

3 participants