mirror of
https://github.com/nlohmann/json.git
synced 2025-07-29 23:01:16 +03:00
Add json_pointer/string_t equality comparison operators (#3664)
This commit is contained in:
committed by
GitHub
parent
e839f58a2a
commit
9e1a7c85e3
19
docs/examples/json_pointer__operator__equal.cpp
Normal file
19
docs/examples/json_pointer__operator__equal.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#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;
|
||||
}
|
4
docs/examples/json_pointer__operator__equal.output
Normal file
4
docs/examples/json_pointer__operator__equal.output
Normal file
@ -0,0 +1,4 @@
|
||||
"" == "": true
|
||||
"" == "": true
|
||||
"" == "/foo": false
|
||||
"/foo" == "/foo": true
|
33
docs/examples/json_pointer__operator__equal_stringtype.cpp
Normal file
33
docs/examples/json_pointer__operator__equal_stringtype.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
"" == "": 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'
|
19
docs/examples/json_pointer__operator__notequal.cpp
Normal file
19
docs/examples/json_pointer__operator__notequal.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#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;
|
||||
}
|
4
docs/examples/json_pointer__operator__notequal.output
Normal file
4
docs/examples/json_pointer__operator__notequal.output
Normal file
@ -0,0 +1,4 @@
|
||||
"" != "": false
|
||||
"" != "": false
|
||||
"" != "/foo": true
|
||||
"/foo" != "/foo": false
|
@ -0,0 +1,32 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
"" != "": false
|
||||
"" != "": false
|
||||
"/foo" != "/foo": false
|
||||
"bar" != "/foo": [json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'bar'
|
Reference in New Issue
Block a user