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 to std::map #1487

Closed
janekaldo opened this issue Feb 20, 2019 · 7 comments
Closed

json to std::map #1487

janekaldo opened this issue Feb 20, 2019 · 7 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@janekaldo
Copy link

janekaldo commented Feb 20, 2019

hey

void config::save() {
	nlohmann::json j = g_Settings.config;
       std::ofstream o("default.json");
	o << j << std::endl;
}


void config::load() {
	nlohmann::json j;
	std::ofstream o("default.json");
	j >> o;
	g_Settings.config = j.get<std::map<std::string, bool>>;
}

this is my current code. saving works perfectly but loading doesnt. compiler gives the following erros:

'nlohmann::basic_jsonstd::map,std::vector,std::string,bool,int64_t,uint64_t,double,std::allocator,nlohmann::adl_serializer::operator >>': was declared deprecated

@hydratim
Copy link

The library already supports the STL std::map interface, however you might need to write your own post-processor that iterates over the object and produces a true std::map.

@nickaein
Copy link
Contributor

nickaein commented Feb 21, 2019

While the conversion from STL containers to json is supported, I doubt there is a conversion available from json to std::map<std::string, bool>>. You might try to implement a from_json method for your config data type or just do the conversion directly inside the load() function. See below

@theodelrieu
Copy link
Contributor

I don't understand, the error is not about conversion from JSON to map, it seems to me that you're compiling with -Werror while using a deprecated function.

Replace j >> o by j = json::parse(o) and it should work as expected.

@nickaein
Copy link
Contributor

Théo is right. There is indeed an overload of from_json avaiable for bool as value type which covers the std::map<string,bool>> conversion. Therefore writing a custom from_json method is unnecessary.

@nlohmann
Copy link
Owner

Instead of j >> o;, please use o << j;.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Feb 22, 2019
@garethsb
Copy link
Contributor

garethsb commented Mar 5, 2019

If the OP is trying to load a json file, don't they need

std::ifstream i("default.json");
i >> j;

@nlohmann
Copy link
Owner

@janekaldo Do you need further support or can I close this issue?

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

No branches or pull requests

6 participants