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

json data = std::string_view("hi"); doesn't work? #801

Closed
lethe555 opened this issue Oct 27, 2017 · 7 comments
Closed

json data = std::string_view("hi"); doesn't work? #801

lethe555 opened this issue Oct 27, 2017 · 7 comments
Labels
confirmed solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@lethe555
Copy link

No description provided.

@nlohmann
Copy link
Owner

std::string_view is a C++17 feature. The library supports C++11 and currently does not know what std::string_view is. This should be fixed in the future. In the meantime, you may need to pass string values as std::string to the library - they are stored internally as std::string anyway, so you would need to make a copy anyway.

@theodelrieu
Copy link
Contributor

You could also provide a specialization of adl_serializer<std::string_view>.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 29, 2017
@nlohmann
Copy link
Owner

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;
}

@gregmarr
Copy link
Contributor

Note that the lifetime of data here must be at least as long as the lifetime of sv, or you'll end up with dangling pointers in sv.

@lethe555
Copy link
Author

I only add adl_serializer::to_json , adl_serializer::from_json may lead to a mistake with the description above
thanks!

@nlohmann
Copy link
Owner

FYI: After merging #1028, conversions from/to std::string_view are now supported out of the box.

@JasonDictos
Copy link

For the next guy:

        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());
        }

Change 'auto ref' to 'auto &ref', otherwise the string is copied then destructed when the frame leaves, resulting in an invalid string view returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

5 participants