diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index bbd2daceb..c7b1b92f8 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -4096,6 +4096,10 @@ class binary_reader auto number_lexer = detail::lexer(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::token_type; @@ -4122,10 +4126,7 @@ class binary_reader number_string, out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr)); } - // 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())); + return sax->number_float(parsed_float, number_text); } case token_type::uninitialized: case token_type::literal_true: diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 473bfa894..d6320c897 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -22774,6 +22774,10 @@ class binary_reader auto number_lexer = detail::lexer(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::token_type; @@ -22800,10 +22804,7 @@ class binary_reader number_string, out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr)); } - // 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())); + return sax->number_float(parsed_float, number_text); } case token_type::uninitialized: case token_type::literal_true: diff --git a/tests/src/unit-bjdata.cpp b/tests/src/unit-bjdata.cpp index 304db5676..6a0dc0f8f 100644 --- a/tests/src/unit-bjdata.cpp +++ b/tests/src/unit-bjdata.cpp @@ -1278,6 +1278,25 @@ 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 lengths = {8192, 8193, 10000}; + for (const auto length : lengths) + { + CAPTURE(length) + const std::string number = "1." + std::string(length - 2, '3'); + std::vector vec = {'H', 'I', static_cast(length & 0xFF), static_cast(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({"number_float(" + number + ")"})); + CHECK(json::from_bjdata(vec).is_number_float()); + } + } + SECTION("errors") { // error while parsing length diff --git a/tests/src/unit-ubjson.cpp b/tests/src/unit-ubjson.cpp index 315f1dea1..58e51b5b3 100644 --- a/tests/src/unit-ubjson.cpp +++ b/tests/src/unit-ubjson.cpp @@ -793,6 +793,25 @@ 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 lengths = {8192, 8193, 10000}; + for (const auto length : lengths) + { + CAPTURE(length) + const std::string number = "1." + std::string(length - 2, '3'); + std::vector vec = {'H', 'I', static_cast(length >> 8), static_cast(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({"number_float(" + number + ")"})); + CHECK(json::from_ubjson(vec).is_number_float()); + } + } + SECTION("errors") { // error while parsing length