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

Use diagnostic positions in exceptions #4561

Open
nlohmann opened this issue Dec 19, 2024 · 4 comments
Open

Use diagnostic positions in exceptions #4561

nlohmann opened this issue Dec 19, 2024 · 4 comments

Comments

@nlohmann
Copy link
Owner

nlohmann commented Dec 19, 2024

We recently merged #4517 which added a macro JSON_DIAGNOSTIC_POSITIONS that, when defined, adds two std::size_t members to JSON values indicating the position of the first and last byte of this element in the input text. We should use this information to provide better diagnostic messages in exceptions.

@hnampally
Copy link
Contributor

@nlohmann I have been looking into this, I was wondering if Json_Diagnostic_Positions and Json_Diagnostics options are mutually exclusive, also please provide some hints for this task.

@nlohmann
Copy link
Owner Author

nlohmann commented Jan 1, 2025

No, the two options are not mutually exclusive:

So an example for the default behavior would be:

[json.exception.type_error.302] type must be number, but is string

for JSON_DIAGNOSTICS, this can be:

[json.exception.type_error.302] (/address/housenumber) type must be number, but is string

Now for JSON_DIAGNOSTIC_POSITIONS we can query the position in the input and could do something like this:

[json.exception.type_error.302] (byte 34-39) type must be number, but is string

And if both options are set, we could have both:

[json.exception.type_error.302] (/address/housenumber, byte 34-39) type must be number, but is string

@hnampally
Copy link
Contributor

Thanks for the clarification, I think I got it now, I will raise a PR shortly.

@hnampally
Copy link
Contributor

@nlohmann Please let me know if the changes I made 2cc096a makes sense, for now I just modified the existing examples to test the code, I wonder if I need to add a new example for this in docs and/or add/modify existing unit tests.

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

2 participants