[go: up one dir, main page]

sonic-rs 0.3.11

Sonic-rs is a fast Rust JSON library based on SIMD
Documentation
use sonic_rs::{from_slice, from_str, Deserialize};

fn main() {
    #[allow(dead_code)]
    #[derive(Debug, Deserialize)]
    struct Foo {
        a: Vec<i32>,
        c: String,
    }

    // deal with Eof errors
    let err = from_str::<Foo>("{\"a\": [").unwrap_err();
    assert!(err.is_eof());
    eprintln!("{}", err);
    // EOF while parsing at line 1 column 6

    //     {"a": [
    //     ......^
    assert_eq!(
        format!("{}", err),
        "EOF while parsing at line 1 column 6\n\n\t{\"a\": [\n\t......^\n"
    );

    // deal with unmatched type errors
    let err = from_str::<Foo>("{ \"b\":[]}").unwrap_err();
    eprintln!("{}", err);
    assert!(err.is_unmatched_type());
    // println as follows:
    // missing field `a` at line 1 column 8
    //
    //     { "b":[]}
    //     ........^
    assert_eq!(
        format!("{}", err),
        "missing field `a` at line 1 column 8\n\n\t{ \"b\":[]}\n\t........^\n"
    );

    // deal with Syntax errors
    let err = from_slice::<Foo>(b"{\"b\":\"\x80\"}").unwrap_err();
    eprintln!("{}", err);
    assert!(err.is_syntax());
    // println as follows:
    // Invalid UTF-8 characters in json at line 1 column 6
    //
    //     {"b":"�"}
    //     ......^...
    assert_eq!(
        format!("{}", err),
        "Invalid UTF-8 characters in json at line 1 column 6\n\n\t{\"b\":\"\"}\n\t......^..\n"
    );
}