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

alias object_t::key_type in basic_json #1442

Closed
FrancoisChabot opened this issue Jan 16, 2019 · 3 comments
Closed

alias object_t::key_type in basic_json #1442

FrancoisChabot opened this issue Jan 16, 2019 · 3 comments
Labels
state: please discuss please discuss the issue or vote for your favorite option state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented Jan 16, 2019

  • Describe the feature in as much detail as possible.

I propose simply adding using key_type = typename object_t::key_type; to basic_json<>

Considering at() and operator[] already make use of it in the public interface, it doesn't seem unreasonable.

N.B. mapped_type might as well be aliased too for completeness.

Caveat: I understand that find() does not return an iterator to a std::pair<key_type, mapped_type> so basic_json<> can never be a true map-like as it stands, but unlike the key_type issue I illustrate lower down, this is something that can be SFINAE'd around.

  • Include sample usage where appropriate.

This would be useful when duck-typing basic_json<> as a map-like type. Specifically, the fact that std::string_view is not implicitly convertible to std::string is currently making this difficult.

Example:

template<typename MapLike_t>
void do_something(std::string_view key, MapLike_t& obj) {
  using key_type = typename MapLike_t::key_type;

  foo(obj[key_type(key)]);
}
@FrancoisChabot FrancoisChabot changed the title alias object_type::key_type in basic_json alias object_t::key_type in basic_json Jan 16, 2019
@nlohmann nlohmann added the state: please discuss please discuss the issue or vote for your favorite option label Jan 16, 2019
@nlohmann
Copy link
Owner

Any opinion on this?

@FrancoisChabot
Copy link
Contributor Author

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.

@stale
Copy link

stale bot commented Feb 15, 2019

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 Feb 15, 2019
@stale stale bot closed this as completed Feb 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: please discuss please discuss the issue or vote for your favorite option 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

Successfully merging a pull request may close this issue.

2 participants