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

Add JSON check #4

Open
nlohmann opened this issue Jul 25, 2020 · 1 comment
Open

Add JSON check #4

nlohmann opened this issue Jul 25, 2020 · 1 comment

Comments

@nlohmann
Copy link

The nlohmann/json has a function accept which just checks whether an input is valid JSON. As no internal structure is built, this check is much faster than parsing. Maybe other libraries have this as well, and benchmarking it could be helpful.

@Loki-Astari
Copy link
Owner

The test "Parse" is used to perform this exact test.

void PerformanceChecker::executeParse(TestBase const& parser, Test const& test)
{
double minDuration = std::chrono::duration<double>::max().count();
Output generator(options, parser, test, "Parse", "1", minDuration);
for (int loop = 0; loop < loopCount; ++loop)
{
TestSetUp testSetUp(parser, setupName(test), true);
std::unique_ptr<ParseResultBase> result;
double duration = timeExecution([&parser, &test, &result]()
{ result.reset(parser.Parse(test.input.c_str(), test.input.size()));});
if (!result)
{
return;
}
minDuration = std::min(minDuration, duration);
}
generator.setPass();
}

You will notice that nothing is done with the result of the parse.
Though it is required to indicate that the test passed (returning a nullptr indicates failure.

In your case we simply need to modify the corresponding test implementation:

virtual ParseResultBase* Parse(const char* j, size_t length) const {
(void)length;
NlohmannParseResult* pr = new NlohmannParseResult;
try {
pr->root = json::parse(j);
}
catch (...) {
delete pr;
return 0;
}
return pr;

If you feel like providing a pull request with this change I would be be happy to merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants