My QT-based single-threaded console app running on Linux parses JSON strings using Boost, and it works fine ordinarily, except when receiving very large chunks of JSON. I have a piece of valid JSON around 160kb in size (!) and when I attempt to parse it, the call to Boost’s JSON parser never returns. I’ve left it a considerable time. If I subsequently break in using the debugger, my app is idly sitting in its message loop, as though nothing happened. The call throws no exceptions. There’s nothing noteworthy about the JSON except for its large size – it’s well-formed and entirely composed of ASCII characters.
How can execution simply “give up” and return to the QT message loop?
void IncomingRequestHandler::OnRequest(const QString& message)
{
try
{
std::stringstream ss;
ss << message.toStdString();
boost::property_tree::ptree requestObject;
cout << "Before read_json" << endl; // Gets here
boost::property_tree::json_parser::read_json(ss, requestObject);
cout << "After read_json" << endl; // Never gets here
// ... Some other code ...
}
catch (const boost::property_tree::json_parser::json_parser_error& e)
{
cout << "Invalid JSON" << endl; // Never gets here
}
catch (const std::runtime_error& e)
{
cout << "Invalid JSON" << endl; // Never gets here
}
catch (...)
{
cout << "Invalid JSON" << endl; // Never gets here
}
}
First, I agree with two comments above: try to minimize your program.
Second, I would try to check whether Qt (stl, boost, this particular version of whatever) can handle strings that large. Make sure your parser is getting the whole string.
Third, I would use ostringstream rather than sstream. 🙂
According to boost documentation, it seems that the only way the parser returns an error is by returning error information in your property_tree. If it continues reading forever, it probably means that it’s reading garbage beyond the actual JSON data and gets stuck on it.
Finally, read_json can accept the file name, so why bother with reading the file and creating a stream? Why don’t you try this:
I’ve just performed a little test with JSON file 400Kb big, and it works just fine: