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

string_view support for object access #2685

Closed
wants to merge 55 commits into from
Closed

string_view support for object access #2685

wants to merge 55 commits into from

Conversation

nlohmann
Copy link
Owner

@nlohmann nlohmann commented Mar 24, 2021

This PR adds support for std::string_view for object access (operator[], at, value, erase).

Todo:

  • Add tests.
  • Update documentation.
  • Add examples.
  • Check if other functions (e.g., contains) also need adjustment.

Closes #1529.

@coveralls
Copy link

coveralls commented Mar 24, 2021

Coverage Status

Coverage remained the same at 100.0% when pulling b18d97d on string_view into 926df56 on develop.

include/nlohmann/json.hpp Outdated Show resolved Hide resolved
include/nlohmann/json.hpp Outdated Show resolved Hide resolved
include/nlohmann/json.hpp Outdated Show resolved Hide resolved
include/nlohmann/json.hpp Outdated Show resolved Hide resolved
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label Nov 20, 2021
@nlohmann
Copy link
Owner Author

@gregmarr @davidstone Going from at(Key&&) to at(const Key&) breaks the windows build. Any ideas?

string_t value(const typename object_t::key_type& key, const char* default_value) const
#if defined(JSON_HAS_CPP_17) // avoid creating a string_t value from default_value
template < class KeyType, typename detail::enable_if_t <
detail::is_usable_as_key_type<basic_json_t, KeyType>::value, int> = 0>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, since this template clause is the same for #if and #else you could move it outside and avoid duplicating it. If you prefer it this way, that's fine too.

@gregmarr
Copy link
Contributor

gregmarr commented Dec 1, 2021

Ugh, just saw the failure on Windows. Will take a look.

@gregmarr
Copy link
Contributor

gregmarr commented Dec 1, 2021

Looks like it's failing with the string literal lookups. I think these two functions were here to handle the string literals. https://github.com/nlohmann/json/pull/2685/files#diff-b56a00981d8f3b87e3ce49a7eb27d36f4586d9c54c3fb628a88cfc000aa5fed4L3813-L3903

@gregmarr
Copy link
Contributor

gregmarr commented Dec 1, 2021

Here is the issue where they were added to fix this problem: #171

@gregmarr
Copy link
Contributor

@nlohmann Have you tried putting back in those two functions?

@nlohmann
Copy link
Owner Author

@nlohmann Have you tried putting back in those two functions?

I'll try, see 253f39c.

@nlohmann
Copy link
Owner Author

nlohmann commented Dec 23, 2021

The CI still fails (https://github.com/nlohmann/json/runs/4618188061?check_suite_focus=true). Any ideas?

@nlohmann nlohmann added state: help needed the issue needs help to proceed and removed release item: ⚡ improvement review needed It would be great if someone could review the proposed changes. labels Dec 30, 2021
@@ -3816,6 +3816,19 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
}

// see https://github.com/nlohmann/json/pull/2685#issuecomment-994015092
template<typename T, std::size_t n>
reference operator[](T * (&key)[n])
Copy link

@jonesmz jonesmz Dec 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a compile-time string literal is, i believe, never a non-const reference to char, they're always const.

and this appears to be a pointer to non-const reference to array of T ?

If it helps you any, you can make an "Alias" typedef, like this:

template<typename T>
using Alias = T;

Then define the signature of your functions like

template<typename T, std::size_t n>
reference operator[](Alias<T[n]> const&)
{
    return ....
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid I do not understand what you mean. In particular, what should go to the .... part.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would go into the "...." part is whatever the operator[] function should do when given a const-ref to char array.

I just don't see how the function as currently written could possible do anything. It's parameter is a reference to array of pointers, not a reference to array of values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#171 (comment)

Looks like I might have misdirected you a bit. The functions that were added in this commit were the single argument versions:

template<typename T>
reference operator[](T* key)

template<typename T>
const_reference operator[](T* key) const

and it was to support string literals converted to plain pointers, not the actual literals themselves:

const char* _VAR1 = "MyKey";
j[_VAR1] = 10;

char* _VAR1 = "MyKey";
j[_VAR1] = 10;

…g_view

� Conflicts:
�	doc/mkdocs/docs/api/basic_json/contains.md
�	doc/mkdocs/docs/api/basic_json/find.md
�	include/nlohmann/json.hpp
�	single_include/nlohmann/json.hpp
�	test/src/unit-regression2.cpp
@nlohmann
Copy link
Owner Author

(I had to resolve the conflicts)

@nlohmann nlohmann removed this from the Release 3.10.5 milestone Jan 2, 2022
@nlohmann
Copy link
Owner Author

nlohmann commented Jan 2, 2022

See #1529 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: help needed the issue needs help to proceed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How can I use std::string_view as the json_key to "operator []" ?
8 participants