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

Compilation error with std::map<std::string, std::string> on vs 2015 #734

Closed
leozzyzheng opened this issue Sep 9, 2017 · 21 comments
Closed
Labels

Comments

@leozzyzheng
Copy link

using json = nlohmann::json;
json j;
std::map<std::string, std::string> m;
m = j.get<decltype(m)>();

it says error C2665: 'std::pair<const _Kty,_Ty>::pair': none of the 2 overloads could convert all the argument types

if I change to

m = j;

it says 'operator =' is ambiguous

Did I miss anything about converting map data to/from json ?

@nlohmann
Copy link
Owner

I can run your above example without problems with Xcode

#include "json.hpp"

using json = nlohmann::json;

int main(int argc, char const *argv[])
{
    json j = {{"key", "value"}};
    std::map<std::string, std::string> m;
    m = j.get<decltype(m)>();
}

I can confirm the error with m = j;. There was a similar ticket, but I can't find it right now. Maybe @theodelrieu remembers it.

@leozzyzheng
Copy link
Author

oh, sorry forget to tell that I faced the problem on vs2015.

@nlohmann
Copy link
Owner

Just to make sure: Can you compile and run the test suite without problems?

@leozzyzheng
Copy link
Author

no, I'm using the code

#include "json.hpp"

using json = nlohmann::json;

int main(int argc, char const *argv[])
{
    json j;
    std::map<std::string, std::string> m;
    m = j.get<decltype(m)>();
}

I think there is no difference between your code and mine. :)

@nlohmann
Copy link
Owner

Do you run at least Microsoft Visual C++ 2015 / Build Tools 14.0.25123.0?

@leozzyzheng
Copy link
Author

vs2015 with update 3, I need tomorrow to check the version number.

@leozzyzheng
Copy link
Author

I have check that I'm using 14.0.25420.1
image

I think it should be without problems.

@leozzyzheng
Copy link
Author

And I have switched to vs 2017, still has problem.

@leozzyzheng leozzyzheng changed the title Compilation error with std::map<std::string, std::string> Compilation error with std::map<std::string, std::string> on vs 2015 Sep 11, 2017
@leozzyzheng
Copy link
Author

The last call stack in json.hpp is here:
image

obj = CompatibleObjectType(begin(*inner_object), end(*inner_object));
It calls
image
according to the error call stack. I can see the template _Iter is std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>>>>.

And this type is used to construct map at here
image
which needs

'std::pair<const _Kty,_Ty>::pair(std::pair<const _Kty,_Ty> &&)'
with
[
     _Kty=std::string,
    _Ty=std::string
]
or       'std::pair<const _Kty,_Ty>::pair(const std::pair<const _Kty,_Ty> &)'
with
[
    _Kty=std::string,
    _Ty=std::string
]

But _Args is const std::pair<const StringType,nlohmann::basic_json<std::map,std::vector,StringType,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>>

I can see the second type of std::pair is not the std::string, so I think this is the problem. I'm not very familiar with template, so maybe the error is caused by another reason. :)

@leozzyzheng
Copy link
Author

I found if I change std::map<std::string, std::string> to std::map<std::string, int>, then it works without problems.

@nlohmann
Copy link
Owner

Sorry for not checking back earlier. Can I close this issue or is there still anything wrong with the library?

@nlohmann nlohmann added the platform: visual studio related to MSVC label Sep 29, 2017
@leozzyzheng
Copy link
Author

I still cannot convert this type into json using vs2015, so I write the special convert code for this type. It's fine to close it and I wish anyone had same problem could give me some advise.

@jseward
Copy link
Contributor

jseward commented Oct 22, 2017

I just tried this out on both VS 2015 and VS 2017. It compiles with no issues. Should be safe to close. @leozzyzheng perhaps try again with latest code?

@leozzyzheng
Copy link
Author

@jseward I will try it later, thanks.

@leozzyzheng
Copy link
Author

@jseward I have downloaded the latest release version on github and used the exactly same code from nlohmann, unfortunately it still have the same compilation error.

@theodelrieu
Copy link
Contributor

Sorry for the late reply, I'm looking into it.

@theodelrieu
Copy link
Contributor

I have no problem with VS2015 14.0.25431.01 Update 3 and the latest develop version.

@nlohmann
Copy link
Owner

I am confused...

@leozzyzheng
Copy link
Author

@theodelrieu The latest develop version has no problem to me! I think it could be closed. :)

@nlohmann
Copy link
Owner

Thanks for checking everyone!

@coolhandle01
Copy link

coolhandle01 commented Nov 13, 2017

TL:DR; I duplicated this with #827. Now I'm gonna do the reading.
..
OK, done the reading.

I have json.h 2.11 from the releases page, and VS 2015 Update 14.0.25431.01 Update 3, on x64 Windows 10, and I'm still suffering this.. do I need a patched or more recent version?

My issue is exactly the same as the one reported by @leozzyzheng:

#include "json.hpp"

using json = nlohmann::json;
using problem = std::map<std::string, std::string>;

problem demo_string_map_issue()
{
	auto test = R"({ "some_object": {"Name1": "Value1", "Name2": "Value2" } })"_json;
	auto some_object = test.at("some_object");

	auto C2665 = some_object.get<problem>(); // error here!
        return C2665;
}

My project is a Unicode project. Wouldn't have thought that'd make a difference, though (I'm handling std::string for JSON data)?


I have investigated leozzyzhengs solution, I think it looks like this for me. Leaving it here for anyone who needs it.

// ergo

std::map<std::string, std::string> str_map_from_json(const json& j)
{
	std::map<std::string, json> jobject = j;
	std::map<std::string, std::string> object;
	for (const auto it : jobject)
		if (it.second.is_string())
			object[it.first] = it.second.get<std::string>();

	return object;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants