mirror of
https://github.com/nlohmann/json.git
synced 2026-11-02 10:55:35 +00:00
json_view: exceptions name the path and the bytes of the value (#5974)
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 parent
f022e3c59c
commit
ddc34f546a
25 files changed
+1600
-696
No files matched your search
Generated
+24
-2
@@ -2720,7 +2720,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP
|
||||
return ej_pair.second == j; \
|
||||
}); \
|
||||
if (it != std::end(m)) e = it->first; \
|
||||
else ::nlohmann::detail::templated_json_throw<nlohmann::detail::out_of_range>(nlohmann::detail::out_of_range::create(nlohmann::detail::exception_id::value_out_of_range, nlohmann::detail::concat("enum value out of range for " #ENUM_TYPE ": ", nlohmann::detail::from_json_dump(j)), nlohmann::detail::exception_context(j))); \
|
||||
else ::nlohmann::detail::templated_json_throw<nlohmann::detail::out_of_range>(nlohmann::detail::out_of_range::create(nlohmann::detail::exception_id::value_out_of_range, nlohmann::detail::concat(nlohmann::detail::exception_prefix(j), "enum value out of range for " #ENUM_TYPE ": ", nlohmann::detail::from_json_dump(j)), nlohmann::detail::exception_context(j))); \
|
||||
}
|
||||
|
||||
// Ugly macros to avoid uglier copy-paste when specializing basic_json. They
|
||||
@@ -5928,6 +5928,28 @@ std::nullptr_t exception_context(const View& /*unused*/) noexcept
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
namespace view
|
||||
{
|
||||
// the path and bytes of the value of a view (defined in <nlohmann/detail/view/errors.hpp>)
|
||||
template<typename View>
|
||||
std::string diagnostics_of(const View& v);
|
||||
} // namespace view
|
||||
|
||||
// what goes in front of the message of an exception about j: nothing for a
|
||||
// basic_json value (exception_context() passes the value itself), the context
|
||||
// basic_json would add (JSON_DIAGNOSTICS, JSON_DIAGNOSTIC_POSITIONS) for a view
|
||||
template<typename BasicJsonType, enable_if_t<is_basic_json<BasicJsonType>::value, int> = 0>
|
||||
const char* exception_prefix(const BasicJsonType& /*unused*/) noexcept
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
template<typename View, enable_if_t<is_json_view<View>::value, int> = 0>
|
||||
std::string exception_prefix(const View& v)
|
||||
{
|
||||
return view::diagnostics_of(v);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief throws because @a j does not have the type a conversion expects
|
||||
@param[in] expected the expected type(s), e.g. "array" or "binary or array"
|
||||
@@ -5939,7 +5961,7 @@ JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE void throw_type_must_be(const cha
|
||||
{
|
||||
static_cast<void>(expected); // unused when JSON_NOEXCEPTION is defined
|
||||
static_cast<void>(j);
|
||||
JSON_THROW(type_error::create(exception_id::type_mismatch, concat("type must be ", expected, ", but is ", j.type_name()), exception_context(j)));
|
||||
JSON_THROW(type_error::create(exception_id::type_mismatch, concat(exception_prefix(j), "type must be ", expected, ", but is ", j.type_name()), exception_context(j)));
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
Generated
+827
-505
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user