mirror of
https://github.com/nlohmann/json
synced 2024-11-29 23:21:04 +00:00
JSON-pointer: add operator+() returning a new json_pointer
This commit is contained in:
parent
e89c946451
commit
a06e7f5d80
@ -114,14 +114,33 @@ class json_pointer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief remove and return last reference pointer
|
@brief append a token at the end of the reference pointer
|
||||||
@throw out_of_range.405 if JSON pointer has no parent
|
|
||||||
*/
|
*/
|
||||||
void push_back(const std::string& tok)
|
void push_back(const std::string& tok)
|
||||||
{
|
{
|
||||||
reference_tokens.push_back(tok);
|
reference_tokens.push_back(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief append a key-token at the end of the reference pointer and return a new json-pointer.
|
||||||
|
*/
|
||||||
|
json_pointer operator+(const std::string& tok) const
|
||||||
|
{
|
||||||
|
auto ptr = *this;
|
||||||
|
ptr.push_back(tok);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief append a array-index-token at the end of the reference pointer and return a new json-pointer.
|
||||||
|
*/
|
||||||
|
json_pointer operator+(const size_t& index) const
|
||||||
|
{
|
||||||
|
auto ptr = *this;
|
||||||
|
ptr.push_back(std::to_string(index));
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// return whether pointer points to the root document
|
/// return whether pointer points to the root document
|
||||||
bool is_root() const noexcept
|
bool is_root() const noexcept
|
||||||
|
@ -11924,14 +11924,33 @@ class json_pointer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief remove and return last reference pointer
|
@brief append a token at the end of the reference pointer
|
||||||
@throw out_of_range.405 if JSON pointer has no parent
|
|
||||||
*/
|
*/
|
||||||
void push_back(const std::string& tok)
|
void push_back(const std::string& tok)
|
||||||
{
|
{
|
||||||
reference_tokens.push_back(tok);
|
reference_tokens.push_back(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief append a key-token at the end of the reference pointer and return a new json-pointer.
|
||||||
|
*/
|
||||||
|
json_pointer operator+(const std::string& tok) const
|
||||||
|
{
|
||||||
|
auto ptr = *this;
|
||||||
|
ptr.push_back(tok);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief append a array-index-token at the end of the reference pointer and return a new json-pointer.
|
||||||
|
*/
|
||||||
|
json_pointer operator+(const size_t& index) const
|
||||||
|
{
|
||||||
|
auto ptr = *this;
|
||||||
|
ptr.push_back(std::to_string(index));
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// return whether pointer points to the root document
|
/// return whether pointer points to the root document
|
||||||
bool is_root() const noexcept
|
bool is_root() const noexcept
|
||||||
|
@ -513,4 +513,58 @@ TEST_CASE("JSON pointers")
|
|||||||
CHECK(j[ptr] == j["object"]["/"]);
|
CHECK(j[ptr] == j["object"]["/"]);
|
||||||
CHECK(ptr.to_string() == "/object/~1");
|
CHECK(ptr.to_string() == "/object/~1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("operators")
|
||||||
|
{
|
||||||
|
const json j =
|
||||||
|
{
|
||||||
|
{"", "Hello"},
|
||||||
|
{"pi", 3.141},
|
||||||
|
{"happy", true},
|
||||||
|
{"name", "Niels"},
|
||||||
|
{"nothing", nullptr},
|
||||||
|
{
|
||||||
|
"answer", {
|
||||||
|
{"everything", 42}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{"list", {1, 0, 2}},
|
||||||
|
{
|
||||||
|
"object", {
|
||||||
|
{"currency", "USD"},
|
||||||
|
{"value", 42.99},
|
||||||
|
{"", "empty string"},
|
||||||
|
{"/", "slash"},
|
||||||
|
{"~", "tilde"},
|
||||||
|
{"~1", "tilde1"}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// empty json_pointer returns the root JSON-object
|
||||||
|
auto ptr = ""_json_pointer;
|
||||||
|
CHECK(j[ptr] == j);
|
||||||
|
|
||||||
|
// simple field access
|
||||||
|
ptr = ptr + "pi";
|
||||||
|
CHECK(j[ptr] == j["pi"]);
|
||||||
|
|
||||||
|
ptr.pop_back();
|
||||||
|
CHECK(j[ptr] == j);
|
||||||
|
|
||||||
|
// object and children access
|
||||||
|
ptr = ptr + "answer";
|
||||||
|
ptr = ptr + "everything";
|
||||||
|
CHECK(j[ptr] == j["answer"]["everything"]);
|
||||||
|
|
||||||
|
ptr.pop_back();
|
||||||
|
ptr.pop_back();
|
||||||
|
CHECK(j[ptr] == j);
|
||||||
|
|
||||||
|
// push key which has to be encoded
|
||||||
|
ptr = ptr + "object";
|
||||||
|
ptr = ptr + "/";
|
||||||
|
CHECK(j[ptr] == j["object"]["/"]);
|
||||||
|
CHECK(ptr.to_string() == "/object/~1");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user