handling invalid input data

  • Anonymous - 2012-07-11

    First, let me say thank you for this great library.

    I am using it to parse JSON-formatted data coming from a web service (the contents of which I have no control over). The specification - and implementation - for this service are still somewhat in flux, and unfortunately the data are not always correctly formatted JSON syntax. Depending on the exact syntax error, there seems to be several different ways that libjson signals this: throwing a std::invalid_argument exception, calling the error callback, or just returning a JSON_NULL node. The libjson::is_valid() function seems to catch all of the different syntax errors, but for performance reasons I am trying to avoid calling is_valid() and parse() together as that seems like it would have to effectively parse everything twice.

    Maybe I've missed something (like some combination of configuration #defines), but is there any way to report invalid input data in a single way? I don't care which way it is, as long as it is consistent.


  • Jonathan Wallace

    Sorry for the slow response.

    If you turn on the JSON_PREPARSE option, it will do both validation and parsing in one go.  The reason that it's not possible without it is because libjson uses lazy parsing (it only parses it once something inside of it is requested) so it can't validate it.  The preparse option is meant for this.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks