1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-28 12:02:00 +03:00

Make json_pointer usable as map key (again) (#3685)

* Make json_pointer usable as map key

* Add unit tests
This commit is contained in:
Florian Albrechtskirchinger
2022-08-07 14:35:40 +02:00
committed by GitHub
parent 231f3108ff
commit 31265dc69c
3 changed files with 71 additions and 4 deletions

View File

@ -14507,7 +14507,7 @@ class json_pointer
}
public:
#ifdef JSON_HAS_CPP_20
#if JSON_HAS_THREE_WAY_COMPARISON
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
template<typename RefStringTypeRhs>
@ -14522,6 +14522,13 @@ class json_pointer
{
return *this == json_pointer(rhs);
}
/// @brief 3-way compares two JSON pointers
template<typename RefStringTypeRhs>
std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
{
return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
}
#else
/// @brief compares two JSON pointers for equality
/// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
@ -14564,6 +14571,12 @@ class json_pointer
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(const StringType& lhs,
const json_pointer<RefStringTypeRhs>& rhs);
/// @brief compares two JSON pointer for less-than
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept;
#endif
private:
@ -14571,7 +14584,7 @@ class json_pointer
std::vector<string_t> reference_tokens;
};
#ifndef JSON_HAS_CPP_20
#if !JSON_HAS_THREE_WAY_COMPARISON
// functions cannot be defined inside class due to ODR violations
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
@ -14618,6 +14631,13 @@ inline bool operator!=(const StringType& lhs,
{
return !(lhs == rhs);
}
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
const json_pointer<RefStringTypeRhs>& rhs) noexcept
{
return lhs.reference_tokens < rhs.reference_tokens;
}
#endif
NLOHMANN_JSON_NAMESPACE_END