#serialization #variant #deserialize #value

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

#642 in Parser implementations

Download history 3066/week @ 2024-12-14 1349/week @ 2024-12-21 1602/week @ 2024-12-28 3254/week @ 2025-01-04 3452/week @ 2025-01-11 3101/week @ 2025-01-18 3311/week @ 2025-01-25 4313/week @ 2025-02-01 4032/week @ 2025-02-08 4057/week @ 2025-02-15 4292/week @ 2025-02-22 4121/week @ 2025-03-01 4153/week @ 2025-03-08 5073/week @ 2025-03-15 3748/week @ 2025-03-22 4196/week @ 2025-03-29

17,900 downloads per month
Used in 52 crates (5 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

~155KB