Compare commits

..
Author SHA1 Message Date
Niels Lohmann b73b7c72db Write a binary subtype independently of number_unsigned_t in dump()
The subtype of a binary value is a std::uint64_t, but dump() wrote it
with dump_integer, which converts its argument to the magnitude type of
number_unsigned_t. With a number_unsigned_t narrower than 64 bits, the
subtype was truncated (subtype 300 with std::uint8_t numbers was
printed as 44).

A dump_subtype helper now writes the subtype for both the compact and
the pretty-printed output: through dump_integer if the magnitude type
of number_unsigned_t has at least 64 bits (the default json, so its
output and speed are unchanged), and with a digit loop otherwise.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-11-16 13:07:47 +01:00
8 changed files with 120 additions and 24 deletions

No files matched your search

@@ -48,11 +48,10 @@ namespace detail
{
/*!
@brief implements binary to decimal floating-point conversion: the shortest
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2
algorithm for all other formats.
@brief implements the Grisu2 algorithm for binary to decimal floating-point
conversion.
The Grisu2 implementation is a slightly modified version of the reference
This implementation is a slightly modified version of the reference
implementation which may be obtained from
http://florian.loitsch.com/publications (bench.tar.gz).
@@ -257,7 +257,7 @@ struct ieee_binary_format<53> // binary64
These formats (float, double, and long double where it is binary64, e.g.
with MSVC or on Apple arm64) are converted by parse_float_native(). The
predicate is the one the serializer uses to choose to_chars() over snprintf.
predicate is the one the serializer uses to choose Grisu2.
*/
template<typename FloatType>
struct has_native_float_format
@@ -915,7 +915,7 @@ class binary_writer
JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j));
}
/// @throw other_error.502 always
/// @throw other_error.407 always
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
static void throw_size_marker_required(const BasicJsonType& j)
{
+43 -5
View File
@@ -636,7 +636,7 @@ class serializer
put_literal("\"subtype\": ");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
}
else
{
@@ -662,7 +662,7 @@ class serializer
put_literal("],\"subtype\":");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
put_char('}');
}
else
@@ -1883,6 +1883,40 @@ class serializer
write_buffer_pos = pos;
}
/*!
@brief dump the subtype of a binary value
The subtype is a std::uint64_t, whatever the number types are. @ref
dump_integer converts its argument to the magnitude type of
number_unsigned_t, so it writes the subtype only if that type has at least
64 bits; otherwise (it would truncate the subtype) the digits are written
one by one.
@param[in] subtype the subtype to dump
*/
void dump_subtype(std::uint64_t subtype)
{
dump_subtype(subtype, std::integral_constant<bool, magnitude_has_at_least_64_bits> {});
}
void dump_subtype(std::uint64_t subtype, std::true_type /*magnitude has at least 64 bits*/)
{
dump_integer(subtype);
}
void dump_subtype(std::uint64_t subtype, std::false_type /*magnitude has less than 64 bits*/)
{
std::array<char, 20> digits{{}}; // 2^64 - 1 has 20 digits
std::size_t start = digits.size();
do
{
digits[--start] = static_cast<char>('0' + (subtype % 10));
subtype /= 10;
}
while (subtype != 0);
put_string(digits, start, digits.size());
}
/*!
@brief dump an integer
@@ -2033,9 +2067,8 @@ class serializer
}
// If number_float_t is an IEEE-754 single or double precision number,
// use to_chars() to produce short numbers which are guaranteed to
// round-trip, using strtof and strtod, resp. (the shortest digits of
// Zmij for binary64, the Grisu2 algorithm for binary32).
// use the Grisu2 algorithm to produce short numbers which are
// guaranteed to round-trip, using strtof and strtod, resp.
//
// NB: The test below works if <long double> == <double>.
static constexpr bool is_ieee_single_or_double
@@ -2137,6 +2170,11 @@ class serializer
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
/// whether every std::uint64_t fits number_magnitude_t (then dump_integer
/// can write a binary subtype)
static constexpr bool magnitude_has_at_least_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits >= 64;
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
/// 64 bits (selected by type, as the comparison would be reported as
/// always true), else compared
+1 -1
View File
@@ -849,7 +849,7 @@ class basic_json_view
return !navigation::editable() || m_doc->edits == nullptr;
}
/// the member of this object with @a key (the first one if the key occurs more than once), or nullptr
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
{
return find_key(key, std::integral_constant<bool, is_editable> {});
+48 -11
View File
@@ -11642,7 +11642,7 @@ struct ieee_binary_format<53> // binary64
These formats (float, double, and long double where it is binary64, e.g.
with MSVC or on Apple arm64) are converted by parse_float_native(). The
predicate is the one the serializer uses to choose to_chars() over snprintf.
predicate is the one the serializer uses to choose Grisu2.
*/
template<typename FloatType>
struct has_native_float_format
@@ -31147,7 +31147,7 @@ class binary_writer
JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j));
}
/// @throw other_error.502 always
/// @throw other_error.407 always
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
static void throw_size_marker_required(const BasicJsonType& j)
{
@@ -36006,11 +36006,10 @@ namespace detail
{
/*!
@brief implements binary to decimal floating-point conversion: the shortest
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2
algorithm for all other formats.
@brief implements the Grisu2 algorithm for binary to decimal floating-point
conversion.
The Grisu2 implementation is a slightly modified version of the reference
This implementation is a slightly modified version of the reference
implementation which may be obtained from
http://florian.loitsch.com/publications (bench.tar.gz).
@@ -38123,7 +38122,7 @@ class serializer
put_literal("\"subtype\": ");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
}
else
{
@@ -38149,7 +38148,7 @@ class serializer
put_literal("],\"subtype\":");
if (binary.has_subtype())
{
dump_integer(binary.subtype());
dump_subtype(binary.subtype());
put_char('}');
}
else
@@ -39370,6 +39369,40 @@ class serializer
write_buffer_pos = pos;
}
/*!
@brief dump the subtype of a binary value
The subtype is a std::uint64_t, whatever the number types are. @ref
dump_integer converts its argument to the magnitude type of
number_unsigned_t, so it writes the subtype only if that type has at least
64 bits; otherwise (it would truncate the subtype) the digits are written
one by one.
@param[in] subtype the subtype to dump
*/
void dump_subtype(std::uint64_t subtype)
{
dump_subtype(subtype, std::integral_constant<bool, magnitude_has_at_least_64_bits> {});
}
void dump_subtype(std::uint64_t subtype, std::true_type /*magnitude has at least 64 bits*/)
{
dump_integer(subtype);
}
void dump_subtype(std::uint64_t subtype, std::false_type /*magnitude has less than 64 bits*/)
{
std::array<char, 20> digits{{}}; // 2^64 - 1 has 20 digits
std::size_t start = digits.size();
do
{
digits[--start] = static_cast<char>('0' + (subtype % 10));
subtype /= 10;
}
while (subtype != 0);
put_string(digits, start, digits.size());
}
/*!
@brief dump an integer
@@ -39520,9 +39553,8 @@ class serializer
}
// If number_float_t is an IEEE-754 single or double precision number,
// use to_chars() to produce short numbers which are guaranteed to
// round-trip, using strtof and strtod, resp. (the shortest digits of
// Zmij for binary64, the Grisu2 algorithm for binary32).
// use the Grisu2 algorithm to produce short numbers which are
// guaranteed to round-trip, using strtof and strtod, resp.
//
// NB: The test below works if <long double> == <double>.
static constexpr bool is_ieee_single_or_double
@@ -39624,6 +39656,11 @@ class serializer
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
/// whether every std::uint64_t fits number_magnitude_t (then dump_integer
/// can write a binary subtype)
static constexpr bool magnitude_has_at_least_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
&& std::numeric_limits<number_magnitude_t>::digits >= 64;
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
/// 64 bits (selected by type, as the comparison would be reported as
/// always true), else compared
+1 -1
View File
@@ -11016,7 +11016,7 @@ class basic_json_view
return !navigation::editable() || m_doc->edits == nullptr;
}
/// the member of this object with @a key (the first one if the key occurs more than once), or nullptr
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
{
return find_key(key, std::integral_constant<bool, is_editable> {});
+22
View File
@@ -483,3 +483,25 @@ TEST_CASE("custom number types: 32-bit integers in the binary formats")
CHECK(json32::to_msgpack(values) == nlohmann::json::to_msgpack(values64));
CHECK(json32::to_ubjson(values) == nlohmann::json::to_ubjson(values64));
}
TEST_CASE("custom number types: binary subtypes wider than the integer types")
{
// the subtype of a binary value is a std::uint64_t, whatever the number types are
using json8 = nlohmann::json::with_integers_t<std::int8_t, std::uint8_t>;
using json16 = nlohmann::json::with_integers_t<std::int16_t, std::uint16_t>;
using json32 = nlohmann::json::with_integers_t<std::int32_t, std::uint32_t>;
const std::uint64_t max_uint64 = (std::numeric_limits<std::uint64_t>::max)();
CHECK(json8::binary({1}, 300).dump() == "{\"bytes\":[1],\"subtype\":300}");
CHECK(json8::binary({200}, 255).dump() == "{\"bytes\":[200],\"subtype\":255}");
CHECK(json16::binary({1}, 65543).dump() == "{\"bytes\":[1],\"subtype\":65543}");
CHECK(json32::binary({1}, 4294967301u).dump() == "{\"bytes\":[1],\"subtype\":4294967301}");
CHECK(json32::binary({1}, max_uint64).dump() == "{\"bytes\":[1],\"subtype\":18446744073709551615}");
CHECK(json32::binary({1}, 0).dump() == "{\"bytes\":[1],\"subtype\":0}");
CHECK(json32::binary({1}, 4294967301u).dump(1) == "{\n \"bytes\": [1],\n \"subtype\": 4294967301\n}");
CHECK(json8::binary({1}).dump() == "{\"bytes\":[1],\"subtype\":null}");
// unchanged for the default number types
CHECK(nlohmann::json::binary({1}, 4294967301u).dump() == "{\"bytes\":[1],\"subtype\":4294967301}");
CHECK(nlohmann::json::binary({1}, max_uint64).dump(1) == "{\n \"bytes\": [1],\n \"subtype\": 18446744073709551615\n}");
}