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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
e3c2d55
:alembic: at() for std::string_view #1529
nlohmann Mar 24, 2021
edd2462
:bug: add missing negation
nlohmann Mar 24, 2021
d770517
:bug: add missing negation
nlohmann Mar 24, 2021
7742859
:alembic: add more std::string_view support
nlohmann Mar 24, 2021
6019b5d
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 25, 2021
5ea15c4
:recycle: unify types for object keys
nlohmann Mar 25, 2021
ba97e9f
:memo: update documentation
nlohmann Mar 25, 2021
360f21e
:white_check_mark: add tests for count() and contains()
nlohmann Mar 25, 2021
2f2c759
:green_heart: fix build
nlohmann Mar 25, 2021
16fb0cb
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 26, 2021
d01a6a4
:construction: add overload for erase(std::string_view)
nlohmann Mar 26, 2021
4a78689
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 28, 2021
d42f37a
:construction: implement operator[] for string_view
nlohmann Mar 28, 2021
47c7e2c
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 29, 2021
268600e
:ok_hand: apply review comments
nlohmann Mar 29, 2021
203b53d
:bulb: update documentation
nlohmann Mar 29, 2021
5c1c79b
:green_heart: fix build
nlohmann Mar 29, 2021
0ff33d0
:ok_hand: apply review comments
nlohmann Mar 29, 2021
f359d74
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Mar 30, 2021
69d74d4
:recycle: clean up
nlohmann Mar 30, 2021
418fdba
:alembic: exclude iterators from KeyType
nlohmann Mar 30, 2021
02b36e9
:alembic: add forgotten forward
nlohmann Mar 30, 2021
ea4891f
:ok_hand: implement some review comments
nlohmann Mar 31, 2021
4a16e55
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann May 3, 2021
0818554
:twisted_rightwards_arrows: merge develop
nlohmann May 3, 2021
12c0bc4
:memo: document less-than comparability
nlohmann May 3, 2021
eb0f8d7
:ok_hand: apply review comments
nlohmann May 3, 2021
aaef7cd
:ok_hand: apply review comments
nlohmann May 4, 2021
b27938a
:ok_hand: apply review comments
nlohmann May 4, 2021
80cb607
:construction: fix return type
nlohmann Jul 16, 2021
8ba8c43
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Jul 16, 2021
575c28c
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Jul 22, 2021
1bd8a10
:ok_hand: fix code according to review comments
nlohmann Jul 22, 2021
3212700
:ok_hand: fix code according to review comments
nlohmann Jul 22, 2021
86dd665
:alembic: add fix for Clang 3.5
nlohmann Jul 22, 2021
5f39b09
:rewind: undo experimental fix for Clang 3.5
nlohmann Jul 23, 2021
d15ad85
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Aug 30, 2021
8b9e297
:twisted_rightwards_arrows: merge develop
nlohmann Aug 30, 2021
11eab8a
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Oct 17, 2021
9b838a0
:twisted_rightwards_arrows: update from develop
nlohmann Oct 17, 2021
76109db
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Nov 20, 2021
e86d8b9
:green_heart: add fix from https://github.com/Minipeps/json/commit/dd…
nlohmann Nov 20, 2021
cf06ba8
:green_heart: add fix from https://github.com/Minipeps/json/commit/dd…
nlohmann Nov 20, 2021
25ca594
:memo: overwork documentation
nlohmann Nov 20, 2021
5379b5d
:white_check_mark: improve coverage
nlohmann Nov 20, 2021
15e4598
:ok_hand: address review comments
nlohmann Nov 23, 2021
0eac6b3
:ok_hand: address review comments
nlohmann Nov 23, 2021
47b9b7e
:ok_hand: address review comments
nlohmann Nov 23, 2021
253f39c
:alembic: experiment from https://github.com/nlohmann/json/pull/2685#…
nlohmann Dec 23, 2021
6115454
Merge branch 'develop' of https://github.com/nlohmann/json into strin…
nlohmann Dec 31, 2021
72d8615
:twisted_rightwards_arrows: merge develop branch
nlohmann Dec 31, 2021
d8db435
:alembic: try different type alias
nlohmann Jan 1, 2022
2cdf3dd
:rewind: undo commit 15e45981ead5ca64a963d79c5dd5b6d18b2c7a3f
nlohmann Jan 2, 2022
b01293f
Merge remote-tracking branch 'origin/string_view' into string_view
nlohmann Jan 2, 2022
b18d97d
:rewind: undo commit d8db435c1e1e60ebd93f9e5036437f9b8cc478e4
nlohmann Jan 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

{
return operator[](static_cast<const T>(key));
}

template<typename T, std::size_t n>
const_reference operator[](T * (&key)[n]) const
{
return operator[](static_cast<const T>(key));
}

/*!
@brief access specified object element with default value

Expand Down
13 changes: 13 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21383,6 +21383,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])
{
return operator[](static_cast<const T>(key));
}

template<typename T, std::size_t n>
const_reference operator[](T * (&key)[n]) const
{
return operator[](static_cast<const T>(key));
}

/*!
@brief access specified object element with default value

Expand Down