Skip to content
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

Exception line #301

Closed
lucassardois opened this issue Aug 27, 2016 · 10 comments
Closed

Exception line #301

lucassardois opened this issue Aug 27, 2016 · 10 comments

Comments

@lucassardois
Copy link

Hello,
it's not an issue but since I found no place to post my question I put it here.
So my question is, is it possible to get the line where the exception was throw in the file ?
For exemple in this file:
{ "none" { "name": "holaaaaa" } }
I would like the exception to return 2 for exemple.
Thanks.

@nlohmann
Copy link
Owner

With issue #244, the exceptions will be overworked and parse errors will be annotated with a byte count to indicate the error location.

@nlohmann
Copy link
Owner

Parse errors will be thrown as json::parse_error exception which have a public member byte indicating the byte count to indicate the error location. Error messages look like this:

[json.exception.parse_error.101] parse error at 7: unexpected end of input; expected ':'

@haimat
Copy link

haimat commented Dec 11, 2016

I am new to this library, but so far it works great. Thanks for your good work! Just one question: I really need this feature, we need to know where in the input file the error was. How can I use this new feature, or when will it be released to the main branch?

@nlohmann
Copy link
Owner

@haimat This feature is planned for the 3.0.0 release - as introducing new exceptions means a change in the public API, we cannot release it to the 2.x.x track. In the meantime, you can check out the feature branch for this feature. It implements the described feature, but may change in the future when we merge it into the develop branch. I would look forward to hearing of your experiences.

@haimat
Copy link

haimat commented Dec 11, 2016

@nlohmann Thank you very much for the quick response. I have tested this new feature and it definitely is an improvement! However, having only the byte number in the stream without knowing the context where (i.e. in which line) the error is within the file is a bit hard to read. Any chance you will implement a translation from byte number to line/column?

@nlohmann
Copy link
Owner

@haimat I'm not sure how much effort this is, because the lexer skips all whitespace. To track a line/column count, this code needs to be adapted. I shall have a look at this, but other issues have priority.

@haimat
Copy link

haimat commented Dec 12, 2016

@nlohmann Does that mean the indicated byte-position is calculated after stripping away the whitespaces? In that case I am afraid it does not help at all, because then I could never match it against the original file :(

@nlohmann
Copy link
Owner

@haimat No, it is the exact byte count, including whitespace.

nlohmann added a commit that referenced this issue Mar 1, 2017
Added class hierarchy for user-defined exceptions (#244). Integrated
parse exceptions 101-103. Parse exceptions include the byte count of
the last read character to locate the position of the error (#301).
@nlohmann
Copy link
Owner

nlohmann commented Mar 8, 2017

This issue is nearly complete. If anyone would like to have a look at the soon-to-be-merged state, please have a look at branch https://github.com/nlohmann/json/tree/feature/exceptions_3.0.0. It contains parse_error exceptions with a byte count to the error, and only the documentation needs some final touches.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 12, 2017
@nlohmann
Copy link
Owner

Merged this feature into develop. All there is left to do is to document the changes between old and new exceptions in the wiki page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants