mirror of
https://github.com/nlohmann/json.git
synced 2026-11-16 16:43:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
13766fc435 |
No files matched your search
@@ -48,10 +48,11 @@ namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief implements the Grisu2 algorithm for binary to decimal floating-point
|
||||
conversion.
|
||||
@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.
|
||||
|
||||
This implementation is a slightly modified version of the reference
|
||||
The Grisu2 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 Grisu2.
|
||||
predicate is the one the serializer uses to choose to_chars() over snprintf.
|
||||
*/
|
||||
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.407 always
|
||||
/// @throw other_error.502 always
|
||||
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
|
||||
static void throw_size_marker_required(const BasicJsonType& j)
|
||||
{
|
||||
|
||||
@@ -636,7 +636,7 @@ class serializer
|
||||
put_literal("\"subtype\": ");
|
||||
if (binary.has_subtype())
|
||||
{
|
||||
dump_subtype(binary.subtype());
|
||||
dump_integer(binary.subtype());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -662,7 +662,7 @@ class serializer
|
||||
put_literal("],\"subtype\":");
|
||||
if (binary.has_subtype())
|
||||
{
|
||||
dump_subtype(binary.subtype());
|
||||
dump_integer(binary.subtype());
|
||||
put_char('}');
|
||||
}
|
||||
else
|
||||
@@ -1883,40 +1883,6 @@ 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
|
||||
|
||||
@@ -2067,8 +2033,9 @@ class serializer
|
||||
}
|
||||
|
||||
// If number_float_t is an IEEE-754 single or double precision number,
|
||||
// use the Grisu2 algorithm to produce short numbers which are
|
||||
// guaranteed to round-trip, using strtof and strtod, resp.
|
||||
// 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).
|
||||
//
|
||||
// NB: The test below works if <long double> == <double>.
|
||||
static constexpr bool is_ieee_single_or_double
|
||||
@@ -2170,11 +2137,6 @@ 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
|
||||
|
||||
@@ -849,7 +849,7 @@ class basic_json_view
|
||||
return !navigation::editable() || m_doc->edits == nullptr;
|
||||
}
|
||||
|
||||
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
|
||||
/// the member of this object with @a key (the first 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> {});
|
||||
|
||||
Generated
+11
-48
@@ -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 Grisu2.
|
||||
predicate is the one the serializer uses to choose to_chars() over snprintf.
|
||||
*/
|
||||
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.407 always
|
||||
/// @throw other_error.502 always
|
||||
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
|
||||
static void throw_size_marker_required(const BasicJsonType& j)
|
||||
{
|
||||
@@ -36006,10 +36006,11 @@ namespace detail
|
||||
{
|
||||
|
||||
/*!
|
||||
@brief implements the Grisu2 algorithm for binary to decimal floating-point
|
||||
conversion.
|
||||
@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.
|
||||
|
||||
This implementation is a slightly modified version of the reference
|
||||
The Grisu2 implementation is a slightly modified version of the reference
|
||||
implementation which may be obtained from
|
||||
http://florian.loitsch.com/publications (bench.tar.gz).
|
||||
|
||||
@@ -38122,7 +38123,7 @@ class serializer
|
||||
put_literal("\"subtype\": ");
|
||||
if (binary.has_subtype())
|
||||
{
|
||||
dump_subtype(binary.subtype());
|
||||
dump_integer(binary.subtype());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -38148,7 +38149,7 @@ class serializer
|
||||
put_literal("],\"subtype\":");
|
||||
if (binary.has_subtype())
|
||||
{
|
||||
dump_subtype(binary.subtype());
|
||||
dump_integer(binary.subtype());
|
||||
put_char('}');
|
||||
}
|
||||
else
|
||||
@@ -39369,40 +39370,6 @@ 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
|
||||
|
||||
@@ -39553,8 +39520,9 @@ class serializer
|
||||
}
|
||||
|
||||
// If number_float_t is an IEEE-754 single or double precision number,
|
||||
// use the Grisu2 algorithm to produce short numbers which are
|
||||
// guaranteed to round-trip, using strtof and strtod, resp.
|
||||
// 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).
|
||||
//
|
||||
// NB: The test below works if <long double> == <double>.
|
||||
static constexpr bool is_ieee_single_or_double
|
||||
@@ -39656,11 +39624,6 @@ 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
|
||||
|
||||
Generated
+1
-1
@@ -11016,7 +11016,7 @@ class basic_json_view
|
||||
return !navigation::editable() || m_doc->edits == nullptr;
|
||||
}
|
||||
|
||||
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
|
||||
/// the member of this object with @a key (the first 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> {});
|
||||
|
||||
@@ -483,25 +483,3 @@ 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}");
|
||||
}
|
||||
Reference in new issue
Block a user