#deserialize-json #serde-json #serialize-deserialize #null #value #variant #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

#1647 in Encoding

Download history 2613/week @ 2024-06-20 2382/week @ 2024-06-27 2331/week @ 2024-07-04 2420/week @ 2024-07-11 2319/week @ 2024-07-18 3049/week @ 2024-07-25 2284/week @ 2024-08-01 2230/week @ 2024-08-08 1819/week @ 2024-08-15 1826/week @ 2024-08-22 2069/week @ 2024-08-29 2643/week @ 2024-09-05 2450/week @ 2024-09-12 2195/week @ 2024-09-19 2233/week @ 2024-09-26 1757/week @ 2024-10-03

8,973 downloads per month
Used in 47 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