-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
json data = std::string_view("hi"); doesn't work? #801
Comments
|
You could also provide a specialization of |
For reference, here is a possible implementation: #include <iostream>
#include <string_view>
#include "json.hpp"
using json = nlohmann::json;
namespace nlohmann {
template<>
struct adl_serializer<std::string_view> {
static void to_json(json& j, const std::string_view& value) {
j = std::string(value.data(), value.size());
}
static void from_json(const json& j, std::string_view& value) {
auto ref = j.get_ref<const std::string&>();
value = std::string_view(ref.data(), ref.size());
}
};
}
int main(int argc, char* argv[]) {
json data = std::string_view("hi");
std::cout << data << std::endl;
data = "hello";
std::string_view sv = data;
std::cout << sv << std::endl;
} |
Note that the lifetime of |
I only add |
FYI: After merging #1028, conversions from/to |
For the next guy:
Change 'auto ref' to 'auto &ref', otherwise the string is copied then destructed when the frame leaves, resulting in an invalid string view returned. |
No description provided.
The text was updated successfully, but these errors were encountered: