-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Delete by json_pointer #1248
Comments
Right now, the JSON Pointer is converted to a string and the |
@nlohmann If you point me roughly towards some piece of code, I am happy to take a look if I could provide a PR |
I made a simple version of this. Just copied existing code and added the erase part. There is also a bug with json pointers when they have only a single forward slash. I will probably eventually create a different json pointer which doesn't vectorize, but instead provide iterator functionality without vectorization. But no time at the moment. |
@timprepscius Could you open a PR? |
@timprepscius And: what is the bug with JSON Pointers with a single slash? |
Will do, will take a few days. Bug JSON Pointer, make tests involving just "/". Crashes: |
I cannot reproduce the bug. This code runs fine: #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main()
{
json::json_pointer ptr("/");
json j;
j[""] = 42;
std::cout << j[ptr] << std::endl;
} Output: |
prootFAIL fails for arrays. Should it? I don't know. Seems like a bug. |
The exception states the problem:
tl;dr: The library is correct to throw an exception in this case. |
Hmmm. My intuition says something is still wrong, but not in the way I thought. Maybe wrong is not the right word. If I have:
It seems to me that this should create an exception- not create the key "wrong" with a null value. |
Any how, has no practical impact on my side, I will make a work around to avoid this scenario, change my understanding of "root" to only be "" not "/" |
This is the default behavior of std::map<std::string, std::string> j;
j["hi"] = "there";
std::cout << j.size() << std::endl;
std::cout << j["wrong"] << std::endl;
std::cout << j.size() << std::endl; The semantics of As for the JSON Pointer |
Yeah. Not my favorite part of the operator[] of map either. |
If |
It's true. But I find that, because of the "construct if not present" feature of std::map, I almost always use find(), !=end(), *i... and/or insert.. It just seems to work out that I almost never want std::map to default create a value. If it is there, and I'm creating it again, it probably means something wrong in code. If it is not there, then I'd want to use insert. Personal coding style I suppose. |
And actually, let's say we were to make std::map not function this way. It would definitely be a bit gross, but would it be super gross? std::map::operator[] could return a "lazy reference", by which I mean a class which contains the key reference, and the map reference. when the lazy reference is dereferenced, via an casting operator T(), if it does not exist it would through an exception. If it is assigned to, via operator =(T &&rhs), it would emplace the T into the map, without doing any default construction. would this work? on a scale of grossness, I would rate this, maybe a 2/10.. you could have j["hi"] = "there". anyhow, irrelevant to json I suppose |
sorry, not emplace.. etc, etc.. point made anyhow. beep borp. |
One clarification needed, on the edge case of erase of root pointer. What is correct behavior? json j; what should happen? |
I think the best behavior would be to set |
Ok, will modify |
Actually, one more question.
So, what does this do? Also, what does this do: As I'm thinking about std::map, I believe So I'm guessing that, no exceptions should be thrown, but, in the second example, "not-a-valid-key1" should not be created. but what about |
I would see a JSON Pointer as an iterator rather than an object key. Therefore, I would propose to throw in case the argument to |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Describe the feature in as much detail as possible.
Use json_pointer for erasing keys from json
Include sample usage where appropriate.
Actual output:
Expected output:
The text was updated successfully, but these errors were encountered: