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

Improve Debugging std.json by Adding std.log.Level to ParseOptions #17995

Closed
VisenDev opened this issue Nov 13, 2023 · 4 comments
Closed

Improve Debugging std.json by Adding std.log.Level to ParseOptions #17995

VisenDev opened this issue Nov 13, 2023 · 4 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@VisenDev
Copy link

Zig Version

0.12.0-dev.1383+7c5d01b95

Steps to Reproduce and Observed Output

Currently when parsing a json file, the only way errors are logged is thru the zig error system - which is fine for most applications - but it is not great for parsers since it makes it very unclear where the parsing failed and why. See #16936

One way to fix this would be this proposal #2647, however since this has been open for several years now, it is unlikely that any change like this will happen soon.

Alternatively an outparameter could be passed which records the location of an error in the input string. This sort of approach is very common in a lot of C codebases.

As a much simpler solution that doesn't involve any language level change would be to add a log level field to std.json.ParseOptions which would enable logging for the progress of parsing and what exactly caused the parser to fail.

A similar example of an approach like this in the standard library would be the verbose_log parameter of the GeneralPurposeAllocator Config which logs when memory allocations are happening.

Expected Output

The std.json parser would be able to log the cause and location of an error on the event that parsing fails. This would make it much easier to track down why parsing fails and make zig a more pleasant and robust language to use.

This use case comes from me doing a lot of json parsing on my personal project in zig. Debugging json parsing issues right now is not super pleasant

@VisenDev VisenDev added the error message This issue points out an error message that is unhelpful and should be improved. label Nov 13, 2023
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Nov 13, 2023
@Vexu Vexu added this to the 0.13.0 milestone Nov 13, 2023
@VisenDev
Copy link
Author

If this has approval from the core team, I may try and give this a shot?
@Vexu

@Vexu
Copy link
Member

Vexu commented Nov 14, 2023

I'd wait to hear @thejoshwolfe's opinion on how this could be done.

@thejoshwolfe
Copy link
Contributor

Thanks for the ping @Vexu ! (I only have notifications setup for pull requests about the json code, not issues, so i would never have seen this without the ping. thank you!) And sorry for ignoring #16936 ; i didn't see that issue.

Alternatively an outparameter could be passed which records the location of an error in the input string. This sort of approach is very common in a lot of C codebases.

There is a mechanism in place already that is basically this. https://ziglang.org/documentation/master/std/#A;std:json.Diagnostics

If you're using json.parseFromSlice(), you'll need to switch to parseFromTokenSource(), but the implementation of the former makes it pretty clear how to switch to the latter.

Does this solve your problem?

@VisenDev
Copy link
Author

I see, I was not aware that the stdlib had this functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

3 participants