2 releases

0.1.1 Jun 18, 2022
0.1.0 May 10, 2021

#272 in Rust patterns

Download history 1170/week @ 2022-08-11 1043/week @ 2022-08-18 916/week @ 2022-08-25 1247/week @ 2022-09-01 1302/week @ 2022-09-08 1401/week @ 2022-09-15 1406/week @ 2022-09-22 1190/week @ 2022-09-29 1347/week @ 2022-10-06 993/week @ 2022-10-13 1053/week @ 2022-10-20 1544/week @ 2022-10-27 2280/week @ 2022-11-03 2108/week @ 2022-11-10 1723/week @ 2022-11-17 1624/week @ 2022-11-24

8,084 downloads per month
Used in 32 crates (2 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:

# extern crate serde_crate as serde;
use serde::{Deserialize, Serialize};

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

License

MIT

Dependencies

~130–250KB