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

how to print out the line which causing exception? #806

Closed
todatamining opened this issue Oct 28, 2017 · 3 comments
Closed

how to print out the line which causing exception? #806

todatamining opened this issue Oct 28, 2017 · 3 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@todatamining
Copy link

todatamining commented Oct 28, 2017

Something wrong with my json, but it just print out:

[json.exception.type_error.302] type must be array, but is null

From the error, it is hard to tell which line is wrong in the json file or my code, is there a way to print out the line causing exception?

update:
I check the json, the error is causing by the following line:

{
"email": [....]
}

in the code, there is a typo

vector emails = info["emails"]; //should be "email"

is it possible to print out the line#?

@nlohmann
Copy link
Owner

No, this is not possible. Once the JSON text is parsed and the json value is constructed, the line numbers are discarded. However, the stack trace of the thrown exception should give you the exact line of the implicit conversion that failed.

@nlohmann
Copy link
Owner

(Explanation for the error: when you call info["emails"] on a non-const json object, the [] creates null entries when it does not find an element at key "emails". As null cannot be converted to an array-like container such as std::vector, exception 302 occurs. Note this is not a parse error, as the JSON object is perfectly fine. Therefore, we cannot provide a line number. If you had used info.at("emails") instead of info["emails"], an exception would have indicated that the object does not contain an entry with key "emails", which would have made diagnostics easier.)

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 28, 2017
@todatamining
Copy link
Author

Great! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants