mirror of
https://github.com/nlohmann/json.git
synced 2026-11-16 16:43:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b73b7c72db |
No files matched your search
@@ -4096,10 +4096,6 @@ class binary_reader
|
||||
auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
|
||||
const auto result_number = number_lexer.scan();
|
||||
const auto number_string = number_lexer.get_token_string();
|
||||
// the number as the lexer read it, for SAX number_float(): unlike
|
||||
// get_token_string(), which is meant for error messages and shortens
|
||||
// long tokens, it is complete; the next scan() replaces it
|
||||
const string_t number_text = number_lexer.get_string();
|
||||
const auto result_remainder = number_lexer.scan();
|
||||
|
||||
using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
|
||||
@@ -4126,7 +4122,10 @@ class binary_reader
|
||||
number_string,
|
||||
out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr));
|
||||
}
|
||||
return sax->number_float(parsed_float, number_text);
|
||||
// number_string is a std::string, while the SAX interface takes a
|
||||
// string_t; convert explicitly, as the two are only implicitly
|
||||
// convertible for some string types
|
||||
return sax->number_float(parsed_float, string_t(number_string.data(), number_string.size()));
|
||||
}
|
||||
case token_type::uninitialized:
|
||||
case token_type::literal_true:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2136,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
|
||||
|
||||
Generated
+45
-7
@@ -22774,10 +22774,6 @@ class binary_reader
|
||||
auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
|
||||
const auto result_number = number_lexer.scan();
|
||||
const auto number_string = number_lexer.get_token_string();
|
||||
// the number as the lexer read it, for SAX number_float(): unlike
|
||||
// get_token_string(), which is meant for error messages and shortens
|
||||
// long tokens, it is complete; the next scan() replaces it
|
||||
const string_t number_text = number_lexer.get_string();
|
||||
const auto result_remainder = number_lexer.scan();
|
||||
|
||||
using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
|
||||
@@ -22804,7 +22800,10 @@ class binary_reader
|
||||
number_string,
|
||||
out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr));
|
||||
}
|
||||
return sax->number_float(parsed_float, number_text);
|
||||
// number_string is a std::string, while the SAX interface takes a
|
||||
// string_t; convert explicitly, as the two are only implicitly
|
||||
// convertible for some string types
|
||||
return sax->number_float(parsed_float, string_t(number_string.data(), number_string.size()));
|
||||
}
|
||||
case token_type::uninitialized:
|
||||
case token_type::literal_true:
|
||||
@@ -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
|
||||
|
||||
@@ -39623,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
|
||||
|
||||
@@ -1278,25 +1278,6 @@ TEST_CASE("BJData")
|
||||
CHECK(j.dump() == "3.141592653589793");
|
||||
}
|
||||
|
||||
SECTION("long floating-point number")
|
||||
{
|
||||
// the SAX parser receives the number text as it is in the
|
||||
// input, also when it is longer than the part of a token
|
||||
// that error messages show
|
||||
const std::vector<std::size_t> lengths = {8192, 8193, 10000};
|
||||
for (const auto length : lengths)
|
||||
{
|
||||
CAPTURE(length)
|
||||
const std::string number = "1." + std::string(length - 2, '3');
|
||||
std::vector<std::uint8_t> vec = {'H', 'I', static_cast<std::uint8_t>(length & 0xFF), static_cast<std::uint8_t>(length >> 8)};
|
||||
vec.insert(vec.end(), number.begin(), number.end());
|
||||
SaxEventLogger logger;
|
||||
CHECK(json::sax_parse(vec, &logger, json::input_format_t::bjdata));
|
||||
CHECK(logger.events == std::vector<std::string>({"number_float(" + number + ")"}));
|
||||
CHECK(json::from_bjdata(vec).is_number_float());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("errors")
|
||||
{
|
||||
// error while parsing length
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
@@ -793,25 +793,6 @@ TEST_CASE("UBJSON")
|
||||
CHECK(j.dump() == "3.141592653589793");
|
||||
}
|
||||
|
||||
SECTION("long floating-point number")
|
||||
{
|
||||
// the SAX parser receives the number text as it is in the
|
||||
// input, also when it is longer than the part of a token
|
||||
// that error messages show
|
||||
const std::vector<std::size_t> lengths = {8192, 8193, 10000};
|
||||
for (const auto length : lengths)
|
||||
{
|
||||
CAPTURE(length)
|
||||
const std::string number = "1." + std::string(length - 2, '3');
|
||||
std::vector<std::uint8_t> vec = {'H', 'I', static_cast<std::uint8_t>(length >> 8), static_cast<std::uint8_t>(length & 0xFF)};
|
||||
vec.insert(vec.end(), number.begin(), number.end());
|
||||
SaxEventLogger logger;
|
||||
CHECK(json::sax_parse(vec, &logger, json::input_format_t::ubjson));
|
||||
CHECK(logger.events == std::vector<std::string>({"number_float(" + number + ")"}));
|
||||
CHECK(json::from_ubjson(vec).is_number_float());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("errors")
|
||||
{
|
||||
// error while parsing length
|
||||
|
||||
Reference in new issue
Block a user