From 93ccdc2738fc020a2ba5b44dbf57310ee779e0e6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 26 Oct 2026 07:13:17 +0100 Subject: [PATCH] json_view: enable_if_not_view_t instead of '!' in the from_json() templates astyle spaces out the template argument list when it contains a negation; the alias spells the condition with negation<> and leaves the lines alone. Signed-off-by: Niels Lohmann --- .../nlohmann/detail/conversions/from_json.hpp | 23 +++++++++++-------- single_include/nlohmann/json.hpp | 23 +++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 4cd1e4391..07b02caaf 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -50,8 +50,13 @@ namespace detail // json_type_of_t its basic_json type). The helpers below are // the accesses that differ between the two. +// the enable_if parameter of the overloads that read basic_json values (written +// with negation<> because astyle spaces out a template argument list with '!') +template +using enable_if_not_view_t = enable_if_t>::value, int>; + // the boolean of j, a boolean -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > typename BasicJsonType::boolean_t boolean_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -64,7 +69,7 @@ bool boolean_value(const View& v) } // the string of j, a string -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::string_t& string_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -79,7 +84,7 @@ typename View::string_t string_value(const View& v) // the binary value of j, a binary value (never a view: a view of a JSON text // holds no binary values) -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::binary_t& binary_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -92,7 +97,7 @@ typename View::basic_json_type::binary_t binary_value(const View& /*unused*/) } // the elements of j, an array, for a range-based for loop -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::array_t& array_elements(const BasicJsonType& j) { return *j.template get_ptr(); @@ -132,7 +137,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) // read a number value of any of the three number types into val; // returns false (leaving val untouched) if j is not a number -template < typename BasicJsonType, typename ArithmeticType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename ArithmeticType, enable_if_not_view_t = 0 > bool get_number_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) @@ -324,7 +329,7 @@ auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines } // (for a view, array_t is read element by element below) -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) { arr = *j.template get_ptr(); @@ -333,7 +338,7 @@ inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType: // fill a std::array from the JSON array j: the size is checked once and the // elements are read in order, so a short array converts the elements it has and // then throws the out_of_range error at() would throw for the first missing one -template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename T, std::size_t N, enable_if_not_view_t = 0 > void from_json_std_array(const BasicJsonType& j, std::array& arr) { const auto& inner_array = *j.template get_ptr(); @@ -556,7 +561,7 @@ void from_json_object_assign(ConstructibleObjectType& obj, Key&& key, Value&& va obj.emplace(std::forward(key), std::forward(value)); } -template < typename BasicJsonType, typename ConstructibleObjectType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename ConstructibleObjectType, enable_if_not_view_t = 0 > void from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& ret) { const auto* inner_object = j.template get_ptr(); @@ -715,7 +720,7 @@ void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) // read a map with enum keys from an object, using the enum's own from_json for // the keys (e.g., from NLOHMANN_JSON_SERIALIZE_ENUM); this is the form written // with JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS -template < typename BasicJsonType, typename Map, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename Map, enable_if_not_view_t = 0 > inline bool from_json_enum_keyed_object(const BasicJsonType& j, Map& m, std::true_type /*key is enum*/) { if (!j.is_object()) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 10756f5ba..bcfc0324f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -6173,8 +6173,13 @@ namespace detail // json_type_of_t its basic_json type). The helpers below are // the accesses that differ between the two. +// the enable_if parameter of the overloads that read basic_json values (written +// with negation<> because astyle spaces out a template argument list with '!') +template +using enable_if_not_view_t = enable_if_t>::value, int>; + // the boolean of j, a boolean -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > typename BasicJsonType::boolean_t boolean_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -6187,7 +6192,7 @@ bool boolean_value(const View& v) } // the string of j, a string -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::string_t& string_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -6202,7 +6207,7 @@ typename View::string_t string_value(const View& v) // the binary value of j, a binary value (never a view: a view of a JSON text // holds no binary values) -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::binary_t& binary_value(const BasicJsonType& j) { return *j.template get_ptr(); @@ -6215,7 +6220,7 @@ typename View::basic_json_type::binary_t binary_value(const View& /*unused*/) } // the elements of j, an array, for a range-based for loop -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > const typename BasicJsonType::array_t& array_elements(const BasicJsonType& j) { return *j.template get_ptr(); @@ -6255,7 +6260,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) // read a number value of any of the three number types into val; // returns false (leaving val untouched) if j is not a number -template < typename BasicJsonType, typename ArithmeticType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename ArithmeticType, enable_if_not_view_t = 0 > bool get_number_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) @@ -6447,7 +6452,7 @@ auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines } // (for a view, array_t is read element by element below) -template < typename BasicJsonType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, enable_if_not_view_t = 0 > inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) { arr = *j.template get_ptr(); @@ -6456,7 +6461,7 @@ inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType: // fill a std::array from the JSON array j: the size is checked once and the // elements are read in order, so a short array converts the elements it has and // then throws the out_of_range error at() would throw for the first missing one -template < typename BasicJsonType, typename T, std::size_t N, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename T, std::size_t N, enable_if_not_view_t = 0 > void from_json_std_array(const BasicJsonType& j, std::array& arr) { const auto& inner_array = *j.template get_ptr(); @@ -6679,7 +6684,7 @@ void from_json_object_assign(ConstructibleObjectType& obj, Key&& key, Value&& va obj.emplace(std::forward(key), std::forward(value)); } -template < typename BasicJsonType, typename ConstructibleObjectType, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename ConstructibleObjectType, enable_if_not_view_t = 0 > void from_json_object_impl(const BasicJsonType& j, ConstructibleObjectType& ret) { const auto* inner_object = j.template get_ptr(); @@ -6838,7 +6843,7 @@ void from_json_pair_array_to_map(const BasicJsonType& j, MapType& m) // read a map with enum keys from an object, using the enum's own from_json for // the keys (e.g., from NLOHMANN_JSON_SERIALIZE_ENUM); this is the form written // with JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS -template < typename BasicJsonType, typename Map, enable_if_t < !is_json_view::value, int > = 0 > +template < typename BasicJsonType, typename Map, enable_if_not_view_t = 0 > inline bool from_json_enum_keyed_object(const BasicJsonType& j, Map& m, std::true_type /*key is enum*/) { if (!j.is_object())