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

use json construct std::string #1462

Closed
yuzeng2017 opened this issue Jan 30, 2019 · 4 comments
Closed

use json construct std::string #1462

yuzeng2017 opened this issue Jan 30, 2019 · 4 comments
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@yuzeng2017
Copy link

  • What is the issue you have?
    “std::basic_string<char,std::char_traits,std::allocator>::basic_string”: Overload function ambiguous.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    json j;
    j["account"] = "123456";
    std::string account_text(j["account"]);
    vs2015 compile error.

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nlohmann
Copy link
Owner

Related: #1061

@stale
Copy link

stale bot commented Mar 1, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Mar 1, 2019
@jerryjiahaha
Copy link

jerryjiahaha commented Mar 5, 2019

I have similar problem under MSVC 2017 (clang and gcc works). I want to convert json results to std::string vector and here is what I tried:

#include "json.hpp"
#include <vector>
#include <iostream>
#include <fstream>

int main()
{
	nlohmann::json jConfig2 = R"(
		{
			"names": ["Tim", "Tom"],
			"nums": [1.2, 2.3]
		}
	)"_json;
	std::vector<float> iNums0;
	std::vector<std::string> sNames0;

	// Just work for float, but std::string failed
	iNums0 = std::vector<float>(jConfig2.at("nums").begin(), jConfig2.at("nums").end());
	for (auto i : iNums0) { std::cout << i << " "; } std::cout << std::endl;

	// Work around for MSVC 2017
	auto itseed = jConfig2.at("names").get<std::vector<std::string>>();
	sNames0 = std::vector<std::string>(itseed.begin(), itseed.end());

	// Compile ok, but run failed: "vector iterators in range are from different containers"
//	sNames0 = std::vector<std::string>(jConfig2.at("names").get<std::vector<std::string>>().begin(), jConfig2.at("names").get<std::vector<std::string>>().end());

	// Comiple failed: Error C2668 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string': ambiguous call to overloaded
//	sNames0 = std::vector<std::string>(jConfig2.at("names").begin(), jConfig2.at("names").end());

	// Compile failed: Error C2593 'operator =' is ambiguous
//	sNames0 = jConfig2.at("names");

	// Compile failed: Error C2440 '<function-style-cast>': cannot convert from 'nlohmann::basic_json<std::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer>' to 'std::vector<std::string,std::allocator<_Ty>>'
//	sNames0 = std::vector<std::string>(jConfig2.at("names"));

	//  It works, too
	std::vector<std::string> _names = jConfig2.at("names");
//	sNames0 = _names;

	for (auto i : sNames0) {
		std::cout << i << " ";
	}
	std::cout << std::endl;

	return 0;
}

The format

std::vector<Type>(jsontype.at("Key").begin(), jsontype.at("Key").end());

works for Type int, float but not works under std::string.

I also tried clang++ and g++ with -Wall. g++ compiled without warnning, clang++ gave me warning:

In file included from ConsoleApplication1.cpp:1:
./json.hpp:1799:1: warning: 'tuple_size' defined as a class template here but previously declared as a struct template
[-Wmismatched-tags]
class tuple_size<::nlohmann::detail::iteration_proxy_value>
^
/usr/sbin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../include/c++/8.2.1/array:350:5: note: did you mean class
here?
struct tuple_size;
^
/usr/sbin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../include/c++/8.2.1/utility:84:5: note: did you mean class
here?
struct tuple_size;
^
In file included from ConsoleApplication1.cpp:1:
./json.hpp:1803:1: warning: 'tuple_element' defined as a class template here but previously declared as a struct template
[-Wmismatched-tags]
class tuple_element<N, ::nlohmann::detail::iteration_proxy_value>
^
/usr/sbin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../include/c++/8.2.1/array:359:5: note: did you mean class
here?
struct tuple_element;
^
/usr/sbin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.1/../../../../include/c++/8.2.1/utility:110:5: note: did you mean class
here?
struct tuple_element;
^
2 warnings generated.

@yuzeng2017 For your problem 1462#issue-404674231. You could try:

	nlohmann::json j;
	j["account"] = "123456";
	std::string account_text = j["account"]; // OK
//	std::string account_text2(j["account"]); // FAIL
	std::string account_text3(j["account"].get<std::string>()); // OK
	std::cout << account_text << account_text3 << std::endl;

Just use .get<std::string>() for MSVC or use = (I forgot which the constructor type is it)

@stale stale bot removed the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Mar 5, 2019
@stale
Copy link

stale bot commented Apr 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Apr 4, 2019
@stale stale bot closed this as completed Apr 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants