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

Setting values of .JSON file #1444

Closed
takeyourwingsback opened this issue Jan 17, 2019 · 8 comments
Closed

Setting values of .JSON file #1444

takeyourwingsback opened this issue Jan 17, 2019 · 8 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@takeyourwingsback
Copy link

takeyourwingsback commented Jan 17, 2019

This is example of my .JSON file.
{"Settings":{"AutoReload":true,"CheckingUpdates":false}}
What function should I use to set the value of "Settings-AutoReload" from "True" to "False", for example? To set and save it.

@nlohmann
Copy link
Owner

Assume your JSON file is called file.json, then you can read it with

std::ifstream input_file("file.json");
json j;
input_file >> j;

Then you can access the value with operator[]:

j["Settings"]["AutoReload"] = false;

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jan 19, 2019
@takeyourwingsback
Copy link
Author

takeyourwingsback commented Jan 19, 2019

Assume your JSON file is called file.json, then you can read it with

std::ifstream input_file("file.json");
json j;
input_file >> j;

Then you can access the value with operator[]:

j["Settings"]["AutoReload"] = false;

Okay. But question is about can I rewrite and save JSON file with the new value? Or I can work only with gotten value, can't set it?
I need to set only value of ["Settings"]["AutoReload"] and with new value rewrite JSON file. Is that possible like in INI ?

@nlohmann
Copy link
Owner

Do you mean how to write a JSON value (like j in the example) to a file? Have a look at https://github.com/nlohmann/json#serialization--deserialization

@takeyourwingsback
Copy link
Author

Do you mean how to write a JSON value (like j in the example) to a file? Have a look at https://github.com/nlohmann/json#serialization--deserialization

Yes, but only this value. Other values should not be changed if user didn't set them. Only needed value should be rewrited in JSON file.

@nlohmann
Copy link
Owner

I do not understand. How should the expected JSON file look like?

@takeyourwingsback
Copy link
Author

takeyourwingsback commented Jan 19, 2019

I do not understand. How should the expected JSON file look like?

I'm sorry. For example, this's current JSON file
{"Settings":{"AutoReload":true,"CheckingUpdates":false}}

The user wanna set the value of "Settings - AutoReload", set it from "True" to "False" and save it by rewriting JSON file with the new value. The new JSON file will look like
{"Settings":{"AutoReload":false,"CheckingUpdates":false}}

As you can see, other value of "Settings - CheckingUpdates" was not touched, because user didn't set it. In short - can I work and rewrite only a specific value? Can I rewrite the file, changed "Settings - AutoReload" value to "True" and didn't touched "Settings - CheckingUpdates" value ? Or should I use INI or something another for this purpose?

@nlohmann
Copy link
Owner

So you want to write the complete changed value j.

// read
std::ifstream input_file("file.json");
json j;
input_file >> j;

// change
j["Settings"]["AutoReload"] = false;

// write
std::ofstream output_file("new_file.json");
j >> output_file;

The content of that file should be

{"Settings":{"AutoReload":false,"CheckingUpdates":false}}

@takeyourwingsback
Copy link
Author

So you want to write the complete changed value j.

// read
std::ifstream input_file("file.json");
json j;
input_file >> j;

// change
j["Settings"]["AutoReload"] = false;

// write
std::ofstream output_file("new_file.json");
j >> output_file;

The content of that file should be

{"Settings":{"AutoReload":false,"CheckingUpdates":false}}

Thank you very much.

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

2 participants