Compare commits

...
6 Commits
Author SHA1 Message Date
Niels Lohmann 3b632e489a Merge #5901 fixes
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 18:59:12 +02:00
Niels Lohmann 2852427b3f json_sax: assert the invariant of place_container for the static analyzer
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 18:59:10 +02:00
Niels Lohmann 8085a8f5ec Use the compact template<...> style for the new from_json overloads
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 18:52:03 +02:00
Niels Lohmann cf4e0164c4 Shorten from_json_inplace_array so astyle aligns its parameters
The opening parenthesis of the previous name sat in column 41, beyond
astyle's default --max-continuation-indent of 40, so the continuation
lines fell back to a plain double indent.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 18:34:29 +02:00
Niels Lohmann 3562da8556 Merge #5901 fixes 2026-10-24 17:06:29 +02:00
Niels Lohmann e30d2aed18 json_view: use exception_id in the value() overloads
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-24 17:06:28 +02:00
5 changed files with 22 additions and 18 deletions

No files matched your search

@@ -375,18 +375,18 @@ struct is_trivial_element
// the element type may not be default constructible: construct every element
// in place (a pack expansion that is unrolled N times)
template < typename BasicJsonType, typename T, std::size_t N >
std::array<T, N> from_json_inplace_array(const BasicJsonType& j, identity_tag<std::array<T, N>> tag,
std::false_type /*is_trivial_element*/)
template<typename BasicJsonType, typename T, std::size_t N>
std::array<T, N> from_json_make_array(const BasicJsonType& j, identity_tag<std::array<T, N>> tag,
std::false_type /*is_trivial_element*/)
{
return from_json_inplace_array_impl(j, tag, make_index_sequence<N> {});
}
// a trivial element type cannot tell default construction plus assignment from
// construction in place, so use a loop instead of unrolling N conversions
template < typename BasicJsonType, typename T, std::size_t N >
std::array<T, N> from_json_inplace_array(const BasicJsonType& j, identity_tag<std::array<T, N>> /*unused*/,
std::true_type /*is_trivial_element*/)
template<typename BasicJsonType, typename T, std::size_t N>
std::array<T, N> from_json_make_array(const BasicJsonType& j, identity_tag<std::array<T, N>> /*unused*/,
std::true_type /*is_trivial_element*/)
{
std::array<T, N> arr; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): every element is assigned or an exception is thrown
from_json_std_array(j, arr);
@@ -402,7 +402,7 @@ auto from_json(const BasicJsonType& j, identity_tag<std::array<T, N>> tag)
throw_type_must_be("array", j);
}
return from_json_inplace_array(j, tag, is_trivial_element<T> {});
return from_json_make_array(j, tag, is_trivial_element<T> {});
}
template<typename BasicJsonType>
@@ -937,6 +937,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state
/// directly if the value was the one too many.
void place_container(const frame& f, frame* enclosing, BasicJsonType&& value, const bool enclosing_is_innermost)
{
// only a container with a slot may have no enclosing container
JSON_ASSERT(f.slot != nullptr || enclosing != nullptr);
if (f.slot != nullptr)
{
*f.slot = std::move(value);
+2 -2
View File
@@ -395,7 +395,7 @@ class basic_json_view
{
if (NLOHMANN_VIEW_UNLIKELY(!is_object()))
{
detail::view::throw_type_error(306, "cannot use value() with ", type_name());
detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name());
}
// (the default is made only if there is no such member)
const basic_json_view r = lookup(key);
@@ -420,7 +420,7 @@ class basic_json_view
{
if (NLOHMANN_VIEW_UNLIKELY(!is_structured()))
{
detail::view::throw_type_error(306, "cannot use value() with ", type_name());
detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name());
}
const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value);
return r.is_discarded() ? string_t(default_value) : r.template get<string_t>();
+9 -7
View File
@@ -6399,18 +6399,18 @@ struct is_trivial_element
// the element type may not be default constructible: construct every element
// in place (a pack expansion that is unrolled N times)
template < typename BasicJsonType, typename T, std::size_t N >
std::array<T, N> from_json_inplace_array(const BasicJsonType& j, identity_tag<std::array<T, N>> tag,
std::false_type /*is_trivial_element*/)
template<typename BasicJsonType, typename T, std::size_t N>
std::array<T, N> from_json_make_array(const BasicJsonType& j, identity_tag<std::array<T, N>> tag,
std::false_type /*is_trivial_element*/)
{
return from_json_inplace_array_impl(j, tag, make_index_sequence<N> {});
}
// a trivial element type cannot tell default construction plus assignment from
// construction in place, so use a loop instead of unrolling N conversions
template < typename BasicJsonType, typename T, std::size_t N >
std::array<T, N> from_json_inplace_array(const BasicJsonType& j, identity_tag<std::array<T, N>> /*unused*/,
std::true_type /*is_trivial_element*/)
template<typename BasicJsonType, typename T, std::size_t N>
std::array<T, N> from_json_make_array(const BasicJsonType& j, identity_tag<std::array<T, N>> /*unused*/,
std::true_type /*is_trivial_element*/)
{
std::array<T, N> arr; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): every element is assigned or an exception is thrown
from_json_std_array(j, arr);
@@ -6426,7 +6426,7 @@ auto from_json(const BasicJsonType& j, identity_tag<std::array<T, N>> tag)
throw_type_must_be("array", j);
}
return from_json_inplace_array(j, tag, is_trivial_element<T> {});
return from_json_make_array(j, tag, is_trivial_element<T> {});
}
template<typename BasicJsonType>
@@ -16060,6 +16060,8 @@ class json_sax_dom_parser : public json_sax_dom_error_state
/// directly if the value was the one too many.
void place_container(const frame& f, frame* enclosing, BasicJsonType&& value, const bool enclosing_is_innermost)
{
// only a container with a slot may have no enclosing container
JSON_ASSERT(f.slot != nullptr || enclosing != nullptr);
if (f.slot != nullptr)
{
*f.slot = std::move(value);
+2 -2
View File
@@ -8082,7 +8082,7 @@ class basic_json_view
{
if (NLOHMANN_VIEW_UNLIKELY(!is_object()))
{
detail::view::throw_type_error(306, "cannot use value() with ", type_name());
detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name());
}
// (the default is made only if there is no such member)
const basic_json_view r = lookup(key);
@@ -8107,7 +8107,7 @@ class basic_json_view
{
if (NLOHMANN_VIEW_UNLIKELY(!is_structured()))
{
detail::view::throw_type_error(306, "cannot use value() with ", type_name());
detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name());
}
const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value);
return r.is_discarded() ? string_t(default_value) : r.template get<string_t>();