Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/nlohmann/detail/meta/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,13 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;


template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr bool value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
///////////////////////////////////////////////////////////////////////////////
Expand Down
27 changes: 21 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3694,9 +3694,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
bool operator==(ScalarType rhs) const noexcept
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator==(T rhs) const noexcept
{
return *this == basic_json(rhs);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator!=(T rhs) const noexcept
{
return *this != basic_json(rhs);
}

/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(std::nullptr_t rhs) const noexcept
{
return *this == basic_json(rhs);
}
Expand Down Expand Up @@ -3727,9 +3742,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: 3-way
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
{
return *this <=> basic_json(rhs); // *NOPAD*
}
Expand Down
34 changes: 28 additions & 6 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4091,6 +4091,13 @@ inline constexpr bool value_in_range_of(T val)
template<bool Value>
using bool_constant = std::integral_constant<bool, Value>;


template <typename T, typename BasicJsonType, typename U = uncvref_t<T>>
struct json_compatible_type
{
static constexpr bool value = !is_basic_json<U>::value && is_compatible_type<BasicJsonType, U>::value;
};

///////////////////////////////////////////////////////////////////////////////
// is_c_string
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -22908,9 +22915,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
bool operator==(ScalarType rhs) const noexcept
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator==(T rhs) const noexcept
{
return *this == basic_json(rhs);
}

/// @brief comparison: not equal
/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
bool operator!=(T rhs) const noexcept
{
return *this != basic_json(rhs);
}

/// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
bool operator==(std::nullptr_t rhs) const noexcept
{
return *this == basic_json(rhs);
}
Expand Down Expand Up @@ -22941,9 +22963,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

/// @brief comparison: 3-way
/// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
template<typename ScalarType>
requires std::is_scalar_v<ScalarType>
std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
template <typename T>
requires detail::json_compatible_type<T, basic_json_t>::value
std::partial_ordering operator<=>(T rhs) const noexcept // *NOPAD*
{
return *this <=> basic_json(rhs); // *NOPAD*
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/unit-comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ TEST_CASE("lexicographical comparison operators")
CAPTURE(i)
CAPTURE(j)
CHECK((j_values[i] == j_values[j]) == expected_eq[i][j]);
CHECK(expected_eq[i][j] == (j_values[i] == j_values[j]));
}
}

Expand Down