{"id":17169138,"url":"https://github.com/rnag/serde-this-or-that","last_synced_at":"2025-04-10T01:12:54.169Z","repository":{"id":42020312,"uuid":"482284975","full_name":"rnag/serde-this-or-that","owner":"rnag","description":"Custom deserialization for fields that can be specified as multiple types.","archived":false,"fork":false,"pushed_at":"2025-01-25T05:32:14.000Z","size":118,"stargazers_count":19,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-10T01:12:49.948Z","etag":null,"topics":["crates","deserialization","json","multiple-type","rust","serde","this-or-that","untagged","visitor-pattern"],"latest_commit_sha":null,"homepage":"https://docs.rs/serde-this-or-that","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rnag.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-16T15:06:01.000Z","updated_at":"2025-01-25T05:31:22.000Z","dependencies_parsed_at":"2023-02-19T02:40:18.841Z","dependency_job_id":null,"html_url":"https://github.com/rnag/serde-this-or-that","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnag%2Fserde-this-or-that","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnag%2Fserde-this-or-that/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnag%2Fserde-this-or-that/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnag%2Fserde-this-or-that/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rnag","download_url":"https://codeload.github.com/rnag/serde-this-or-that/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137891,"owners_count":21053775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crates","deserialization","json","multiple-type","rust","serde","this-or-that","untagged","visitor-pattern"],"created_at":"2024-10-14T23:25:10.635Z","updated_at":"2025-04-10T01:12:54.128Z","avatar_url":"https://github.com/rnag.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# serde-this-or-that\n\n[\u003cimg alt=\"github\" src=\"https://img.shields.io/badge/github-source-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\" height=\"22\"\u003e](https://github.com/rnag/serde-this-or-that)\n[\u003cimg alt=\"crates.io\" src=\"https://img.shields.io/crates/v/serde-this-or-that.svg?style=for-the-badge\u0026color=fc8d62\u0026logo=rust\" height=\"22\"\u003e](https://crates.io/crates/serde-this-or-that)\n[\u003cimg alt=\"docs.rs\" src=\"https://img.shields.io/docsrs/serde-this-or-that/latest?style=for-the-badge\u0026labelColor=555555\u0026logoColor=white\u0026logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K\" height=\"22\"\u003e](https://docs.rs/serde-this-or-that)\n[\u003cimg alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/rnag/serde-this-or-that/build.yml?branch=main\u0026style=for-the-badge\" height=\"22\"\u003e](https://github.com/rnag/serde-this-or-that/actions?query=branch%3Amain)\n\nCustom deserialization for fields that can be specified as multiple types.\n\n---\n\nThis crate works with Cargo with a `Cargo.toml` like:\n\n```toml\n[dependencies]\nserde-this-or-that = \"0.5.0\"\nserde = { version = \"1\", features = [\"derive\"] }\nserde_json = \"1\"\n```\n\n## Getting started\n\nAdd some usage to your application.\n\nHere's an example of using `serde-this-or-that` in code:\n\n```rust\nuse serde::Deserialize;\nuse serde_json::from_str;\nuse serde_this_or_that::{as_bool, as_f64, as_opt_i64, as_u64};\n\n#[derive(Deserialize, Debug)]\n#[serde(rename_all = \"camelCase\")]\nstruct MyStruct {\n    #[serde(deserialize_with = \"as_bool\")]\n    is_active: bool,\n    #[serde(deserialize_with = \"as_u64\")]\n    num_attempts: u64,\n    #[serde(deserialize_with = \"as_f64\")]\n    grade: f64,\n    // uses #[serde(default)] in case the field is missing in JSON\n    #[serde(default, deserialize_with = \"as_opt_i64\")]\n    confidence: Option\u003ci64\u003e,\n}\n\nfn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let string = r#\"\n    {\n        \"isActive\": \"True\",\n        \"numAttempts\": \"\",\n        \"grade\": \"81\",\n        \"confidence\": \"A+\"\n    }\n    \"#;\n\n    let s: MyStruct = from_str(string)?;\n    println!(\"{s:#?}\");\n\n    assert!(s.is_active);\n    assert_eq!(s.num_attempts, 0);\n    assert_eq!(s.grade, 81.0);\n    assert_eq!(s.confidence, None);\n\n    Ok(())\n}\n```\n\n## Exported Functions\n\n- [`as_bool`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_bool.html) / [\n  `as_opt_bool`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_opt_bool.html)\n- [`as_f64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_f64.html) / [\n  `as_opt_f64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_opt_f64.html)\n- [`as_i64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_i64.html) / [\n  `as_opt_i64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_opt_i64.html)\n- [`as_string`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_string.html) / [\n  `as_opt_string`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_opt_string.html)\n- [`as_u64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_u64.html) / [\n  `as_opt_u64`](https://docs.rs/serde-this-or-that/latest/serde_this_or_that/fn.as_opt_u64.html)\n\n## Examples\n\nYou can check out sample usage of this crate in\nthe [examples/](https://github.com/rnag/serde-this-or-that/tree/main/examples)\nfolder in the project repo on GitHub.\n\n## Performance\n\nThe benchmarks suggest that implementing a custom\n[`Visitor`] as `serde-this-or-that` does, performs\non average **about 15x better** than an approach with an [untagged enum].\n\nA separate benchmark compares performance against the [serde_with] crate:\nit indicates both crates perform about the same,\nassuming only [`DisplayFromStr`] is used.\nBut when [`PickFirst`] is involved, `serde-this-or-that` appears to\nperform **about 12x better** in an average case.\n\nThe benchmarks live in the [benches/](https://github.com/rnag/serde-this-or-that/tree/main/benches)\nfolder, and can be run with `cargo bench`.\n\n[`Visitor`]: https://docs.serde.rs/serde/de/trait.Visitor.html\n\n[untagged enum]: https://stackoverflow.com/a/66961340/10237506\n\n[serde_with]: https://docs.rs/serde_with\n\n[`DisplayFromStr`]: https://docs.rs/serde_with/latest/serde_with/struct.DisplayFromStr.html\n\n[`PickFirst`]: https://docs.rs/serde_with/latest/serde_with/struct.PickFirst.html\n\n## Optionals\n\nThe extra helper functions that begin with `as_opt`, return an `Option\u003cT\u003e` of the respective data type `T`,\nrather than the type `T` itself (see [#4](https://github.com/rnag/serde-this-or-that/issues/4)).\n\nOn success, they return a value of `T` wrapped in [\n`Some`](https://doc.rust-lang.org/std/option/enum.Option.html#variant.Some).\n\nOn error, or when there is a `null` value, or one of an *invalid* data type, the\n`as_opt` helper functions return [`None`](https://doc.rust-lang.org/std/option/enum.Option.html#variant.None) instead.\n\n## Contributing\n\nContributions are welcome! Open a pull request to fix a bug, or [open an issue][]\nto discuss a new feature or change.\n\nCheck out the [Contributing][] section in the docs for more info.\n\n[Contributing]: CONTRIBUTING.md\n\n[open an issue]: https://github.com/rnag/serde-this-or-that/issues\n\n## License\n\nThis project is proudly licensed under the MIT license ([LICENSE](LICENSE)\nor http://opensource.org/licenses/MIT).\n\n`serde-this-or-that` can be distributed according to the MIT license. Contributions\nwill be accepted under the same license.\n\n## Authors\n\n* [Ritvik Nag](https://github.com/rnag)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnag%2Fserde-this-or-that","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frnag%2Fserde-this-or-that","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnag%2Fserde-this-or-that/lists"}