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

nlohmann::json and =nullptr #2156

Closed
apivovarov opened this issue Jun 1, 2020 · 5 comments
Closed

nlohmann::json and =nullptr #2156

apivovarov opened this issue Jun 1, 2020 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@apivovarov
Copy link

apivovarov commented Jun 1, 2020

Can I use operators = nullptr and == nullptr with nlohmann::json?

For example:

nlohmann::json myjson = nullptr;

std::ifstream jsonFile (path);
try {
  jsonFile >> myjson;
} catch (nlohmann::json::exception&) {
  myjson = nullptr;
  cout << "Load json file failed\n";
}

if (myjson == nullptr) {
  cout << "Json is not loaded\n";
}

What exactly json myjson = nullptr is doing?

@apivovarov apivovarov changed the title nlohmann::json and nullptr nlohmann::json and =nullptr Jun 1, 2020
@dota17
Copy link
Contributor

dota17 commented Jun 1, 2020

yes, you can. There are several implements about overload operator= and operator== inside this lib,
if the assigned value or the compared value are JSON type value(nlohmann::json), then it's ok to use it.
json myjson = nullptr is to set myjson to null, no longer hold the original value.

@apivovarov
Copy link
Author

Can I use myjson == nullptr to check that nlohmann::json object was created but never filled with data?

if (myjson == nullptr) {
  cout << "Json is not loaded\n";
}

@nlohmann
Copy link
Owner

nlohmann commented Jun 2, 2020

Yes, you can. It would be even more expressive if you would use if (myjson.is_null()).

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 2, 2020
@apivovarov
Copy link
Author

Thank you!

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

3 participants