Crate serde_this_or_that

Crate serde_this_or_that 

Source
Expand description

githubcrates-iodocs-rs


Custom deserialization for fields that can be specified as multiple types.


§Usage

use serde::Deserialize;
use serde_json::from_str;
use serde_this_or_that::{as_bool, as_f64, as_opt_i64, as_u64};

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct MyStruct {
    #[serde(deserialize_with = "as_bool")]
    is_active: bool,
    #[serde(deserialize_with = "as_u64")]
    num_attempts: u64,
    #[serde(deserialize_with = "as_f64")]
    grade: f64,
    // uses #[serde(default)] in case the field is missing in JSON
    #[serde(default, deserialize_with = "as_opt_i64")]
    confidence: Option<i64>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let string = r#"
    {
        "isActive": "True",
        "numAttempts": "",
        "grade": "81",
        "confidence": "A+"
    }
    "#;

    let s: MyStruct = from_str(string)?;
    println!("{s:#?}");

    assert!(s.is_active);
    assert_eq!(s.num_attempts, 0);
    assert_eq!(s.grade, 81.0);
    assert_eq!(s.confidence, None);

    Ok(())
}

§Examples

You can check out sample usage of this crate in the examples/ folder in the project repo on GitHub.

§Performance

The benchmarks suggest that implementing a custom Visitor as serde-this-or-that does, performs on average about 15x better than an approach with an untagged enum.

A separate benchmark compares performance against the serde_with crate: it indicates both crates perform about the same, assuming only DisplayFromStr is used. But when PickFirst is involved, serde-this-or-that appears to perform about 12x better in an average case.

The benchmarks live in the benches/ folder, and can be run with cargo bench.

§Optionals

The extra helper functions that begin with as_opt, return an Option<T> of the respective data type T, rather than the type T itself (see #4).

On success, they return a value of T wrapped in Some.

On error, or when there is a null value, or one of an invalid data type, the as_opt helper functions return None instead.

§Readme Docs

You can find the crate’s readme documentation on the crates.io page, or alternatively in the README.md file on the GitHub project repo.

Functions§

as_bool
De-serialize either a null, bool, str, u64, or f64 as a boolean value.
as_f64
De-serialize either a null, str, f64, u64, or i64 as a float value.
as_i64
De-serialize either a null, str, i64, f64, or u64 as a signed value.
as_opt_bool
De-serialize either a bool, str, u64, or f64 as a boolean value wrapped in Some, and an i64 or null value as None.
as_opt_f64
De-serialize either a str, f64, u64, or i64 as a float value wrapped in Some, and a bool or null value as None.
as_opt_i64
De-serialize either a str, i64, f64, or u64 as a signed value wrapped in Some, and a bool or null value as None.
as_opt_string
De-serialize either a str, bool, i64, f64, or u64 as an (owned) string value wrapped in Some, and a null value as None.
as_opt_u64
De-serialize either a str, u64, f64, or i64 as an unsigned value wrapped in Some, and a bool or null value as None.
as_string
De-serialize either a null, str, bool, i64, f64, or u64 as an (owned) string value.
as_u64
De-serialize either a null, str, u64, f64, or i64 as an unsigned value.