2 releases

0.1.1 Jun 18, 2022
0.1.0 May 10, 2021

#866 in Parser implementations

Download history 3632/week @ 2023-05-26 5557/week @ 2023-06-02 7449/week @ 2023-06-09 8874/week @ 2023-06-16 8371/week @ 2023-06-23 7293/week @ 2023-06-30 7331/week @ 2023-07-07 2493/week @ 2023-07-14 2455/week @ 2023-07-21 2993/week @ 2023-07-28 2776/week @ 2023-08-04 2228/week @ 2023-08-11 2221/week @ 2023-08-18 2393/week @ 2023-08-25 2201/week @ 2023-09-01 1780/week @ 2023-09-08

9,063 downloads per month
Used in 37 crates (3 directly)

MIT license

10KB
139 lines

js_option

This crate provides a type JsOption that is very similar to the standard library's Option type except that it has three variants:

  • Some(value): Like Option::Some
  • Null: Explicitly not some value
  • Undefined: Implicitly not some value

This type can be useful when you want to deserialize JSON to a Rust struct and not loose information: A regular Option deserializes to None from both an explicit null or a missing field (this is due to special casing of Option in the Deserialize and Serialize derive macros, for other types a missing field will make deserialization fail unless there is a #[serde(skip)], #[serde(skip_deserializing)] or #[serde(default)] attribute).

Example:

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct MyStruct {
    #[serde(default, skip_serializing_if = "JsOption::is_undefined")]
    my_field: JsOption<String>,
}

License

MIT

Dependencies

~180KB