json icon indicating copy to clipboard operation
json copied to clipboard

Error position wrong for "unicode code point" error

Open samuelcolvin opened this issue 1 year ago • 1 comments

The following

fn main() {
    let json = vec![b'"', 128, b' ', b' ', b' ', b'"'];
    let e = serde_json::from_slice::<String>(&json).unwrap_err();
    println!("serde_json error: {e}");
    let e = std::str::from_utf8(&json).unwrap_err();
    println!("from_utf error:   {e}");
}

outputs:

serde_json error: invalid unicode code point at line 1 column 6
from_utf error:   invalid utf-8 sequence of 1 bytes from index 1

serde_json claims the error position was a the end of the JSON string, not where it actually was. If you insert more spaces, the error always comes at the next ".

In comparison, if the error is caused by a control character, the position is correct:

fn main() {
    // let json = vec![b'"', 128, b' ', b' ', b' ', b'"'];
    let json = vec![b'"', b'\t', b' ', b' ', b' ', b'"'];
    let e = serde_json::from_slice::<String>(&json).unwrap_err();
    println!("serde_json error: {e}");
}

outputs:

serde_json error: control character (\u0000-\u001F) found while parsing a string at line 1 column 2

I'm using serde_json version 1.0.87.

samuelcolvin avatar Feb 04 '24 21:02 samuelcolvin

Bump. Anyone?

samuelcolvin avatar Feb 18 '24 08:02 samuelcolvin