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

Errors report incorrect column number when JSON includes multibyte characters #1048

Open
fpdotmonkey opened this issue Jul 30, 2023 · 0 comments

Comments

@fpdotmonkey
Copy link

fpdotmonkey commented Jul 30, 2023

If you have a JSON packet that has multibyte, i.e. non-ASCII, characters, then errors reported related to that packet such as the data error I trigger below will report a higher column number than you would see in a text editor, likely a byte offset.

This renders the reported column number useless in instances of non-English data or emojis and is magnified if the data is a large single-line packet, which isn't uncommon.

use serde::{Deserialize, Serialize};

fn main() {
    // `'not a number'` is at line 1 column 66
    let json = "{\"my_ascii_chars\": \"asdf\", \"my_utf8_chars\": \"/ˈa.ʎo/\", \"my_int\": \"not a number\"}";
    let test: Option<ColumnTest> = match serde_json::from_str::<ColumnTest>(json) {
        Ok(_) => panic!("no error generated"),
        Err(error) => {
            println!("{error}");
            assert_eq!(error.column(), 66);
            None
        }
    };
}

#[derive(Debug, Serialize, Deserialize)]
struct ColumnTest {
    my_ascii_chars: String,
    my_utf8_chars: String,
    my_int: u32,
}

Output:

invalid type: string "not a number", expected u32 at line 1 column 81
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `81`,
 right: `66`', src/main.rs:10:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant