Skip to content

Add cxx17 string_view operator[] for Json::Value #999

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

Closed
wants to merge 3 commits into from

Conversation

ObjatieGroba
Copy link

Is it possible to add string_view?
How can we turn it off on older c++ versions?
Can it be another define "JSON_USE_CXX17"? Or compiler defines?

Thank you

@@ -12,6 +12,7 @@
#include <ostream>
#include <sstream>
#include <string>
#include <string_view>
Copy link
Member

Choose a reason for hiding this comment

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

It seems the system doesn't support C++17 Standard now, so it cannot open include file string_view
but if you like, you could use the __plusplus marco switch :

#if __cplusplus >= 201703L 
#include <string_view>
#endif 

@@ -141,6 +142,7 @@ using Allocator = typename std::conditional<JSONCPP_USING_SECURE_MEMORY,
SecureAllocator<T>,
std::allocator<T>>::type;
using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
using StringView = std::basic_string_view<char, std::char_traits<char>>;
Copy link
Member

Choose a reason for hiding this comment

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

Only when the __cplusplus >= 201703L, can this semantics work.

@baylesj
Copy link
Contributor

baylesj commented Aug 14, 2019

We would have to include abseil or move to C++17 for this, so I'm voting to close for now.

@baylesj baylesj closed this Aug 14, 2019
@ObjatieGroba
Copy link
Author

Have to include abseil or move to c++17 for this

Why? It is just StringView. There are many cases where we can use it. What difference between const string ref and StringView?

@dota17
Copy link
Member

dota17 commented Aug 14, 2019

Have to include abseil or move to c++17 for this

Why? It is just StringView. There are many cases where we can use it. What difference between const string ref and StringView?

string_view can avoid unnecessary memory allocation. but it only applied in the new c++17 standard and abseil lib.

@ObjatieGroba
Copy link
Author

ObjatieGroba commented Aug 14, 2019

it only applied in the new c++17 standard and abseil lib

What about people who use current master with c++17 standart? For them it is new standart and std::string_view can be applied because it contains pointer and size. Where the problem is?
For usage in operator[] const reference same as StringView. There are no memory allocation on this operator.
Can we add std::string_view to functions, where we do not care about memory ownership?
It is really necessary for people who use c++17 because them have to do smth like that

std::string_view sv;
Json::Value json;
json[std::string(sv)] = .. // Memory allocation. Why?

Thank you

@ObjatieGroba
Copy link
Author

Maybe it is possible to add as default disabled feature?
To enable just define JSON_USE_STRING_VIEW

@dota17
Copy link
Member

dota17 commented Aug 14, 2019

I guess baylesj temporarily closed this PR because we haven't decided to use abseil C++ library or C++17 Standard library.
Currently, we should focus on how to migrate to C++14, so we should not rush to use new C++ standard, I think?
And there is a link about the detail of std::string_view, hope it can help you.

@BillyDonahue
Copy link
Contributor

supporting std::string_view as a value key should be doable but it would have to be carefully controlled by a #if and we'd have to carefully control the overload set to avoid ambiguities with other string-like types we're using.

I don't think abseil has much to do with the decision though, unless I'm missing something.

@aberaud
Copy link

aberaud commented Jun 4, 2021

As more projects move to C++17, I believe this becomes increasingly necessary.

Additionally to the proposals in this PR, it would be great to have

bool Json::Value::getString(std::string_view& out) const;

To be able to read a string Json value without unneeded allocations + copies.

@aberaud
Copy link

aberaud commented Mar 24, 2022

GCC is now moving to C++17 by default, and C++20/22 include additional support for std::string_view.

Other libraries like asio have included support for std::string_view for years now.

Could we please list issues with the current patch so they could be solved, so this could move forward and get merged?

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

Successfully merging this pull request may close these issues.

5 participants