serde-this-or-that

github crates.io docs.rs build status

Custom deserialization for fields that can be specified as multiple types.


This crate works with Cargo with a Cargo.toml like:

toml [dependencies] serde-this-or-that = "0.1" serde_json = "1.0"

Getting started

Add some usage to your application.

Here's an example of using serde-this-or-that in code:

```rust use serdejson::fromstr; use serdethisorthat::{asbool, as_u64, Deserialize};

[derive(Deserialize)]

[serde(rename_all = "camelCase")]

struct MyStruct { #[serde(deserializewith = "asbool")] isactive: bool, #[serde(deserializewith = "asu64")] numattempts: u64, }

fn main() -> Result<(), Box> { let string = r#" { "isActive": "True", "numAttempts": "3" } "#;

let s: MyStruct = from_str(string)?;

assert!(s.is_active);
assert_eq!(s.num_attempts, 3);

Ok(())

} ```

Examples

You can check out sample usage of this crate in the examples/ folder in the project repo on GitHub.

Performance

The benchmarks suggest that implementing a custom Visitor as serde-this-or-that does, performs on average about 10x better than an approach with an [untagged enum].

The benchmarks live in the benches/ folder, and can be run with cargo bench.

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

Check out the Contributing section in the docs for more info.

License

This project is proudly licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

serde-this-or-that can be distributed according to the MIT license. Contributions will be accepted under the same license.

Authors