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

Q: When I received an illegal string,How the program knows? #2162

Closed
huluBrother opened this issue Jun 4, 2020 · 11 comments
Closed

Q: When I received an illegal string,How the program knows? #2162

huluBrother opened this issue Jun 4, 2020 · 11 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@huluBrother
Copy link

std::string body = "";//illegal
std::string body = "a"//illegal
nlohmann::json reqContext = nlohmann::json::parse(body);

how the program knows?

Of course, the premise is that the program cannot crash.
Thanks

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

You can use accept() instead of parse which returns a bool. There is also a parameter allow_exceptions for parse to suppress exceptions, see https://nlohmann.github.io/json/doxygen/classnlohmann_1_1basic__json_a4c734aac1ea36f4b62f25308b6c6784a.html#a4c734aac1ea36f4b62f25308b6c6784a

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

You can use accept() instead of parse which returns a bool. There is also a parameter allow_exceptions for parse to suppress exceptions, see https://nlohmann.github.io/json/doxygen/classnlohmann_1_1basic__json_a4c734aac1ea36f4b62f25308b6c6784a.html#a4c734aac1ea36f4b62f25308b6c6784a
Sorry, I understand what you mean, but I did not understand how to pass this parameter, can you give an example?

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

Instead of calling

auto result = json::parse(body);

do this:

auto result = json::parse(body, nullptr, false);

@huluBrother
Copy link
Author

Instead of calling

auto result = json::parse(body);

do this:

auto result = json::parse(body, nullptr, false);

The content returned by this result is a discard?
How can I tell if parse is successful?

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

From the documentation:

Returns: deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.

@huluBrother
Copy link
Author

From the documentation:

Returns: deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.

value_t::discarded. In which file, how do I refer to this variable

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

You can check with result.is_discarded() whether the value is discarded. Or result == json::value_t::discarded.

@huluBrother
Copy link
Author

You can check with result.is_discarded() whether the value is discarded. Or result == json::value_t::discarded.

	std::string body = "a";
	auto result = nlohmann::json::parse(body, nullptr, false);

	if (result == nlohmann::json::value_t::discarded) {
		std::cout << "nlohmann::json::value_t::discarded" << std::endl;
	}
	if (result.is_discarded()) {
		std::cout << "is_discard" << std::endl;
	}

Thank you very much, I solved the problem.
but result == nlohmann::json::value_t::discarded return false,

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

Sorry, I did not check my code example.

This works:

    if (result.type() == nlohmann::json::value_t::discarded) {
        std::cout << "nlohmann::json::value_t::discarded" << std::endl;
    }

@nlohmann
Copy link
Owner

nlohmann commented Jun 4, 2020

Do you need further assistance with this issue?

@huluBrother
Copy link
Author

Do you need further assistance with this issue?

Thanks, this has satisfied my needs, let me 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

2 participants