Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow push_back() and pop_back() calls on json_pointer #1434

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/nlohmann/detail/json_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class json_pointer
return res;
}

private:
/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
Expand All @@ -114,6 +113,16 @@ class json_pointer
return last;
}

/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
*/
void push_back(const std::string& tok)
{
reference_tokens.push_back(tok);
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
}

private:
/// return whether pointer points to the root document
bool is_root() const noexcept
{
Expand Down
11 changes: 10 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11885,7 +11885,6 @@ class json_pointer
return res;
}

private:
/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
Expand All @@ -11902,6 +11901,16 @@ class json_pointer
return last;
}

/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
*/
void push_back(const std::string& tok)
{
reference_tokens.push_back(tok);
}

private:
/// return whether pointer points to the root document
bool is_root() const noexcept
{
Expand Down
54 changes: 54 additions & 0 deletions test/src/unit-json_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,58 @@ TEST_CASE("JSON pointers")
CHECK(j.is_object());
}
}

SECTION("push and pop")
{
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.push_back("pi");
CHECK(j[ptr] == j["pi"]);

ptr.pop_back();
CHECK(j[ptr] == j);

// object and children access
ptr.push_back("answer");
ptr.push_back("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.push_back("object");
ptr.push_back("/");
CHECK(j[ptr] == j["object"]["/"]);
CHECK(ptr.to_string() == "/object/~1");
}
}