-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
JSON-pointer: add operator+() returning a new json_pointer #1454
Conversation
Wouldn't it be preferable to match |
Good point, at first I was convinced you're right, but reading the documentation
The operator I'm adding is not doing that but it only adds 1 new token add the end of the pointer. My C++-powers are not going far enough to see whether this can be easily achieved with such an operator as well (and keeping it simple). But I'm eager to learn. |
As far as a member operator vs a namespace operator, the distinction is fairly light, beyond the academic merits of preferring free-floating functions in general. A concrete advantage of using a namespace-level operator instead of a class member is that it allows to compose in the other direction as well. So you could eventually support: This brings us to a more concrete reason to use On top of that |
In my opinion, it does make sense to provide this feature via class json_pointer
{
// existing class members
explicit json_pointer(const std::string& s = "")
// ...
std::string pop_back()
// ...
void push_back(const std::string& tok)
// ...
// concatenate two `json_pointer`s
json_pointer& operator/=(const json_pointer& ptr); // effectively returns reference_tokens.insert(reference_tokens.end(), ptr.reference_tokens.begin(), ptr.reference_tokens.end()), *this
friend json_pointer operator/(const json_pointer& lhs, const json_pointer& rhs); // effectively returns json_pointer(lhs) /= rhs
// append a single unescaped `reference-token` to a `json_pointer`
json_pointer& operator/=(std::string tok); // effectively returns this->push_back(std::move(tok)), *this
json_pointer& operator/=(std::size_t index); // effectively returns *this /= std::to_string(index)
friend json_pointer operator/(const json_pointer& lhs, std::string tok); // effectively returns json_pointer(lhs) /= std::move(tok)
friend json_pointer operator/(const json_pointer& lhs, std::size_t index); // effectively returns json_pointer(lhs) /= index
}; The operation of removing the last reference-token is less common and |
Thanks for suggesting this. @FrancoisChabot already convinced. A lack of time made me not getting forward. @garethsb-sony Feel free to get along with this PR with your suggestion. |
Adds the operator+() to the json_pointer-class.
Fixes some comment/documentations as well.
Pull request checklist
Read the Contribution Guidelines for detailed information.
include/nlohmann
directory, runmake amalgamate
to create the single-header filesingle_include/nlohmann/json.hpp
. The whole process is described here.Please don't
#ifdef
s or other means.