google / serde_json5

Apache License 2.0
12 stars 9 forks source link

Extend the public interface with Deserializer #3

Closed xamgore closed 6 months ago

xamgore commented 11 months ago

If there was an error while matching the parsed data with the defined struct, the path a.b.c[0] may be returned. One could use serde_path_to_error crate for that, but it requires access to the Deserializer. This PR is for that.

    #[derive(Deserialize)]
    struct Data { a: String };

    let j = r#"{ b: 42 }"#;

    // Some Deserializer.
    let jd = &mut serde_json5::Deserializer::from_str(j)?;

    let result: Result<Package, _> = serde_path_to_error::deserialize(jd);
    match result {
        Ok(_) => panic!("expected a type error"),
        Err(err) => {
            let path = err.path().to_string();
            println!("{}", path);
        }
    }
google-cla[bot] commented 11 months ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

xamgore commented 11 months ago

Signed CLA.

gebressler commented 6 months ago

LGTM, sorry for the long wait!