Return a JSONError value with an error string if parsing fails #78
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, parse failures return NULL and I just have to print a generic "something went wrong" message. It would be nice to have some kind of error message pointing to the line with the problem.
This change makes the parse functions try to return a value of type JSONError with a string value containing an error message. If it cannot allocate the value then it still returns NULL. I updated the test code accordingly. Since this could break programs that expect only NULL on failure I hid it behind a preprocessor definition.
The only other API change is adding
const char* json_value_get_error(const JSON_Value *value)
.The main limitation is that the error message contains the line number after comments are removed which may not match the line number the user sees in a file. The error message also contains part of the line that parsing failed on, so users could notice the line number and line content do not match.
I've considered a few other related changes but did not want to make overly invasive changes without finding out your preferences.
json_parse()
function that accepts a struct with the filename/string and feature flags (remove comments, return error values, skip line numbers, etc).]
explain that it is an unmatched array bracket.},
and the previous non-whitespace character is a comma then explain that trailing commas are invalid.It's no problem if you want to rewrite any of this or would prefer me to implement it differently. I tried to match the existing code and comment style.