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

Problem parsing array to global vector #896

Closed
davidtabernerom opened this issue Dec 28, 2017 · 3 comments
Closed

Problem parsing array to global vector #896

davidtabernerom opened this issue Dec 28, 2017 · 3 comments
Labels
confirmed solution: duplicate the issue is a duplicate; refer to the linked issue instead

Comments

@davidtabernerom
Copy link

davidtabernerom commented Dec 28, 2017

I am trying to parse a JSON Array of values to a global declared vector.
For some reason, it does not properly parsed it to an actually declared vector, but it does to a newly inline created vector.

This does NOT work...

#include "./json.hpp"
std::vector<std::string> UserList;
int main(int argc, char *argv[])
{
	//
	std::ifstream ConfigurationFile("./userlist.json");
	nlohmann::json ConfigurationJSON;
	ConfigurationFile >> ConfigurationJSON;
	UserList = ConfigurationJSON["Program"]["UserList"];
	//
}

The console returns this

./source/main.cpp: In function ‘int main(int, char**)’:
../source/main.cpp:116:19: error: ambiguous overload for ‘operator=’ (operand types are ‘std::vector<std::__cxx11::basic_string<char> >’ and ‘nlohmann::basic_json<>::value_type {aka nlohmann::basic_json<>}’)
  UserList = ConfigurationJSON["Program"]["UserList"];

While both this...

#include "./json.hpp"
int main(int argc, char *argv[])
{
	//
	std::ifstream ConfigurationFile("./userlist.json");
	nlohmann::json ConfigurationJSON;
	ConfigurationFile >> ConfigurationJSON;
	std::vector<std::string> UserList = ConfigurationJSON["Program"]["UserList"];
	//
}

and this...

#include "./json.hpp"
std::vector<std::string> UserList;
int main(int argc, char *argv[])
{
	//
	std::ifstream ConfigurationFile("./userlist.json");
	nlohmann::json ConfigurationJSON;
	ConfigurationFile >> ConfigurationJSON;
	std::vector<std::string> TemporaryVector = ConfigurationJSON["Program"]["UserList"];
	UserList = TemporaryVector;
	//
}

do work.

@nlohmann
Copy link
Owner

Unfortunately, the issue can be reproduced much simpler:

#include "json.hpp"

using json = nlohmann::json;

int main()
{
    json j = {1,2,3,4};
    
    // works
    std::vector<int> v1 = j;
    
    // does not work
    std::vector<int> v2;
    v2 = j;
}

The error message is:

main.cpp:15:8: Use of overloaded operator '=' is ambiguous
(with operand types 'std::vector<int>' and 'json' (aka 'basic_json<>'))

I am sure we had this issue before, but could not find a solution.

However, you can try this:

v2 = j.get<std::vector<int>>();

@nlohmann
Copy link
Owner

Duplicate of #667.

@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label Dec 28, 2017
@nlohmann
Copy link
Owner

See this note in the README:

In case your type contains several operator= definitions, code like your_variable = your_json; may not compile. You need to write your_variable = your_json.get<decltype your_variable>(); instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: duplicate the issue is a duplicate; refer to the linked issue instead
Projects
None yet
Development

No branches or pull requests

2 participants