#serde #serialization

serde_path_to_error

Path to the element that failed to deserialize

9 releases

0.1.8 Aug 3, 2022
0.1.7 Jan 27, 2022
0.1.5 Sep 15, 2021
0.1.4 Dec 16, 2020
0.1.1 Jan 7, 2019

#171 in Encoding

Download history 26733/week @ 2022-04-21 27558/week @ 2022-04-28 26183/week @ 2022-05-05 23487/week @ 2022-05-12 25012/week @ 2022-05-19 23837/week @ 2022-05-26 27649/week @ 2022-06-02 30842/week @ 2022-06-09 29295/week @ 2022-06-16 32860/week @ 2022-06-23 39570/week @ 2022-06-30 50743/week @ 2022-07-07 42983/week @ 2022-07-14 41842/week @ 2022-07-21 48517/week @ 2022-07-28 40942/week @ 2022-08-04

181,729 downloads per month
Used in 175 crates (50 directly)

MIT/Apache

81KB
2.5K SLoC

Serde path to error

github crates.io docs.rs build status

Find out the path at which a deserialization error occurred. This crate provides a wrapper that works with any existing Serde Deserializer and exposes the chain of field names leading to the error.

[dependencies]
serde = "1.0"
serde_path_to_error = "0.1"
use serde::Deserialize;
use std::collections::BTreeMap as Map;

#[derive(Deserialize)]
struct Package {
    name: String,
    dependencies: Map<String, Dependency>,
}

#[derive(Deserialize)]
struct Dependency {
    version: String,
}

fn main() {
    let j = r#"{
        "name": "demo",
        "dependencies": {
            "serde": {
                "version": 1
            }
        }
    }"#;

    // Some Deserializer.
    let jd = &mut serde_json::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();
            assert_eq!(path, "dependencies.serde.version");
        }
    }
}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~135–255KB