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

std::map<std::string, std::string> from json object yields C2665: 'std::pair<const _Kty,_Ty>::pair': none of the 2 overloads could convert all the argument types #827

Closed
coolhandle01 opened this issue Nov 13, 2017 · 4 comments

Comments

@coolhandle01
Copy link

coolhandle01 commented Nov 13, 2017

please close me, I am a duplicate of #734.

@coolhandle01 coolhandle01 changed the title std::map<std::string, std::string> from json object (compiler error) yields C2665: 'std::pair<const _Kty,_Ty>::pair': none of the 2 overloads could convert all the argument types std::map<std::string, std::string> from json object yields C2665: 'std::pair<const _Kty,_Ty>::pair': none of the 2 overloads could convert all the argument types Nov 13, 2017
@gregmarr
Copy link
Contributor

The map underlying a json object is of type std::map<std::string, json>. The value of each entry can be any of the valid json types, so this isn't a compile-time conversion. You will probably have to iterate through all of the entries in the object and check their types manually. I'm not aware of any magic in the library that will allow you to pull out just the items with a string value type. (Maybe one of the other users/maintainers knows of some method here that I don't.)

@gregmarr
Copy link
Contributor

If you're going to do this more than once, you might be able to do something with the code from this section of the README:

This serializer works fine when you have control over the type's namespace. However, what about boost::optional, or std::filesystem::path (C++17)? Hijacking the boost namespace is pretty bad, and it's illegal to add something other than template specializations to std...

namespace nlohmann {
    template <typename T>
    struct adl_serializer<std::map<std::string, std::string>> {
        static void from_json(const json& j, std::map<std::string, std::string>& mapss) {
            // If j is an object, iterate over the values of j and store the ones that are strings
        }
    };
}

@coolhandle01
Copy link
Author

Thanks for your timely response @gregmarr, but this appears to be a duplicate of #734 I'm afraid. I think that approach was mentioned on 734 by leozzyzheng. 734 was closed, but I'm not sure what the consensus was.

From 734:

I am confused...

Me too.

@coolhandle01
Copy link
Author

I've figured it out @gregmarr. Thanks again for the timely responses. I've posted a suggested workaround on #734. I'll be quiet now. :)

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

No branches or pull requests

3 participants