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

Add nlohmann::json::key_type #1951

Closed
esraels opened this issue Feb 21, 2020 · 3 comments
Closed

Add nlohmann::json::key_type #1951

esraels opened this issue Feb 21, 2020 · 3 comments
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@esraels
Copy link

esraels commented Feb 21, 2020

  • Describe the feature in as much detail as possible.
    I'm planning to make a template where it accepts any kind of maps. It need to have key_type and value_type just like the map containers in STL. I want also the nlohmann::json to be supported on that template helper functions. json::value_type is already there but the key_type is not yet. I checked the latest version 3.7.3 and it is still not yet there.

Here is one of my template functions:

template<typename M> inline bool HasKey(M const& p_map, typename M::key_type const& p_key) {
	return p_map.find(p_key) != p_map.end();
}

  • Include sample usage where appropriate.
using json = nlohmann::json;

std::map mData = getSomeMapData();
if(MyLib::HasKey(mData , "keyX")){
      //found
}

json& jA = getSomeJSONData();
if(MyLib::HasKey(jA, "keyY")){
      //found
}
@FrancoisChabot
Copy link
Contributor

I would still very much appreciate this change. See #1442.

@nlohmann
Copy link
Owner

It's worth noting that this could potentially cause existing users of the library to start detecting basic_json<> as a map-type where they didn't use to if they enable_if<> against the presence of key_type and/or mapped_type.

From my perspective, that's part of the point, but there is potential for this change to be subtly disruptive to upstream users.

#1442 (comment)

This makes me hesitant.

@stale
Copy link

stale bot commented Apr 19, 2020

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.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 19, 2020
@stale stale bot closed this as completed Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: enhancement/improvement state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants