Re: [boost] JSON parser error handling
On Mon, Apr 25, 2022 at 1:55 PM Kenneth Porter via Boost
I don't see any information on where in the source string the parse error happened so I have to set a breakpoint in my tag_invoke functions to figure out where the syntax errors lie.
You have two potentially throwing lines:
boost::json::value jv = boost::json::parse(contents);
and
axes = boost::json::value_to
--On Monday, April 25, 2022 4:49 PM -0700 Vinnie Falco
If your exception is coming from the call to parse() and you want to know where in the string you are, then you need to change your code to create an instance of json::parser and call parser::write or parser::write_some to feed in the input. These functions return the number of characters consumed, which will be less than the amount passed in if an error occurs. In this case, the number of characters parsed successfully can be used to calculate an offset into the input buffer to determine where the syntax error happened.
If your exception is coming from the call to value_to<>(), things are not so simple. The original JSON string is no longer available, and information about which json::value came from what part of the string is not stored (that would slow down the library and take up unnecessary memory).
Aha, I begin to understand the issues. Two different classes of error can occur. Catching a problem in the parse() call would catch JSON syntax errors. My JSON was correct as it had been machine-generated from an online XML translator. I was seeing semantic errors in turning a valid JSON structure into my custom C++ structure. So I somehow need to figure out where in the hierarchy the translation failed. Given that, I could output the JSON structure as text with an indicator where the structure wasn't what the translator expected.
participants (2)
-
Kenneth Porter
-
Vinnie Falco