Skip to content

Commit

Permalink
Add documentation about preserving values
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 13, 2023
1 parent 3a1d4e0 commit 8ac7200
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ You can customize serialization from Rust to JavaScript by setting the following

You can also use the `Serializer::json_compatible()` preset to create a JSON compatible serializer. It enables `serialize_missing_as_null`, `serialize_maps_as_objects`, and `serialize_bytes_as_arrays` under the hood.

### Preserving JavaScript values

Sometimes you want to preserve original JavaScript value instead of converting it into a Rust type. This is particularly useful for types that can't be converted without losing the data, such as [`Date`](https://docs.rs/js-sys/latest/js_sys/struct.Date.html), [`RegExp`](https://docs.rs/js-sys/latest/js_sys/struct.RegExp.html) or 3rd-party types.

`serde_wasm_bindgen::preserve` allows you to do just that:

```rust
#[derive(Serialize, Deserialize)]
pub struct Example {
pub regular_field: i32,

#[serde(with = "serde_wasm_bindgen::preserve")]
pub preserved_date: js_sys::Date,

#[serde(with = "serde_wasm_bindgen::preserve")]
pub preserved_arbitrary_value: JsValue,
}
```

## License

Licensed under the MIT license. See the
Expand Down

0 comments on commit 8ac7200

Please sign in to comment.