mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 14:05:18 +00:00
Round-trip BJData ND-array annotations exactly (single precision, key order)
to_bjdata() encoded a JData-annotated object as a BJData ND-array in two cases where from_bjdata() then returned a different value, breaking the documented round-trip guarantee: 1. A "single" element that is finite and in range but not exactly representable as float (e.g. 0.1) or that underflows to 0 (e.g. 1e-300) was silently narrowed instead of falling back to a plain object, unlike out-of-range integer elements. write_bjdata_ndarray() now only accepts a "single" element if it survives the narrowing to float and back, the same criterion write_compact_float() already uses for CBOR/MessagePack. 2. from_bjdata() emitted the annotation keys as _ArraySize_, _ArrayType_, _ArrayData_ instead of the documented _ArrayType_, _ArraySize_, _ArrayData_, because the size key is written while the dimension vector is read, before the type key. For ordered_json, whose comparison takes key order into account, this made a round trip of the documented example compare unequal. The element type marker is known before the dimension vector is read (it precedes '#'), so it is now passed down and the "_ArrayType_" key is emitted first. Fixes #5661. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -132,8 +132,14 @@ The library uses the following mapping from JSON values types to BJData types ac
|
||||
parsed back as a regular array,
|
||||
- every entry of `"_ArraySize_"` is a positive integer, and their product is representable as a `std::size_t`,
|
||||
- `"_ArrayData_"` is an array holding exactly that many elements, and
|
||||
- every element of `"_ArrayData_"` is a number of the kind named by `"_ArrayType_"` (a floating-point number for
|
||||
`single` and `double`, an integer otherwise).
|
||||
- every element of `"_ArrayData_"` is a number of the kind named by `"_ArrayType_"`: for the integer types, a
|
||||
value that fits the named width; for `double`, any value; for `single`, a value that survives narrowing to
|
||||
`float` and back without change (for instance, `0.1` does not, since it is not exactly representable as
|
||||
`float`).
|
||||
|
||||
An annotated object is always read back with its keys in the order shown above, `"_ArrayType_"`, `"_ArraySize_"`,
|
||||
`"_ArrayData_"`, regardless of the order the ND-array's header stores them in on the wire. This matters for
|
||||
`ordered_json`, whose comparison takes key order into account.
|
||||
|
||||
The current version of this library does not yet support automatic detection of and conversion from a nested JSON
|
||||
array input to a BJData ND-array.
|
||||
|
||||
Reference in New Issue
Block a user