diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index ebdeef26f..38c29a772 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -1914,20 +1914,25 @@ class binary_reader } default: - { - } + break; } auto last_token = get_token_string(); - if (input_format == input_format_t::bjdata) + std::string message; + + if (input_format != input_format_t::bjdata) { - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x" + last_token, "string"), nullptr)); + message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token; } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token; + } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), BasicJsonType())); } /*! @param[out] dim an integer vector storing the ND array dimensions - @return whether array creation completed + @return whether reading ND array size vector is successful */ bool get_ubjson_ndarray_size(std::vector& dim) { @@ -2106,7 +2111,7 @@ class binary_reader { return false; } - result = 1; + result = ( (dim.size() > 0) ? 1 : 0 ); for (auto i : dim) { result *= i; @@ -2115,15 +2120,20 @@ class binary_reader } default: - { - } + break; } auto last_token = get_token_string(); - if (input_format == input_format_t::bjdata) + std::string message; + + if (input_format != input_format_t::bjdata) { - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x" + last_token, "size"), nullptr)); + message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token; } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token; + } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), BasicJsonType())); } /*! @@ -2352,8 +2362,7 @@ class binary_reader return get_ubjson_object(); default: // anything else - { - } + break; } auto last_token = get_token_string(); return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ad7cb827c..3b432fdf5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10290,20 +10290,25 @@ class binary_reader } default: - { - } + break; } auto last_token = get_token_string(); - if (input_format == input_format_t::bjdata) + std::string message; + + if (input_format != input_format_t::bjdata) { - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x" + last_token, "string"), nullptr)); + message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token; } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token; + } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), BasicJsonType())); } /*! @param[out] dim an integer vector storing the ND array dimensions - @return whether array creation completed + @return whether reading ND array size vector is successful */ bool get_ubjson_ndarray_size(std::vector& dim) { @@ -10482,7 +10487,7 @@ class binary_reader { return false; } - result = 1; + result = ( (dim.size() > 0) ? 1 : 0 ); for (auto i : dim) { result *= i; @@ -10491,15 +10496,20 @@ class binary_reader } default: - { - } + break; } auto last_token = get_token_string(); - if (input_format == input_format_t::bjdata) + std::string message; + + if (input_format != input_format_t::bjdata) { - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x" + last_token, "size"), nullptr)); + message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token; } - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); + else + { + message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token; + } + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), BasicJsonType())); } /*! @@ -10728,8 +10738,7 @@ class binary_reader return get_ubjson_object(); default: // anything else - { - } + break; } auto last_token = get_token_string(); return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); diff --git a/test/src/unit-bjdata.cpp b/test/src/unit-bjdata.cpp index a5377b838..c21e5a7bb 100644 --- a/test/src/unit-bjdata.cpp +++ b/test/src/unit-bjdata.cpp @@ -1184,7 +1184,7 @@ TEST_CASE("BJData") std::vector vec3 = {'H', 'i', 2, '1', '.'}; CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error); std::vector vec4 = {'H', 2, '1', '0'}; - CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x02", json::parse_error); + CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x02", json::parse_error); } } } @@ -2534,7 +2534,7 @@ TEST_CASE("BJData") std::vector v = {'S', '1', 'a'}; json _; CHECK_THROWS_AS(_ = json::from_bjdata(v), json::parse_error&); - CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData string: expected length type specification (U, i, I, u, l, m, L, M); last byte: 0x31"); + CHECK_THROWS_WITH(_ = json::from_bjdata(v), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData string: expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x31"); } SECTION("parse bjdata markers in ubjson") @@ -2632,7 +2632,7 @@ TEST_CASE("BJData") std::vector v0 = {'[', '#', 'T', ']'}; CHECK_THROWS_AS(_ = json::from_bjdata(v0), json::parse_error&); - CHECK_THROWS_WITH(_ = json::from_bjdata(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x54"); + CHECK_THROWS_WITH(_ = json::from_bjdata(v0), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x54"); CHECK(json::from_bjdata(v0, true, false).is_discarded()); } @@ -2703,7 +2703,7 @@ TEST_CASE("BJData") std::vector vST = {'[', '$', 'i', '#', '[', '$', 'i', '#'}; json _; CHECK_THROWS_AS(_ = json::from_bjdata(vST), json::parse_error&); - CHECK_THROWS_WITH(_ = json::from_bjdata(vST), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0xFF"); + CHECK_THROWS_WITH(_ = json::from_bjdata(vST), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0xFF"); CHECK(json::from_bjdata(vST, true, false).is_discarded()); std::vector v = {'[', '$', 'i', '#', '[', '$', 'i', '#', 'i', 2, 1, 2}; @@ -2718,7 +2718,7 @@ TEST_CASE("BJData") std::vector vS = {'[', '$', 'i', '#', '[', '#', 'i', 2, 1, 2, 1}; CHECK_THROWS_AS(_ = json::from_bjdata(vS), json::parse_error&); - CHECK_THROWS_WITH(_ = json::from_bjdata(vS), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, I, u, l, m, L, M) after '#'; last byte: 0x01"); + CHECK_THROWS_WITH(_ = json::from_bjdata(vS), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x01"); CHECK(json::from_bjdata(vS, true, false).is_discarded()); std::vector vT = {'[', '$', 'i', '#', '[', 'i', 2, 'i'};