Skip to content

Discuss: replace static "iterator_wrapper" function with "items" member function #874

Description

@nlohmann

The static iterator_wrapper function is a means to access objects' keys in a range for. Example:

json j_object = {{"one", 1}, {"two", 2}};

for (auto& x : json::iterator_wrapper(j_object)) {
    std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
}

I never was happy about the name of the function and there were several issues where users expected json to behave as a map so that .first and .second could be used.

Question 1

As for the name: What about replacing the static iterator_wrapper function by a member function items (name motivated by Python)?

Usage:

json j_object = {{"one", 1}, {"two", 2}};

for (auto& x : j_object.items()) {
    std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
}

Question 2

One could even think about supporting .first and .second, but this would mean pre-computing the values for .key() and .value() which would mean a bit of overhead.

What do you think?

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions