Description
The BJData ndarray writer maps _ArrayType_ = "byte" to the type marker B unconditionally, with no gate on bjdata_version. But B (byte) is a Draft-3-only marker: the docs (bjdata.md, "Binary values") state it "must be explicitly enabled using the version parameter of to_bjdata", and BJData Draft 2 has no B type.
In write_bjdata_ndarray (include/nlohmann/detail/output/binary_writer.hpp) the type map contains {"byte", 'B'} and the marker is written regardless of the requested version. So with the default version = draft2, an annotated byte array produces [$B#…, a stream that is invalid under Draft 2 and that a strict third-party Draft-2 reader will reject.
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.
Low severity / spec-conformance and interop, and only reachable with JData-annotated input, but the behavior contradicts the documented version-gating of B.
Reproduction steps
Serialize a byte ndarray with default (Draft-2) options and inspect the bytes / round-trip.
Expected vs. actual results
- Expected (Draft-2 default): either a fallback encoding that does not use the Draft-3
B marker, or an error; and round-trip equality like the other dtypes.
- Actual: bytes contain
B (0x42); the value round-trips to a binary value instead of the original object.
Minimal code example
#include <nlohmann/json.hpp>
#include <cstdio>
using json = nlohmann::json;
int main()
{
json j;
j["_ArrayType_"] = "byte";
j["_ArraySize_"] = json::array({2});
j["_ArrayData_"] = json::array({1, 2});
auto v = json::to_bjdata(j); // default draft2
for (auto b : v) std::printf("%02x ", b); // 5b 24 42 23 5b 69 02 5d 01 02 ("[$B#[i2]..")
std::printf("\nback=%s\n", json::from_bjdata(v).dump().c_str());
}
Error messages
5b 24 42 23 5b 69 02 5d 01 02
back={"bytes":[1,2],"subtype":null}
(The stream contains the Draft-3 B marker under the Draft-2 default, and the round-trip yields a binary value rather than the original annotated object.)
Suggested fix
Only accept "byte"/emit B when bjdata_version selects Draft 3; otherwise fall back to a plain-object encoding (consistent with the other unrepresentable-annotation cases).
Compiler and operating system
g++ 13.3.0 (Ubuntu 24.04, x86-64)
Library version
develop @ 01853ed6bcf9ebe88ec2e248ea757b86417b1487
Validation
Description
The BJData ndarray writer maps
_ArrayType_ = "byte"to the type markerBunconditionally, with no gate onbjdata_version. ButB(byte) is a Draft-3-only marker: the docs (bjdata.md, "Binary values") state it "must be explicitly enabled using theversionparameter ofto_bjdata", and BJData Draft 2 has noBtype.In
write_bjdata_ndarray(include/nlohmann/detail/output/binary_writer.hpp) the type map contains{"byte", 'B'}and the marker is written regardless of the requestedversion. So with the defaultversion = draft2, an annotatedbytearray produces[$B#…, a stream that is invalid under Draft 2 and that a strict third-party Draft-2 reader will reject.Separately, unlike every other
_ArrayType_(e.g.uint8, which round-trips back to the annotated object), the result parses back as a binary value, soto_bjdata/from_bjdatachanges the value.Low severity / spec-conformance and interop, and only reachable with JData-annotated input, but the behavior contradicts the documented version-gating of
B.Reproduction steps
Serialize a
bytendarray with default (Draft-2) options and inspect the bytes / round-trip.Expected vs. actual results
Bmarker, or an error; and round-trip equality like the other dtypes.B(0x42); the value round-trips to a binary value instead of the original object.Minimal code example
Error messages
(The stream contains the Draft-3
Bmarker under the Draft-2 default, and the round-trip yields a binary value rather than the original annotated object.)Suggested fix
Only accept
"byte"/emitBwhenbjdata_versionselects Draft 3; otherwise fall back to a plain-object encoding (consistent with the other unrepresentable-annotation cases).Compiler and operating system
g++ 13.3.0 (Ubuntu 24.04, x86-64)
Library version
develop@01853ed6bcf9ebe88ec2e248ea757b86417b1487Validation
developbranch is used.