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

Access keys when iteration with new for loop C++11 #256

Closed
dermariusz opened this issue Jun 1, 2016 · 8 comments
Closed

Access keys when iteration with new for loop C++11 #256

dermariusz opened this issue Jun 1, 2016 · 8 comments

Comments

@dermariusz
Copy link

dermariusz commented Jun 1, 2016

Here is an example where you can access the values of the json object. However, I have no idea to access the keys.

using json = nlohmann::json;
int main() {
    json j = {
        {"name", "John Doe"},
        {"born", "xxxx"}
    };
    for (auto& element: j) {
        std::cout << element << std::endl;
    }
}

Here is working example with stl where it is possible to access the keys:

#include <iostream>
#include <map>

using std::string;

int main() {
    std::map<string, string> m = {
        {"name", "Mariusz Wojcik"},
        {"born", "1997"}
    };
    for (std::pair<string, string> p: m) {
        std::cout << std::get<0>(p) << ": " << std::get<1>(p) << std::endl;
    }
}

So there is my question: Is it possible to access these keys with json in a similiar way?

@nlohmann
Copy link
Owner

nlohmann commented Jun 1, 2016

Hi @MDickie,

if you iterate explicitly via for (auto it = j.begin(); it != j.end(); ++j) you can use it.key() and itj.value()to access the object's key and value, respectively. For range for, this is not possible, because thevalue_typeof ajsonobject isjsonitself and not something likestd::pair. To overcome this, there exists an [iterator_wrapper`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_ab936779c70bec68343ef440ed13251e5.html#ab936779c70bec68343ef440ed13251e5) that provides an adapter that you can use like this:

for (auto& element : json::iterator_wrapper(j)) {
    std::cout << element.key() << " maps to " << element.value() << std::endl;
}

Note that the name of this function may change in the future - when I introduced it, it seemed as if people agreed that they did not like the name, but did not propose something better 😄. So if you have an idea, I'm all ears!

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jun 5, 2016
@qis
Copy link

qis commented Jun 11, 2016

I would propose the name "object_iterator" which more closely resembles std::filesystem::directory_iterator.

Edit: I see that there is such an iterator. Will take a closer look at it.

@nlohmann
Copy link
Owner

@qis Thanks for the hint! I'll also see whether I can find some inspiration there.

@MDickie Did #256 (comment) solve your problem?

@dermariusz
Copy link
Author

@nlohmann Yeah, thanks

@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Jun 14, 2016
@nlohmann
Copy link
Owner

I close this issue. I'm still happy for any comments regarding the name of the iterator_wrapper.

@cezheng
Copy link

cezheng commented Jun 17, 2016

just to share how Swift name it when using for ... in to iterate the objects in an array with relevant indexes

let a = ["a", "b","c"]
for (index, item) in a.enumerated() {
  // ...
}

it may look nicer if we define json::iterator_wrapper as a member function of json, like this

json j = {{"a", 1}, {"b", 2}, {"c", 3}};
for (auto& entry : j.object_iterator()) {
  // entry.key();
  // entry.value();
  // ...
}

@qis
Copy link

qis commented Jun 17, 2016

I would expect json::object_iterator to be a type and not a function.

@nlohmann
Copy link
Owner

Yes, the () looks weird in this setting.

I like Python's items() member function:

Returns the element attributes as a sequence of (name, value) pairs. The attributes are returned in an arbitrary order.

(in fact documentation from xml.etree.ElementTree.items())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants