File: README.md

package info (click to toggle)
rust-js-option 0.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 96 kB
  • sloc: makefile: 2
file content (33 lines) | stat: -rw-r--r-- 1,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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](https://opensource.org/licenses/MIT)