json icon indicating copy to clipboard operation
json copied to clipboard

Consider adding `offset` and `length` methods to `serde_json::Error`

Open alilleybrinker opened this issue 2 years ago • 1 comments

As it stands, serde_json::Error includes the line number and column number of the start of an item whose parsing resulted in a parse error. This is great, but doesn't interoperate well with other things (in my case, miette) which expect a byte offset and byte length of the offending error.

Use case: I'm trying to wire up serde_json with miette to get prettier / clearer error messages in test code which tries to deserialize a variety of JSON files with a specific, fairly complex, schema.

The line and column numbers, in combination with the original program text, can be resolved into the initial byte offset, though the length then would require tokenization of the specific token at that byte offset.

Would it be possible for serde_json::Error to instead expose Error::offset and Error::length methods (names subject to bikeshedding)?

alilleybrinker avatar Jun 12 '23 19:06 alilleybrinker

Aha! It turns out miette has some functionality to do what I need already. This may still be worth considering anyway, but my immediate use cases is resolved.

alilleybrinker avatar Jun 12 '23 19:06 alilleybrinker