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

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

Closed
nlohmann opened this issue Dec 13, 2017 · 10 comments
Assignees
Milestone

Comments

@nlohmann
Copy link
Owner

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?

@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Dec 13, 2017
@gregmarr
Copy link
Contributor

I think adding the .items() would be useful to make it less verbose. Having .first and .second would mean that you'd have to add a std::string and a std::reference_wrapper to the type. This would increase the size, and potentially change semantics of .value(), because the type would be something like std::reference_wrapper<std::remove_reference<IteratorType::reference>::type> instead of just IteratorType::reference. The semantics are already a bit different than iterating over a map as it allows you to iterate over an array and get the indices as strings, so I don't think having .key() and .value() makes it that much more difficult to use.

@nlohmann
Copy link
Owner Author

nlohmann commented Jan 4, 2018

Any further comments?

@gregmarr
Copy link
Contributor

gregmarr commented Jan 4, 2018

Oh, thanks for referencing this, it points out that I missed something in my evaluation on the deduction guides issue.

@gregmarr
Copy link
Contributor

gregmarr commented Jan 5, 2018

Just one more: we don't necessarily need to remove .value() or make it return .second, so we can do this without changing the semantics of .value().

I'd say add .items() now. It's independent from .first/.second and we can continue that bit via #901.

nlohmann added a commit that referenced this issue Jan 5, 2018
@nlohmann
Copy link
Owner Author

nlohmann commented Jan 9, 2018

Instead of adding .items(), I would like to replace iterator_wrapper with it. I think iterator_wrapper should be deprecated with the next release.

@nlohmann nlohmann added this to the Release 3.1.0 milestone Jan 9, 2018
@gregmarr
Copy link
Contributor

gregmarr commented Jan 9, 2018

To follow semver, we need to deprecate it now and remove it in 4.0.

@nlohmann
Copy link
Owner Author

nlohmann commented Jan 9, 2018

Right, this is what I meant.

nlohmann added a commit that referenced this issue Jan 23, 2018
Also fixed some warnings from GCC.
@nlohmann nlohmann removed the state: please discuss please discuss the issue or vote for your favorite option label Jan 23, 2018
@nlohmann nlohmann self-assigned this Jan 23, 2018
@nlohmann
Copy link
Owner Author

iterator_wrapper is now deprecated and will be removed in 4.0.0.

@Anthony-J-Garot
Copy link

nlohmann added a commit that referenced this issue Dec 28, 2021
@nlohmann
Copy link
Owner Author

@Anthony-J-Garot I did so in be1eafa which should be merged to develop soon(ish).

nlohmann added a commit that referenced this issue Dec 29, 2021
* 🔥 consolidate documentation
* ♻️ overwork std specializations
* 🚚 move images files to mkdocs
* ♻️ fix URLs
* 🔧 tweak MkDocs configuration
* 🔧 add namespaces
* 📝 document deprecations
* 📝 document documentation generation
* 🚸 improve search
* 🚸 add examples
* 🚧 start adding documentation for macros
* 📝 add note for #874 (comment)
* 📝 overwork example handling
* 📝 fix Markdown tables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants