// until C++20 template<typename RefStringTypeLhs, typename RefStringTypeRhs> bool operator==( const json_pointer<RefStringTypeLhs>& lhs, const json_pointer<RefStringTypeRhs>& rhs) noexcept; // (1) template<typename RefStringTypeLhs, typename StringType> bool operator==( const json_pointer<RefStringTypeLhs>& lhs, const StringType& rhs); // (2) template<typename RefStringTypeRhs, typename StringType> bool operator==( const StringType& lhs, const json_pointer<RefStringTypeRhs>& rhs); // (2) // since C++20 template<typename RefStringTypeRhs> bool operator==( const json_pointer<RefStringTypeRhs>& rhs) const noexcept; // (1) bool operator==(const string_t& rhs) const; // (2)
Compares two JSON pointers for equality by comparing their reference tokens.
Compares a JSON pointer and a string or a string and a JSON pointer for equality by converting the string to a JSON pointer and comparing the JSON pointers according to 1.
RefStringTypeLhs, RefStringTypeRhs : the string type of the left-hand side or right-hand side JSON pointer, respectively
StringType : the string type derived from the json_pointer operand (json_pointer::string_t)
lhs (in) : first value to consider
rhs (in) : second value to consider
whether the values lhs/*this and rhs are equal
s is nonempty and does not begin with a slash (/); see example below.~) in the given JSON pointer s is not followed by 0 (representing ~) or 1 (representing /); see example below.Constant if lhs and rhs differ in the number of reference tokens, otherwise linear in the number of reference tokens.
!!! warning “Deprecation”
Overload 2 is deprecated and will be removed in a future major version release. See the [migration guide](../../integration/migration_guide.md#json-pointers) for how to update existing code.
??? example “Example: (1) Comparing JSON pointers”
The example demonstrates comparing JSON pointers.
```cpp
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// compare JSON pointers
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" == \"" << ptr0 << "\": " << (ptr0 == ptr0) << '\n'
<< "\"" << ptr0 << "\" == \"" << ptr1 << "\": " << (ptr0 == ptr1) << '\n'
<< "\"" << ptr1 << "\" == \"" << ptr2 << "\": " << (ptr1 == ptr2) << '\n'
<< "\"" << ptr2 << "\" == \"" << ptr2 << "\": " << (ptr2 == ptr2) << std::endl;
}
```
Output:
```
"" == "": true
"" == "": true
"" == "/foo": false
"/foo" == "/foo": true
```
??? example “Example: (2) Comparing JSON pointers and strings”
The example demonstrates comparing JSON pointers and strings, and when doing so may raise an exception.
```cpp
#include <exception>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
// different JSON pointers
json::json_pointer ptr0;
json::json_pointer ptr1("");
json::json_pointer ptr2("/foo");
// different strings
std::string str0("");
std::string str1("/foo");
std::string str2("bar");
// compare JSON pointers and strings
std::cout << std::boolalpha
<< "\"" << ptr0 << "\" == \"" << str0 << "\": " << (ptr0 == str0) << '\n'
<< "\"" << str0 << "\" == \"" << ptr1 << "\": " << (str0 == ptr1) << '\n'
<< "\"" << ptr2 << "\" == \"" << str1 << "\": " << (ptr2 == str1) << std::endl;
try
{
std::cout << "\"" << str2 << "\" == \"" << ptr2 << "\": " << (str2 == ptr2) << std::endl;
}
catch (const json::parse_error& ex)
{
std::cout << ex.what() << std::endl;
}
}
```
Output:
```
"" == "": true
"" == "": true
"/foo" == "/foo": true
"bar" == "/foo": [json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'bar'
```