Skip to content

nlohmann::json_pointer::operator==

// until C++20
template<typenameRefStringTypeLhs,typenameRefStringTypeRhs>
booloperator==(
constjson_pointer<RefStringTypeLhs>&lhs,
constjson_pointer<RefStringTypeRhs>&rhs)noexcept;// (1)
template<typenameRefStringTypeLhs,typenameStringType>
booloperator==(
constjson_pointer<RefStringTypeLhs>&lhs,
constStringType&rhs);// (2)
template<typenameRefStringTypeRhs,typenameStringType>
booloperator==(
constStringType&lhs,
constjson_pointer<RefStringTypeRhs>&rhs);// (2)
// since C++20
classjson_pointer{
template<typenameRefStringTypeRhs>
booloperator==(
constjson_pointer<RefStringTypeRhs>&rhs)constnoexcept;// (1)
booloperator==(conststring_t&rhs)const;// (2)
};
  1. Compares two JSON pointers for equality by comparing their reference tokens.

  2. 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.

Template parameters

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)

Parameters

lhs (in)
first value to consider
rhs (in)
second value to consider

Return value

whether the values lhs/*this and rhs are equal

Exception safety

  1. No-throw guarantee: this function never throws exceptions.
  2. Strong exception safety: if an exception occurs, the original value stays intact.

Exceptions

  1. (none)
  2. The function can throw the following exceptions:
  3. Throws parse_error.107 if the given JSON pointer s is nonempty and does not begin with a slash (/); see example below.
  4. Throws parse_error.108 if a tilde (~) in the given JSON pointer s is not followed by 0 (representing ~) or 1 (representing /); see example below.

Complexity

Constant if lhs and rhs differ in the number of reference tokens, otherwise linear in the number of reference tokens.

Notes

Deprecation

Overload 2 is deprecated and will be removed in a future major version release.

Examples

Example: (1) Comparing JSON pointers

The example demonstrates comparing JSON pointers.

#include<iostream>
#include<nlohmann/json.hpp>
usingjson=nlohmann::json;
intmain()
{
// different JSON pointers
json::json_pointerptr0;
json::json_pointerptr1("");
json::json_pointerptr2("/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: (2) Comparing JSON pointers and strings

The example demonstrates comparing JSON pointers and strings, and when doing so may raise an exception.

#include<exception>
#include<iostream>
#include<nlohmann/json.hpp>
usingjson=nlohmann::json;
intmain()
{
// different JSON pointers
json::json_pointerptr0;
json::json_pointerptr1("");
json::json_pointerptr2("/foo");
// different strings
std::stringstr0("");
std::stringstr1("/foo");
std::stringstr2("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(constjson::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'

Version history

  1. Added in version 2.1.0. Added C++20 member functions in version 3.11.2.
  2. Added for backward compatibility and deprecated in version 3.11.2.

AltStyle によって変換されたページ (->オリジナル) /