diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 0feea2833..2a8d7543e 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,6 +13,7 @@ #include // valarray #include +#include #include #include #include @@ -27,7 +28,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } n = nullptr; } @@ -58,7 +59,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -67,7 +68,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } b = *j.template get_ptr(); } @@ -77,7 +78,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); } @@ -93,7 +94,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); @@ -133,7 +134,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -150,7 +151,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -241,8 +242,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -253,7 +253,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } bin = *j.template get_ptr(); @@ -265,7 +265,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } ConstructibleObjectType ret; @@ -319,7 +319,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -348,14 +348,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -368,14 +368,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } diff --git a/include/nlohmann/detail/diagnostics_t.hpp b/include/nlohmann/detail/diagnostics_t.hpp new file mode 100644 index 000000000..727e82350 --- /dev/null +++ b/include/nlohmann/detail/diagnostics_t.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include +#include +#include + +namespace nlohmann +{ +namespace detail +{ + +template +class diagnostics_t +{ + public: + diagnostics_t() noexcept = default; + diagnostics_t(const BasicJsonType& j) noexcept + : m_j(&j) + {} + + std::string diagnostics() const + { +#if JSON_DIAGNOSTICS + if (m_j == nullptr) + { + return ""; + } + + std::vector tokens; + for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + tokens.emplace_back(std::to_string(i)); + continue; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (element.second == *current) + { + tokens.emplace_back(element.first.c_str()); + continue; + } + } + break; + } + + default: + break; + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; +#endif + } + + private: + const BasicJsonType* m_j = static_cast(nullptr); +}; + +} // namespace detail +} // namespace nlohmann diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index dd92897d5..56a582f79 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -4,6 +4,7 @@ #include // runtime_error #include // to_string +#include #include #include @@ -127,18 +128,20 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; + position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -204,9 +207,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template + static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; + std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -258,9 +262,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template + static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("type_error", id_) + what_arg; + std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; return type_error(id_, w.c_str()); } @@ -305,9 +310,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template + static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("out_of_range", id_) + what_arg; + std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; return out_of_range(id_, w.c_str()); } @@ -343,9 +349,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template + static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("other_error", id_) + what_arg; + std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; return other_error(id_, w.c_str()); } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 806e36030..a896f1558 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -13,6 +13,7 @@ #include // make_pair, move #include // vector +#include #include #include #include @@ -64,6 +65,7 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -137,7 +139,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); } } @@ -213,7 +215,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -234,7 +236,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); } // All BSON binary values have a subtype @@ -316,7 +318,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); } } } @@ -716,7 +718,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } case cbor_tag_handler_t::ignore: @@ -831,7 +833,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -926,7 +928,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -1025,7 +1027,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); } } } @@ -1492,7 +1494,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -1574,7 +1576,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -1824,7 +1826,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); } } @@ -1894,7 +1896,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); } } } @@ -1932,7 +1934,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); } return get_ubjson_size_value(result.first); @@ -2022,7 +2024,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -2043,7 +2045,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -2221,7 +2223,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } switch (result_number) @@ -2233,7 +2235,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } } @@ -2389,7 +2391,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); } return true; } diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 61266188b..f29aa1a68 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -5,6 +5,7 @@ #include // move #include // vector +#include #include #include @@ -154,6 +155,7 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; + using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -219,8 +221,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -245,8 +246,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -336,6 +336,7 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; + using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -406,7 +407,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -469,7 +470,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index ffe483aa1..40ef371eb 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -8,6 +8,7 @@ #include // vector #include +#include #include #include #include @@ -57,6 +58,7 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; + using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -96,7 +98,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -125,7 +127,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -162,7 +164,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } return result; @@ -209,7 +211,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -222,7 +224,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // remember we are now inside an object @@ -265,7 +267,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -336,7 +338,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + exception_message(token_type::uninitialized, "value"), diagnostics_t())); } default: // the last token was unexpected @@ -344,7 +346,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + exception_message(token_type::literal_or_value, "value"), diagnostics_t())); } } } @@ -391,7 +393,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + exception_message(token_type::end_array, "array"), diagnostics_t())); } else // object { @@ -404,7 +406,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -418,7 +420,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // parse values @@ -447,7 +449,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + exception_message(token_type::end_object, "object"), diagnostics_t())); } } } diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index cceb8d05f..565ac64d1 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -3,6 +3,7 @@ #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +#include #include #include #include @@ -51,6 +52,7 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); + using diagnostics_t = detail::diagnostics_t; public: @@ -257,7 +259,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -266,7 +268,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -300,7 +302,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -401,7 +403,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -438,7 +440,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -446,7 +448,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -494,7 +496,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: { @@ -565,7 +567,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -586,13 +588,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -601,7 +603,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -619,7 +621,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); } /*! diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 865376cf1..f3a70bdce 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -8,6 +8,7 @@ #include // move #include // vector +#include #include #include #include @@ -21,6 +22,8 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; + using diagnostics_t = detail::diagnostics_t; + public: /*! @brief create JSON pointer @@ -247,7 +250,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } reference_tokens.pop_back(); @@ -271,7 +274,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } return reference_tokens.back(); @@ -337,15 +340,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + s + - "' must not begin with '0'")); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); } std::size_t processed_chars = 0; @@ -356,20 +357,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type")); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE } return static_cast(res); @@ -380,7 +381,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } json_pointer result = *this; @@ -443,7 +444,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); } } @@ -515,7 +516,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -548,7 +549,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -557,7 +558,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -595,9 +596,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); } // use unchecked array access @@ -606,7 +605,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -639,7 +638,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -648,7 +647,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -752,9 +751,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); } // extract the reference tokens: @@ -789,7 +786,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); } } @@ -916,7 +913,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); } BasicJsonType result; @@ -926,7 +923,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); } // assign value to reference pointed to by JSON pointer; Note that if diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 72ebbeda6..10bc34011 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -8,6 +8,7 @@ #include // string #include // isnan, isinf +#include #include #include #include @@ -57,7 +58,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; } } } @@ -906,8 +907,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, j.diagnostics() + - "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -1031,7 +1031,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); } } diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 0a34c8011..338414180 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -13,6 +13,7 @@ #include // move #include +#include #include #include #include @@ -499,7 +500,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -593,7 +594,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 13685dd10..086c0f557 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -51,6 +51,7 @@ SOFTWARE. #include #include #include +#include #include #include #include @@ -189,10 +190,15 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; + template + friend class ::nlohmann::detail::diagnostics_t; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + /// shortcut + using diagnostics_t = ::nlohmann::detail::diagnostics_t; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -1060,7 +1066,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE } break; } @@ -1629,7 +1635,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list")); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); } } @@ -1950,7 +1956,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); } // copy type from first iterator @@ -1968,7 +1974,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); } break; } @@ -2042,8 +2048,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + - std::string(first.m_object->type_name()))); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } assert_invariant(); @@ -2763,60 +2768,6 @@ class basic_json /// @} - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - std::vector tokens; - for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (current->m_parent->m_value.array->operator[](i) == *current) - { - tokens.emplace_back(std::to_string(i)); - continue; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (element.second == *current) - { - tokens.emplace_back(element.first.c_str()); - continue; - } - } - break; - } - - default: - break; - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + b; - }) + ") "; -#else - return ""; -#endif - } - private: ////////////////// // value access // @@ -2830,7 +2781,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); } /// get a pointer to the value (object) @@ -2951,7 +2902,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); } public: @@ -3379,7 +3330,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -3390,7 +3341,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -3451,12 +3402,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3498,12 +3449,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3555,12 +3506,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3606,12 +3557,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -3675,7 +3626,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3705,7 +3656,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3757,7 +3708,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3799,7 +3750,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3853,7 +3804,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3897,7 +3848,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -3969,7 +3920,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4042,7 +3993,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4196,7 +4147,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -4212,7 +4163,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range")); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); } if (is_string()) @@ -4248,7 +4199,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -4309,7 +4260,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -4326,7 +4277,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); } if (is_string()) @@ -4364,7 +4315,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -4407,7 +4358,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -4441,14 +4392,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -5393,7 +5344,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5431,7 +5382,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5484,7 +5435,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -5592,7 +5543,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -5654,7 +5605,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -5730,7 +5681,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5743,7 +5694,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -5787,7 +5738,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5803,7 +5754,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -5841,24 +5792,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); } // insert to array and return iterator @@ -5903,13 +5854,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -5954,19 +5905,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -6003,11 +5954,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -6054,20 +6005,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } for (auto it = first; it != last; ++it) @@ -6162,7 +6113,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6195,7 +6146,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6228,7 +6179,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6261,7 +6212,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -6275,7 +6226,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -8484,7 +8435,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); } // default case: insert add offset @@ -8518,7 +8469,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); } } else if (parent.is_array()) @@ -8531,7 +8482,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); } // iterate and apply the operations @@ -8551,13 +8502,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); } // no error: return value @@ -8567,7 +8518,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); } // collect mandatory members @@ -8645,7 +8596,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); } break; @@ -8655,7 +8606,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); } } } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 59a829b5f..bd79bd6fd 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -73,6 +73,175 @@ SOFTWARE. #include // runtime_error #include // to_string +// #include + + +#include +#include +// #include + + +#include // array +#include // size_t +#include // uint8_t +#include // string + +namespace nlohmann +{ +namespace detail +{ +/////////////////////////// +// JSON type enumeration // +/////////////////////////// + +/*! +@brief the JSON type enumeration + +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), +@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), +@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and +@ref basic_json::is_structured() rely on it. + +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +approximate integers which do not fit in the limits of their respective type. + +@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type + +@since version 1.0.0 +*/ +enum class value_t : std::uint8_t +{ + null, ///< null value + object, ///< object (unordered set of name/value pairs) + array, ///< array (ordered collection of values) + string, ///< string value + boolean, ///< boolean value + number_integer, ///< number value (signed integer) + number_unsigned, ///< number value (unsigned integer) + number_float, ///< number value (floating-point) + binary, ///< binary array (ordered collection of bytes) + discarded ///< discarded by the parser callback function +}; + +/*! +@brief comparison operator for JSON types + +Returns an ordering that is similar to Python: +- order: null < boolean < number < object < array < string < binary +- furthermore, each type is not smaller than itself +- discarded values are not comparable +- binary is represented as a b"" string in python and directly comparable to a + string; however, making a binary array directly comparable with a string would + be surprising behavior in a JSON file. + +@since version 1.0.0 +*/ +inline bool operator<(const value_t lhs, const value_t rhs) noexcept +{ + static constexpr std::array order = {{ + 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, + 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, + 6 /* binary */ + } + }; + + const auto l_index = static_cast(lhs); + const auto r_index = static_cast(rhs); + return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; +} +} // namespace detail +} // namespace nlohmann + + +namespace nlohmann +{ +namespace detail +{ + +template +class diagnostics_t +{ + public: + diagnostics_t() noexcept = default; + diagnostics_t(const BasicJsonType& j) noexcept + : m_j(&j) + {} + + std::string diagnostics() const + { +#if JSON_DIAGNOSTICS + if (m_j == nullptr) + { + return ""; + } + + std::vector tokens; + for (const auto* current = m_j; current->m_parent != nullptr; current = current->m_parent) + { + switch (current->m_parent->type()) + { + case value_t::array: + { + for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) + { + if (current->m_parent->m_value.array->operator[](i) == *current) + { + tokens.emplace_back(std::to_string(i)); + continue; + } + } + break; + } + + case value_t::object: + { + for (const auto& element : *current->m_parent->m_value.object) + { + if (element.second == *current) + { + tokens.emplace_back(element.first.c_str()); + continue; + } + } + break; + } + + default: + break; + } + } + + if (tokens.empty()) + { + return ""; + } + + return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, + [](const std::string & a, const std::string & b) + { + return a + "/" + b; + }) + ") "; +#else + return ""; +#endif + } + + private: + const BasicJsonType* m_j = static_cast(nullptr); +}; + +} // namespace detail +} // namespace nlohmann + // #include @@ -2500,18 +2669,20 @@ class parse_error : public exception @param[in] what_arg the explanatory string @return parse_error object */ - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + template + static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; + position_string(pos) + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, pos.chars_read_total, w.c_str()); } - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg) + template + static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { std::string w = exception::name("parse_error", id_) + "parse error" + (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + what_arg; + ": " + diagnostics.diagnostics() + what_arg; return parse_error(id_, byte_, w.c_str()); } @@ -2577,9 +2748,10 @@ caught.,invalid_iterator} class invalid_iterator : public exception { public: - static invalid_iterator create(int id_, const std::string& what_arg) + template + static invalid_iterator create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("invalid_iterator", id_) + what_arg; + std::string w = exception::name("invalid_iterator", id_) + diagnostics.diagnostics() + what_arg; return invalid_iterator(id_, w.c_str()); } @@ -2631,9 +2803,10 @@ caught.,type_error} class type_error : public exception { public: - static type_error create(int id_, const std::string& what_arg) + template + static type_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("type_error", id_) + what_arg; + std::string w = exception::name("type_error", id_) + diagnostics.diagnostics() + what_arg; return type_error(id_, w.c_str()); } @@ -2678,9 +2851,10 @@ caught.,out_of_range} class out_of_range : public exception { public: - static out_of_range create(int id_, const std::string& what_arg) + template + static out_of_range create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("out_of_range", id_) + what_arg; + std::string w = exception::name("out_of_range", id_) + diagnostics.diagnostics() + what_arg; return out_of_range(id_, w.c_str()); } @@ -2716,9 +2890,10 @@ caught.,other_error} class other_error : public exception { public: - static other_error create(int id_, const std::string& what_arg) + template + static other_error create(int id_, const std::string& what_arg, const detail::diagnostics_t& diagnostics) { - std::string w = exception::name("other_error", id_) + what_arg; + std::string w = exception::name("other_error", id_) + diagnostics.diagnostics() + what_arg; return other_error(id_, w.c_str()); } @@ -2729,6 +2904,8 @@ class other_error : public exception } // namespace detail } // namespace nlohmann +// #include + // #include // #include @@ -3423,87 +3600,6 @@ struct is_constructible_tuple> : conjunction -#include // array -#include // size_t -#include // uint8_t -#include // string - -namespace nlohmann -{ -namespace detail -{ -/////////////////////////// -// JSON type enumeration // -/////////////////////////// - -/*! -@brief the JSON type enumeration - -This enumeration collects the different JSON types. It is internally used to -distinguish the stored values, and the functions @ref basic_json::is_null(), -@ref basic_json::is_object(), @ref basic_json::is_array(), -@ref basic_json::is_string(), @ref basic_json::is_boolean(), -@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), -@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), -@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and -@ref basic_json::is_structured() rely on it. - -@note There are three enumeration entries (number_integer, number_unsigned, and -number_float), because the library distinguishes these three types for numbers: -@ref basic_json::number_unsigned_t is used for unsigned integers, -@ref basic_json::number_integer_t is used for signed integers, and -@ref basic_json::number_float_t is used for floating-point numbers or to -approximate integers which do not fit in the limits of their respective type. - -@sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON -value with the default value for a given type - -@since version 1.0.0 -*/ -enum class value_t : std::uint8_t -{ - null, ///< null value - object, ///< object (unordered set of name/value pairs) - array, ///< array (ordered collection of values) - string, ///< string value - boolean, ///< boolean value - number_integer, ///< number value (signed integer) - number_unsigned, ///< number value (unsigned integer) - number_float, ///< number value (floating-point) - binary, ///< binary array (ordered collection of bytes) - discarded ///< discarded by the parser callback function -}; - -/*! -@brief comparison operator for JSON types - -Returns an ordering that is similar to Python: -- order: null < boolean < number < object < array < string < binary -- furthermore, each type is not smaller than itself -- discarded values are not comparable -- binary is represented as a b"" string in python and directly comparable to a - string; however, making a binary array directly comparable with a string would - be surprising behavior in a JSON file. - -@since version 1.0.0 -*/ -inline bool operator<(const value_t lhs, const value_t rhs) noexcept -{ - static constexpr std::array order = {{ - 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, - 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, - 6 /* binary */ - } - }; - - const auto l_index = static_cast(lhs); - const auto r_index = static_cast(rhs); - return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; -} -} // namespace detail -} // namespace nlohmann - - namespace nlohmann { namespace detail @@ -3513,7 +3609,7 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) { if (JSON_HEDLEY_UNLIKELY(!j.is_null())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be null, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } n = nullptr; } @@ -3544,7 +3640,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -3553,7 +3649,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) { if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be boolean, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } b = *j.template get_ptr(); } @@ -3563,7 +3659,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); } @@ -3579,7 +3675,7 @@ void from_json(const BasicJsonType& j, ConstructibleStringType& s) { if (JSON_HEDLEY_UNLIKELY(!j.is_string())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be string, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } s = *j.template get_ptr(); @@ -3619,7 +3715,7 @@ void from_json(const BasicJsonType& j, std::forward_list& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.clear(); std::transform(j.rbegin(), j.rend(), @@ -3636,7 +3732,7 @@ void from_json(const BasicJsonType& j, std::valarray& l) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } l.resize(j.size()); std::transform(j.begin(), j.end(), std::begin(l), @@ -3727,8 +3823,7 @@ void()) { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + - std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } from_json_array_impl(j, arr, priority_tag<3> {}); @@ -3739,7 +3834,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) { if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be binary, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } bin = *j.template get_ptr(); @@ -3751,7 +3846,7 @@ void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) { if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } ConstructibleObjectType ret; @@ -3805,7 +3900,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val) } default: - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be number, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } } @@ -3834,14 +3929,14 @@ void from_json(const BasicJsonType& j, std::map& { if (JSON_HEDLEY_UNLIKELY(!j.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), detail::diagnostics_t(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -3854,14 +3949,14 @@ void from_json(const BasicJsonType& j, std::unordered_map(j))); } m.clear(); for (const auto& p : j) { if (JSON_HEDLEY_UNLIKELY(!p.is_array())) { - JSON_THROW(type_error::create(302, j.diagnostics() + "type must be array, but is " + std::string(p.type_name()))); + JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), detail::diagnostics_t(j))); } m.emplace(p.at(0).template get(), p.at(1).template get()); } @@ -4666,6 +4761,8 @@ class byte_container_with_subtype : public BinaryType // #include +// #include + // #include @@ -4805,6 +4902,8 @@ std::size_t hash(const BasicJsonType& j) #include // make_pair, move #include // vector +// #include + // #include // #include @@ -5295,6 +5394,8 @@ class span_input_adapter #include // move #include // vector +// #include + // #include // #include @@ -5446,6 +5547,7 @@ class json_sax_dom_parser using number_float_t = typename BasicJsonType::number_float_t; using string_t = typename BasicJsonType::string_t; using binary_t = typename BasicJsonType::binary_t; + using diagnostics_t = detail::diagnostics_t; /*! @param[in, out] r reference to a JSON value that is manipulated while @@ -5511,8 +5613,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5537,8 +5638,7 @@ class json_sax_dom_parser if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + - "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5628,6 +5728,7 @@ class json_sax_dom_callback_parser using binary_t = typename BasicJsonType::binary_t; using parser_callback_t = typename BasicJsonType::parser_callback_t; using parse_event_t = typename BasicJsonType::parse_event_t; + using diagnostics_t = detail::diagnostics_t; json_sax_dom_callback_parser(BasicJsonType& r, const parser_callback_t cb, @@ -5698,7 +5799,7 @@ class json_sax_dom_callback_parser // check object limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive object size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -5761,7 +5862,7 @@ class json_sax_dom_callback_parser // check array limit if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) { - JSON_THROW(out_of_range::create(408, ref_stack.back()->diagnostics() + "excessive array size: " + std::to_string(len))); + JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), diagnostics_t(*ref_stack.back()))); } return true; @@ -7827,6 +7928,7 @@ class binary_reader using json_sax_t = SAX; using char_type = typename InputAdapterType::char_type; using char_int_type = typename std::char_traits::int_type; + using diagnostics_t = detail::diagnostics_t; public: /*! @@ -7900,7 +8002,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) { return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"))); + parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), diagnostics_t())); } } @@ -7976,7 +8078,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 1)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), diagnostics_t())); } return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); @@ -7997,7 +8099,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(len < 0)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), diagnostics_t())); } // All BSON binary values have a subtype @@ -8079,7 +8181,7 @@ class binary_reader { std::array cr{{}}; (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()))); + return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), diagnostics_t())); } } } @@ -8479,7 +8581,7 @@ class binary_reader case cbor_tag_handler_t::error: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } case cbor_tag_handler_t::ignore: @@ -8594,7 +8696,7 @@ class binary_reader default: // anything else (0xFF is handled inside the other types) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -8689,7 +8791,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -8788,7 +8890,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), diagnostics_t())); } } } @@ -9255,7 +9357,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -9337,7 +9439,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), diagnostics_t())); } } } @@ -9587,7 +9689,7 @@ class binary_reader default: auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), diagnostics_t())); } } @@ -9657,7 +9759,7 @@ class binary_reader default: { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), diagnostics_t())); } } } @@ -9695,7 +9797,7 @@ class binary_reader return false; } auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), diagnostics_t())); } return get_ubjson_size_value(result.first); @@ -9785,7 +9887,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current > 127)) { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"))); + return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), diagnostics_t())); } string_t s(1, static_cast(current)); return sax->string(s); @@ -9806,7 +9908,7 @@ class binary_reader default: // anything else { auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"))); + return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), diagnostics_t())); } } } @@ -9984,7 +10086,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } switch (result_number) @@ -9996,7 +10098,7 @@ class binary_reader case token_type::value_float: return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"))); + return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), diagnostics_t())); } } @@ -10152,7 +10254,7 @@ class binary_reader if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) { return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context))); + parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), diagnostics_t())); } return true; } @@ -10239,6 +10341,8 @@ class binary_reader // #include +// #include + // #include // #include @@ -10294,6 +10398,7 @@ class parser using string_t = typename BasicJsonType::string_t; using lexer_t = lexer; using token_type = typename lexer_t::token_type; + using diagnostics_t = detail::diagnostics_t; public: /// a parser reading from an input adapter @@ -10333,7 +10438,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -10362,7 +10467,7 @@ class parser sdp.parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } // in case of an error, return discarded value @@ -10399,7 +10504,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"))); + exception_message(token_type::end_of_input, "value"), diagnostics_t())); } return result; @@ -10446,7 +10551,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) { @@ -10459,7 +10564,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // remember we are now inside an object @@ -10502,7 +10607,7 @@ class parser { return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'")); + out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) @@ -10573,7 +10678,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::uninitialized, "value"))); + exception_message(token_type::uninitialized, "value"), diagnostics_t())); } default: // the last token was unexpected @@ -10581,7 +10686,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::literal_or_value, "value"))); + exception_message(token_type::literal_or_value, "value"), diagnostics_t())); } } } @@ -10628,7 +10733,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_array, "array"))); + exception_message(token_type::end_array, "array"), diagnostics_t())); } else // object { @@ -10641,7 +10746,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::value_string, "object key"))); + exception_message(token_type::value_string, "object key"), diagnostics_t())); } if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) @@ -10655,7 +10760,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::name_separator, "object separator"))); + exception_message(token_type::name_separator, "object separator"), diagnostics_t())); } // parse values @@ -10684,7 +10789,7 @@ class parser return sax->parse_error(m_lexer.get_position(), m_lexer.get_token_string(), parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_object, "object"))); + exception_message(token_type::end_object, "object"), diagnostics_t())); } } } @@ -10895,6 +11000,8 @@ template struct internal_iterator #include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next #include // conditional, is_const, remove_const +// #include + // #include // #include @@ -10950,6 +11057,7 @@ class iter_impl // make sure BasicJsonType is basic_json or const basic_json static_assert(is_basic_json::type>::value, "iter_impl only accepts (const) basic_json"); + using diagnostics_t = detail::diagnostics_t; public: @@ -11156,7 +11264,7 @@ class iter_impl } case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -11165,7 +11273,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11199,7 +11307,7 @@ class iter_impl return m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11300,7 +11408,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -11337,7 +11445,7 @@ class iter_impl // if objects are not the same, the comparison is undefined if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) { - JSON_THROW(invalid_iterator::create(212, m_object->diagnostics() + "cannot compare iterators of different containers")); + JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", diagnostics_t(*m_object))); } JSON_ASSERT(m_object != nullptr); @@ -11345,7 +11453,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(213, m_object->diagnostics() + "cannot compare order of object iterators")); + JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", diagnostics_t(*m_object))); case value_t::array: return (m_it.array_iterator < other.m_it.array_iterator); @@ -11393,7 +11501,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: { @@ -11464,7 +11572,7 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(209, m_object->diagnostics() + "cannot use offsets with object iterators")); + JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", diagnostics_t(*m_object))); case value_t::array: return m_it.array_iterator - other.m_it.array_iterator; @@ -11485,13 +11593,13 @@ class iter_impl switch (m_object->m_type) { case value_t::object: - JSON_THROW(invalid_iterator::create(208, m_object->diagnostics() + "cannot use operator[] for object iterators")); + JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", diagnostics_t(*m_object))); case value_t::array: return *std::next(m_it.array_iterator, n); case value_t::null: - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); default: { @@ -11500,7 +11608,7 @@ class iter_impl return *m_object; } - JSON_THROW(invalid_iterator::create(214, m_object->diagnostics() + "cannot get value")); + JSON_THROW(invalid_iterator::create(214, "cannot get value", diagnostics_t(*m_object))); } } } @@ -11518,7 +11626,7 @@ class iter_impl return m_it.object_iterator->first; } - JSON_THROW(invalid_iterator::create(207, m_object->diagnostics() + "cannot use key() for non-object iterators")); + JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", diagnostics_t(*m_object))); } /*! @@ -11675,6 +11783,8 @@ class json_reverse_iterator : public std::reverse_iterator #include // move #include // vector +// #include + // #include // #include @@ -11691,6 +11801,8 @@ class json_pointer NLOHMANN_BASIC_JSON_TPL_DECLARATION friend class basic_json; + using diagnostics_t = detail::diagnostics_t; + public: /*! @brief create JSON pointer @@ -11917,7 +12029,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } reference_tokens.pop_back(); @@ -11941,7 +12053,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } return reference_tokens.back(); @@ -12007,15 +12119,13 @@ class json_pointer // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) { - JSON_THROW(detail::parse_error::create(106, 0, - "array index '" + s + - "' must not begin with '0'")); + JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", diagnostics_t())); } // error condition (cf. RFC 6901, Sect. 4) if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", diagnostics_t())); } std::size_t processed_chars = 0; @@ -12026,20 +12136,20 @@ class json_pointer } JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // check if the string was completely read if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", diagnostics_t())); } // only triggered on special platforms (like 32bit), see also // https://github.com/nlohmann/json/pull/2203 if (res >= static_cast((std::numeric_limits::max)())) { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type")); // LCOV_EXCL_LINE + JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", diagnostics_t())); // LCOV_EXCL_LINE } return static_cast(res); @@ -12050,7 +12160,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(empty())) { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent")); + JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", diagnostics_t())); } json_pointer result = *this; @@ -12113,7 +12223,7 @@ class json_pointer single value; that is, with an empty list of reference tokens. */ default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten")); + JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", diagnostics_t(j))); } } @@ -12185,7 +12295,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12218,7 +12328,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -12227,7 +12337,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12265,9 +12375,7 @@ class json_pointer if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) { // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", diagnostics_t(*ptr))); } // use unchecked array access @@ -12276,7 +12384,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12309,7 +12417,7 @@ class json_pointer // "-" always fails the range check JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range")); + ") is out of range", diagnostics_t(*ptr))); } // note: at performs range check @@ -12318,7 +12426,7 @@ class json_pointer } default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", diagnostics_t(*ptr))); } } @@ -12422,9 +12530,7 @@ class json_pointer // check if nonempty reference string begins with slash if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) { - JSON_THROW(detail::parse_error::create(107, 1, - "JSON pointer must be empty or begin with '/' - was: '" + - reference_string + "'")); + JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", diagnostics_t())); } // extract the reference tokens: @@ -12459,7 +12565,7 @@ class json_pointer (reference_token[pos + 1] != '0' && reference_token[pos + 1] != '1'))) { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); + JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", diagnostics_t())); } } @@ -12586,7 +12692,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!value.is_object())) { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened")); + JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", diagnostics_t(value))); } BasicJsonType result; @@ -12596,7 +12702,7 @@ class json_pointer { if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive")); + JSON_THROW(detail::type_error::create(315, "values in object must be primitive", diagnostics_t(element))); } // assign value to reference pointed to by JSON pointer; Note that if @@ -12736,6 +12842,8 @@ class json_ref #include // string #include // isnan, isinf +// #include + // #include // #include @@ -12912,7 +13020,7 @@ class binary_writer default: { - JSON_THROW(type_error::create(317, j.diagnostics() + "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()))); + JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), detail::diagnostics_t(j)));; } } } @@ -13761,8 +13869,7 @@ class binary_writer const auto it = name.find(static_cast(0)); if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { - JSON_THROW(out_of_range::create(409, j.diagnostics() + - "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")")); + JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", detail::diagnostics_t(j))); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; @@ -13886,7 +13993,7 @@ class binary_writer } else { - JSON_THROW(out_of_range::create(407, j.diagnostics() + "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64")); + JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", detail::diagnostics_t(j))); } } @@ -15574,6 +15681,8 @@ char* to_chars(char* first, const char* last, FloatType value) } // namespace detail } // namespace nlohmann +// #include + // #include // #include @@ -16066,7 +16175,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn)); + JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -16160,7 +16269,7 @@ class serializer { std::string sn(3, '\0'); (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn)); + JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, detail::diagnostics_t())); } case error_handler_t::ignore: @@ -16825,10 +16934,15 @@ class basic_json friend class ::nlohmann::detail::json_sax_dom_parser; template friend class ::nlohmann::detail::json_sax_dom_callback_parser; + template + friend class ::nlohmann::detail::diagnostics_t; /// workaround type for MSVC using basic_json_t = NLOHMANN_BASIC_JSON_TPL; + /// shortcut + using diagnostics_t = ::nlohmann::detail::diagnostics_t; + JSON_PRIVATE_UNLESS_TESTED: // convenience aliases for types residing in namespace detail; using lexer = ::nlohmann::detail::lexer_base; @@ -17696,7 +17810,7 @@ class basic_json object = nullptr; // silence warning, see #821 if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1")); // LCOV_EXCL_LINE + JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", diagnostics_t())); // LCOV_EXCL_LINE } break; } @@ -18265,7 +18379,7 @@ class basic_json // if object is wanted but impossible, throw an exception if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) { - JSON_THROW(type_error::create(301, "cannot create object from initializer list")); + JSON_THROW(type_error::create(301, "cannot create object from initializer list", diagnostics_t())); } } @@ -18586,7 +18700,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(201, diagnostics() + "iterators are not compatible")); + JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", diagnostics_t())); } // copy type from first iterator @@ -18604,7 +18718,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t())); } break; } @@ -18678,8 +18792,7 @@ class basic_json } default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + - std::string(first.m_object->type_name()))); + JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), diagnostics_t())); } assert_invariant(); @@ -19399,60 +19512,6 @@ class basic_json /// @} - std::string diagnostics() const - { -#if JSON_DIAGNOSTICS - std::vector tokens; - for (const basic_json* current = this; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (current->m_parent->m_value.array->operator[](i) == *current) - { - tokens.emplace_back(std::to_string(i)); - continue; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (element.second == *current) - { - tokens.emplace_back(element.first.c_str()); - continue; - } - } - break; - } - - default: - break; - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + b; - }) + ") "; -#else - return ""; -#endif - } - private: ////////////////// // value access // @@ -19466,7 +19525,7 @@ class basic_json return m_value.boolean; } - JSON_THROW(type_error::create(302, diagnostics() + "type must be boolean, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), diagnostics_t(*this))); } /// get a pointer to the value (object) @@ -19587,7 +19646,7 @@ class basic_json return *ptr; } - JSON_THROW(type_error::create(303, obj.diagnostics() + "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()))); + JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), diagnostics_t(obj))); } public: @@ -20015,7 +20074,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -20026,7 +20085,7 @@ class basic_json { if (!is_binary()) { - JSON_THROW(type_error::create(302, diagnostics() + "type must be binary, but is " + std::string(type_name()))); + JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), diagnostics_t(*this))); } return *get_ptr(); @@ -20087,12 +20146,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20134,12 +20193,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20191,12 +20250,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20242,12 +20301,12 @@ class basic_json JSON_CATCH (std::out_of_range&) { // create better exception explanation - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + key + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", diagnostics_t(*this))); } } else { - JSON_THROW(type_error::create(304, diagnostics() + "cannot use at() with " + std::string(type_name()))); + JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -20311,7 +20370,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20341,7 +20400,7 @@ class basic_json return m_value.array->operator[](idx); } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a numeric argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20393,7 +20452,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20435,7 +20494,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20489,7 +20548,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20533,7 +20592,7 @@ class basic_json return m_value.object->find(key)->second; } - JSON_THROW(type_error::create(305, diagnostics() + "cannot use operator[] with a string argument with " + std::string(type_name()))); + JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20605,7 +20664,7 @@ class basic_json return default_value; } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20678,7 +20737,7 @@ class basic_json } } - JSON_THROW(type_error::create(306, diagnostics() + "cannot use value() with " + std::string(type_name()))); + JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -20832,7 +20891,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -20848,7 +20907,7 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) { - JSON_THROW(invalid_iterator::create(205, "iterator out of range")); + JSON_THROW(invalid_iterator::create(205, "iterator out of range", diagnostics_t(*this))); } if (is_string()) @@ -20884,7 +20943,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -20945,7 +21004,7 @@ class basic_json // make sure iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) { - JSON_THROW(invalid_iterator::create(203, diagnostics() + "iterators do not fit current value")); + JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", diagnostics_t(*this))); } IteratorType result = end(); @@ -20962,7 +21021,7 @@ class basic_json if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() || !last.m_it.primitive_iterator.is_end())) { - JSON_THROW(invalid_iterator::create(204, diagnostics() + "iterators out of range")); + JSON_THROW(invalid_iterator::create(204, "iterators out of range", diagnostics_t(*this))); } if (is_string()) @@ -21000,7 +21059,7 @@ class basic_json } default: - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } return result; @@ -21043,7 +21102,7 @@ class basic_json return m_value.object->erase(key); } - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -21077,14 +21136,14 @@ class basic_json { if (JSON_HEDLEY_UNLIKELY(idx >= size())) { - JSON_THROW(out_of_range::create(401, diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(*this))); } m_value.array->erase(m_value.array->begin() + static_cast(idx)); } else { - JSON_THROW(type_error::create(307, diagnostics() + "cannot use erase() with " + std::string(type_name()))); + JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22029,7 +22088,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22067,7 +22126,7 @@ class basic_json // push_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22120,7 +22179,7 @@ class basic_json // push_back only works for null objects or objects if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(308, diagnostics() + "cannot use push_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -22228,7 +22287,7 @@ class basic_json // emplace_back only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace_back() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an array @@ -22290,7 +22349,7 @@ class basic_json // emplace only works for null objects or arrays if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) { - JSON_THROW(type_error::create(311, diagnostics() + "cannot use emplace() with " + std::string(type_name()))); + JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), diagnostics_t(*this))); } // transform null object into an object @@ -22366,7 +22425,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22379,7 +22438,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -22423,7 +22482,7 @@ class basic_json // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22439,7 +22498,7 @@ class basic_json #endif } - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } /*! @@ -22477,24 +22536,24 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) { - JSON_THROW(invalid_iterator::create(211, diagnostics() + "passed iterators may not belong to container")); + JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", diagnostics_t(*this))); } // insert to array and return iterator @@ -22539,13 +22598,13 @@ class basic_json // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterator does not fit current value")); + JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", diagnostics_t(*this))); } // insert to array and return iterator @@ -22590,19 +22649,19 @@ class basic_json // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(309, diagnostics() + "cannot use insert() with " + std::string(type_name()))); + JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); @@ -22639,11 +22698,11 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } if (JSON_HEDLEY_UNLIKELY(!j.is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(j.type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), diagnostics_t(*this))); } for (auto it = j.cbegin(); it != j.cend(); ++it) @@ -22690,20 +22749,20 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(!is_object())) { - JSON_THROW(type_error::create(312, diagnostics() + "cannot use update() with " + std::string(type_name()))); + JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), diagnostics_t(*this))); } // check if range iterators belong to the same JSON object if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) { - JSON_THROW(invalid_iterator::create(210, diagnostics() + "iterators do not fit")); + JSON_THROW(invalid_iterator::create(210, "iterators do not fit", diagnostics_t(*this))); } // passed iterators must belong to objects if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() || !last.m_object->is_object())) { - JSON_THROW(invalid_iterator::create(202, diagnostics() + "iterators first and last must point to objects")); + JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", diagnostics_t(*this))); } for (auto it = first; it != last; ++it) @@ -22798,7 +22857,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22831,7 +22890,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22864,7 +22923,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22897,7 +22956,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -22911,7 +22970,7 @@ class basic_json } else { - JSON_THROW(type_error::create(310, diagnostics() + "cannot use swap() with " + std::string(type_name()))); + JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), diagnostics_t(*this))); } } @@ -25120,7 +25179,7 @@ class basic_json if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) { // avoid undefined behavior - JSON_THROW(out_of_range::create(401, parent.diagnostics() + "array index " + std::to_string(idx) + " is out of range")); + JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", diagnostics_t(parent))); } // default case: insert add offset @@ -25154,7 +25213,7 @@ class basic_json } else { - JSON_THROW(out_of_range::create(403, diagnostics() + "key '" + last_path + "' not found")); + JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", diagnostics_t(*this))); } } else if (parent.is_array()) @@ -25167,7 +25226,7 @@ class basic_json // type check: top level value must be an array if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(json_patch))); } // iterate and apply the operations @@ -25187,13 +25246,13 @@ class basic_json // check if desired value is present if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", diagnostics_t(val))); } // check if result is of type string if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) { - JSON_THROW(parse_error::create(105, 0, diagnostics() + error_msg + " must have string member '" + member + "'")); + JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", diagnostics_t(val))); } // no error: return value @@ -25203,7 +25262,7 @@ class basic_json // type check: every element of the array must be an object if (JSON_HEDLEY_UNLIKELY(!val.is_object())) { - JSON_THROW(parse_error::create(104, 0, diagnostics() + "JSON patch must be an array of objects")); + JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", diagnostics_t(val))); } // collect mandatory members @@ -25281,7 +25340,7 @@ class basic_json // throw an exception if test fails if (JSON_HEDLEY_UNLIKELY(!success)) { - JSON_THROW(other_error::create(501, val.diagnostics() + "unsuccessful: " + val.dump())); + JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), diagnostics_t(val))); } break; @@ -25291,7 +25350,7 @@ class basic_json { // op must be "add", "remove", "replace", "move", "copy", or // "test" - JSON_THROW(parse_error::create(105, 0, diagnostics() + "operation value '" + op + "' is invalid")); + JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", diagnostics_t(val))); } } } diff --git a/test/src/unit-json_patch.cpp b/test/src/unit-json_patch.cpp index 53570113b..9d1966f0a 100644 --- a/test/src/unit-json_patch.cpp +++ b/test/src/unit-json_patch.cpp @@ -692,7 +692,11 @@ TEST_CASE("JSON patch") json patch = {"op", "add", "path", "", "value", 1}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.104] parse error: (/0) JSON patch must be an array of objects"); +#else "[json.exception.parse_error.104] parse error: JSON patch must be an array of objects"); +#endif } SECTION("missing 'op'") @@ -701,7 +705,11 @@ TEST_CASE("JSON patch") json patch = {{{"foo", "bar"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation must have member 'op'"); +#else "[json.exception.parse_error.105] parse error: operation must have member 'op'"); +#endif } SECTION("non-string 'op'") @@ -710,7 +718,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation must have string member 'op'"); +#else "[json.exception.parse_error.105] parse error: operation must have string member 'op'"); +#endif } SECTION("invalid operation") @@ -719,7 +731,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "foo"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation value 'foo' is invalid"); +#else "[json.exception.parse_error.105] parse error: operation value 'foo' is invalid"); +#endif } } @@ -731,7 +747,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -740,7 +760,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -749,7 +773,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "add"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'add' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'add' must have member 'value'"); +#endif } SECTION("invalid array index") @@ -770,7 +798,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "remove"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'remove' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -779,7 +811,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "remove"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'remove' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'remove' must have string member 'path'"); +#endif } SECTION("nonexisting target location (array)") @@ -818,7 +854,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -827,7 +867,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -836,7 +880,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "replace"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'replace' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'replace' must have member 'value'"); +#endif } SECTION("nonexisting target location (array)") @@ -866,7 +914,12 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), - "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'"); +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'path'" +#else + "[json.exception.parse_error.105] parse error: operation 'move' must have member 'path'" +#endif + ); } SECTION("non-string 'path'") @@ -875,7 +928,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'path'"); +#endif } SECTION("missing 'from'") @@ -884,7 +941,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have member 'from'"); +#endif } SECTION("non-string 'from'") @@ -893,7 +954,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "move"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'move' must have string member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'move' must have string member 'from'"); +#endif } SECTION("nonexisting from location (array)") @@ -923,7 +988,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -932,7 +1001,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'path'"); +#endif } SECTION("missing 'from'") @@ -941,7 +1014,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have member 'from'"); +#endif } SECTION("non-string 'from'") @@ -950,7 +1027,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "copy"}, {"path", ""}, {"from", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'copy' must have string member 'from'"); +#else "[json.exception.parse_error.105] parse error: operation 'copy' must have string member 'from'"); +#endif } SECTION("nonexisting from location (array)") @@ -980,7 +1061,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have member 'path'"); +#endif } SECTION("non-string 'path'") @@ -989,7 +1074,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}, {"path", 1}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have string member 'path'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have string member 'path'"); +#endif } SECTION("missing 'value'") @@ -998,7 +1087,11 @@ TEST_CASE("JSON patch") json patch = {{{"op", "test"}, {"path", ""}}}; CHECK_THROWS_AS(j.patch(patch), json::parse_error&); CHECK_THROWS_WITH(j.patch(patch), +#if JSON_DIAGNOSTICS + "[json.exception.parse_error.105] parse error: (/0) operation 'test' must have member 'value'"); +#else "[json.exception.parse_error.105] parse error: operation 'test' must have member 'value'"); +#endif } } }