#deserialize #json #variant #null #value #options #undefined

js_option

An Option-like type with separate null and undefined variants

2 releases

0.1.1 Jun 18, 2022
0.1.0 May 10, 2021

#976 in Parser implementations

Download history 2157/week @ 2023-12-04 2651/week @ 2023-12-11 2395/week @ 2023-12-18 2036/week @ 2023-12-25 2199/week @ 2024-01-01 2366/week @ 2024-01-08 2404/week @ 2024-01-15 2533/week @ 2024-01-22 2335/week @ 2024-01-29 2944/week @ 2024-02-05 2542/week @ 2024-02-12 2485/week @ 2024-02-19 2948/week @ 2024-02-26 2528/week @ 2024-03-04 2296/week @ 2024-03-11 2873/week @ 2024-03-18

10,818 downloads per month
Used in 41 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:

# 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

~175KB