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

Get line and column number on valid JSON? #637

Open
oOBoomberOo opened this issue Mar 27, 2020 · 3 comments · May be fixed by #1246
Open

Get line and column number on valid JSON? #637

oOBoomberOo opened this issue Mar 27, 2020 · 3 comments · May be fixed by #1246

Comments

@oOBoomberOo
Copy link

I'm currently parsing JSON data and validating its input and if there is invalid input I'll display the error message to the user.
I can't just use Deserializer trait because to validate these input I need to populate external data first.

@MaulingMonkey
Copy link

MaulingMonkey commented Oct 1, 2020

I recently wrote json-spanned-value = "0.2" (github, crates.io, docs.rs) to tackle this. Caveats:

  • Currently requires the JSON be loaded into memory first
  • Byte offsets only, you'll need to create line/column information yourself
  • (Ab)uses the fact that serde_json only reads one byte at a time from std::io::Read s
  • Might not play nicely with #[serde(untagged)] enums, or anything else that might make multiple attempts to parse data
  • Requires you to use json_spanned_value::from_* instead of serde_json::from_*
  • Only one user to find all the bugs, missing features, and edge cases so far - myself

That said, it seems to be working OK for my use cases - maybe give it a whirl? Basically, you just wrap anything you want a span of with json_spanned_value::Spanned<...>. You can stick them in Deserializeable struct, or use a json_spanned_value::Value which recursively wraps a whole tree in Spanned sections.

I've got an examples/demo.rs using codespan-reporting = "0.9.5" that can be matched via .vscode/tasks.json problemMatcher:

image

@heaths
Copy link

heaths commented Nov 2, 2023

Since serde::Deserializer (and impl) are already visiting each byte of a string or stream, wouldn't it be more efficient to optionally - via feature - parse any combination of \r and \n (pairs treated as a single newline) and pass them to an optionally-implemented visit_newline or something? An implementation such as serde_json could keep track of line numbers at least so it could opt to attach it to serde_json::Value - again, probably as an optional feature to reduce allocations.

toml::Spanned does indeed keep track of bytes, but in many cases a byte offset and length is useful to end users, and without doubly consuming and parsing newlines from a string or stream, an implementation can't efficiently determine line numbers.

@demurgos
Copy link

I'm encoutering a similar issue where I want to parse a JSON document while keeping track of source locations. serde_spanned from the toml crate is probably the best start; but I 100% agree that a (line, column) pair would help a lot for UI, on top of the byte offset.

@robsdedude robsdedude linked a pull request Mar 11, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants