Skip to content

Commit

Permalink
Allow comparing different json_pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Apr 7, 2022
1 parent 03dbf3e commit f5f975a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
33 changes: 23 additions & 10 deletions include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -836,11 +836,10 @@ class json_pointer
@exceptionsafety No-throw guarantee: this function never throws exceptions.
*/
friend bool operator==(json_pointer const& lhs,
json_pointer const& rhs) noexcept
{
return lhs.reference_tokens == rhs.reference_tokens;
}
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept;

/*!
@brief compares two JSON pointers for inequality
Expand All @@ -853,13 +852,27 @@ class json_pointer
@exceptionsafety No-throw guarantee: this function never throws exceptions.
*/
friend bool operator!=(json_pointer const& lhs,
json_pointer const& rhs) noexcept
{
return !(lhs == rhs);
}
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept;

/// the reference tokens
std::vector<string_t> reference_tokens;
};

// functions cannot be defined inside class due to ODR violations
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens == rhs.reference_tokens;
}

template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return !(lhs == rhs);
}
} // namespace nlohmann
33 changes: 23 additions & 10 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13301,11 +13301,10 @@ class json_pointer

@exceptionsafety No-throw guarantee: this function never throws exceptions.
*/
friend bool operator==(json_pointer const& lhs,
json_pointer const& rhs) noexcept
{
return lhs.reference_tokens == rhs.reference_tokens;
}
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept;

/*!
@brief compares two JSON pointers for inequality
Expand All @@ -13318,15 +13317,29 @@ class json_pointer

@exceptionsafety No-throw guarantee: this function never throws exceptions.
*/
friend bool operator!=(json_pointer const& lhs,
json_pointer const& rhs) noexcept
{
return !(lhs == rhs);
}
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
// NOLINTNEXTLINE(readability-redundant-declaration)
friend bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept;

/// the reference tokens
std::vector<string_t> reference_tokens;
};

// functions cannot be defined inside class due to ODR violations
template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator==(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return lhs.reference_tokens == rhs.reference_tokens;
}

template<typename RefStringTypeLhs, typename RefStringTypeRhs>
inline bool operator!=(json_pointer<RefStringTypeLhs> const& lhs,
json_pointer<RefStringTypeRhs> const& rhs) noexcept
{
return !(lhs == rhs);
}
} // namespace nlohmann

// #include <nlohmann/detail/json_ref.hpp>
Expand Down
14 changes: 14 additions & 0 deletions test/src/unit-json_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,15 @@ TEST_CASE("JSON pointers")
CHECK(ptr.to_string() == "/object/~1");
}

SECTION("equality comparison")
{
auto ptr1 = json::json_pointer("/foo/bar");
auto ptr2 = json::json_pointer("/foo/bar");

CHECK(ptr1 == ptr2);
CHECK_FALSE(ptr1 != ptr2);
}

SECTION("backwards compatibility and mixing")
{
json j = R"(
Expand Down Expand Up @@ -695,5 +704,10 @@ TEST_CASE("JSON pointers")

CHECK(j.value(ptr, "x") == j.value(ptr_j, "x"));
CHECK(j.value(ptr, "x") == j.value(ptr_oj, "x"));

CHECK(ptr == ptr_j);
CHECK(ptr == ptr_oj);
CHECK_FALSE(ptr != ptr_j);
CHECK_FALSE(ptr != ptr_oj);
}
}

0 comments on commit f5f975a

Please sign in to comment.