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

#Most efficient way to search for child parameters (recursive find?) #1141

Closed
JimAtCanon opened this issue Jun 22, 2018 · 5 comments
Closed
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@JimAtCanon
Copy link

JimAtCanon commented Jun 22, 2018

Hello All-

I have an application where I have a hierarchical set of JSON objects. Meaning that I have objects that in turn have child JSON objects and so on. I need to have the ability to allow the user to query the value of one of the objects using something akin to a file path. For example, example if my JSON object looks like:

{
         "One":1,
         "Two":2,
         "Child":{
                 "Child1":1
                 "Child2":2
                 }
         "Three":3
}

I want to provide a method that will return the value of any parameter by passing a 'path' to the parameter. Using the example above, if I wanted the value of the parameter "Child2", I could request it using the path 'Child/Child2', which would return the value 2. I have it working with the code below:

std::filesystem::path path(parameterPath);
for (const auto &parameterName : path) {
   nlohmann::json::iterator iterator = parameterValue.find(parameterName);
   if (iterator != parameterValue.end()) {
      parameterValue = *iterator; //  This makes a copy of the parameter, which is overkill, I just want a reference to it.
      }
     else {
         parameterValue.clear();
         break;
     }
}

However I suspect that this is inefficient because I am creating a copy of the parameters as I traverse the tree. I have also thought about iterators and pointers, but thought I would reach out to the community to to see if anyone has already solved this problem, hopefully in a more efficient manor.

Any suggestions?

Thanks!

Jim

@nlohmann
Copy link
Owner

The library supports JSON Pointer, see https://github.com/nlohmann/json#json-pointer-and-json-patch. You could write j["/Child/Child2"_json_pointer] to access the value you described with Child/Child2.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 22, 2018
@nlohmann
Copy link
Owner

@JimAtCanon Does #1141 (comment) work for your use case?

@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2018

@JimAtCanon Do you need further assistance?

@JimAtCanon
Copy link
Author

Not on this topic, the JSON pointer worked. It would have been nice if there were an example of how to create a pointer with a string variable. All of the examples I found were using string constants postfixed with the '_json_pointer' it took me some digging to find the constructor for the json_pointer class. An example using string variables would have been helpful.

Thanks once again for an excellent library.

Jim

j_original["/baz/1"_json_pointer];

@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2018

Thanks for checking back! And thanks for the hint - I shall add a paragraph to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants