-
-
Notifications
You must be signed in to change notification settings - Fork 330
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
Error message if parsing fails #79
Comments
you should print log |
This is actually quite nice solution (extending JSONValue union with error case). I don't understand however why you are creating an error message in parson. In my opinion the library should try not handle any end user communication. It should return structure with line and column numbers and preferably a type of error. With this information the application which is using the library can form an error message in whatever way it finds suitable. Please note that there are also some minor style issues in your patch:
|
The API for that might look something like this:
In the future, new I have cleaned up the style inconsistencies in PR #78. I also changed |
Sorry for taking so long to respond, but I've been quite busy recently. Instead of extending existing function a nicer solution would be to add a new one. I really don't want people to use a different API based on a compiler flag. Example: enum json_parse_error_t {
JSON_PARSE_ERROR_NONE = 0,
JSON_PARSE_ERROR_MISSING_BRACKET,
...
};
typedef struct json_parse_error_t {
enum json_parse_error_t error;
unsigned int line;
unsigned int line_offset;
} JSON_Parse_Error;
typedef struct json_parse_option_t {
int accept_comments;
} JSON_Parse_Options;
JSON_Value* json_parse(const char *string, JSON_Parse_Options parse_options, JSON_Parse_Error *err); /* err is optional and can be NULL */ It's only a proposal though. The real issue is that the API feels a bit dated and inconsistent in some places. It's been growing gradually over last 5 years and a cleanup would be nice at some point in future (parson 2.0?). I cannot guarantee any timeline, but it's something I keep at the back of my mind. Anyway, I'd rather not accept your pull request (#78), because I don't want I'm going to keep this issue open because other people might have a similar problem and it needs to be resolved at some point. |
I don't mind the closed pull request. It was just a starting point for what this could look like. I should have just referenced the branch on my fork. I would suggest including the offset of the error from the beginning of the string as well. It makes it easy for applications to directly show the text that caused the failure. With only the line and offset they would have to rescan the string to count lines. What kind of ABI stability are you going for with parson? Adding an option to the proposed JSON_Parse_Options struct would change its size. Some option APIs:
A few more awkward APIs I've seen.
Technically, the proposed JSON_Parse_Error struct has the same issue. But I'm having a hard time coming up with plausible changes to it other than adding the absolute string offset. Maybe using size_t to support really large files. |
I've implemented the proposed API with a few adjustments on my parse-errors-v2 branch 880a058 to see what it would look like. Changes:
Basically, I added a While updating the tests, I noticed some test_suite_3 failures were for different reasons than the comments indicated. These three have "control character" comments after them.
I added tests so each error type has at least one test. I also added tests for the three Parse Validation failures in issue #53 that are fixed by using |
Currently, when parsing fails a NULL value is returned and my software can only log a "something went wrong" message. It would be great to be able to log the line number and line contents that caused the failure.
I apologize for not reading the bottom of the readme about creating issues before a pull request. Pull request #78 shows a minimally invasive way to implement this. It returns a JSONError value with an error message in the string field.
The text was updated successfully, but these errors were encountered: