#serialization #serde-default #undefined #variant #option-like #serde-json #deserialize

js_option

An Option-like type with separate null and undefined variants

3 unstable releases

0.2.0 Sep 14, 2025
0.1.1 Jun 18, 2022
0.1.0 May 10, 2021

#240 in Encoding

Download history 5476/week @ 2025-07-21 5708/week @ 2025-07-28 6603/week @ 2025-08-04 7728/week @ 2025-08-11 8340/week @ 2025-08-18 10363/week @ 2025-08-25 8675/week @ 2025-09-01 8502/week @ 2025-09-08 8721/week @ 2025-09-15 8686/week @ 2025-09-22 18836/week @ 2025-09-29 7809/week @ 2025-10-06 7444/week @ 2025-10-13 7676/week @ 2025-10-20 7934/week @ 2025-10-27 7801/week @ 2025-11-03

31,118 downloads per month
Used in 65 crates (7 directly)

MIT license

10KB
138 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

~135KB