{"id":21887684,"url":"https://github.com/olekspickle/serde_default_utils","last_synced_at":"2025-04-15T09:53:30.490Z","repository":{"id":216480327,"uuid":"741451808","full_name":"olekspickle/serde_default_utils","owner":"olekspickle","description":"A set of simple helper functions to cut corners with serde_default","archived":false,"fork":false,"pushed_at":"2024-11-18T03:50:18.000Z","size":20,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-13T20:03:10.935Z","etag":null,"topics":["convenience-functions","default","nostd","rust","serde"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/serde_default_utils","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/olekspickle.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-10T12:29:07.000Z","updated_at":"2024-11-18T03:50:22.000Z","dependencies_parsed_at":"2024-10-16T17:06:20.414Z","dependency_job_id":"5b2dfea9-ddef-4ffd-94f9-2226d1d86da6","html_url":"https://github.com/olekspickle/serde_default_utils","commit_stats":null,"previous_names":["alekspickle/serde_default_utils","olekspickle/serde_default_utils"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olekspickle%2Fserde_default_utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olekspickle%2Fserde_default_utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olekspickle%2Fserde_default_utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olekspickle%2Fserde_default_utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olekspickle","download_url":"https://codeload.github.com/olekspickle/serde_default_utils/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249048714,"owners_count":21204306,"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":["convenience-functions","default","nostd","rust","serde"],"created_at":"2024-11-28T11:11:59.719Z","updated_at":"2025-04-15T09:53:30.473Z","avatar_url":"https://github.com/olekspickle.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# serde_default_utils\n\n[![v](https://img.shields.io/badge/v-0.3.0-blueviolet)]()\n\n## Overview\nThis is a simple set of functions to make your life easier while working with defaults in serde.\nBased on [const generic parameters](https://doc.rust-lang.org/reference/items/generics.html#const-generics).\nHeavily inspired by discussions on issues about serde defaults, but mostly [this one](https://github.com/serde-rs/serde/issues/368)\n\n## Kudos\n- JohnTheCoolingFan posted this solution, I just made it available as crate and a macro that\n    helps to generate another const generic function for any const generic type.\n- bytedream         made a more powerful version of it, and although I still see const generic approach as more readable,\n    I have to admit that for strings it's superior, hence - included under the feature\n\n\n## Example\n```rust\n    use serde_default_utils::*;\n    use serde::{Deserialize, Serialize};\n\n        const JSON: \u0026str =\n            r#\"{\n                \"yes_or_no\":false,\n                \"max\":60,\n                \"delta\":-77,\n                \"delimeter\":\"☀\",\n                \"motto\":\"I matter\"\n            }\"#;\n        const INLINE_JSON: \u0026str =\n            r#\"{\n                \"inline_motto\":\"I matter even more\",\n                \"slice\":[\"I\", \"matter\", \"even\", \"more\"],\n                \"slice_u64\":[9000, 8000, 10000, 7000]\n            }\"#;\n    const EMPTY_JSON: \u0026str = r#\"{}\"#;\n    const MAX: u32 = 7;\n\n    #[derive(Serialize, Deserialize, Default)]\n    struct Config {\n        #[serde(default = \"default_bool::\u003ctrue\u003e\")]\n        yes_or_no: bool,\n        #[serde(default = \"default_i16::\u003c-3\u003e\")]\n        delta: i16,\n        // you can even use consts right here\n        #[serde(default = \"default_u32::\u003cMAX\u003e\")]\n        max: u32,\n        #[serde(default = \"default_char::\u003c'☀'\u003e\")]\n        delimeter: char,\n    }\n\n    #[cfg(feature = \"inline\")]\n    #[serde_inline_default]\n    #[derive(Serialize, Deserialize, Default)]\n    struct InlineConfig {\n        #[serde_inline_default(\"You matter even more\".to_string())]\n        inline_motto: String,\n        #[serde_inline_default(vec![\"You\".into(), \"matter\".into(), \"even\".into(), \"more\".into()])]\n        slice: Vec\u003cString\u003e,\n        #[serde_inline_default(vec![98712, 12346, 129389, 102937])]\n        slice_u64: Vec\u003cu64\u003e,\n    }\n\n    fn main() {\n        // existing json fields are not changed\n        let config: Config = serde_json::from_str(JSON).unwrap();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"yes_or_no\":false,\"delta\":-77,\"max\":60,\"delimeter\":\"☀\"}\"#, \u0026s);\n        // if the field is not present - it is substituted with defaults\n        let config: Config = serde_json::from_str(EMPTY_JSON).unwrap();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"yes_or_no\":true,\"delta\":-3,\"max\":7,\"delimeter\":\"☀\"}\"#, \u0026s);\n        // the default impl is just calling underlying type defaults unless you have a custom impl Default\n        let config = Config::default();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"yes_or_no\":false,\"delta\":0,\"max\":0,\"delimeter\":\"\\u0000\"}\"#, \u0026s);\n\n        // Inline\n        #[cfg(feature = \"inline\")]\n        {\n        // existing json fields are not changed\n        let config: InlineConfig = serde_json::from_str(INLINE_JSON).unwrap();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"inline_motto\":\"I matter even more\",\"slice\":[\"I\",\"matter\",\"even\",\"more\"],\"slice_u64\":[9000,8000,10000,7000]}\"#, \u0026s);\n        // if the field is not present - it is substituted with defaults\n        let config: InlineConfig = serde_json::from_str(EMPTY_JSON).unwrap();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"inline_motto\":\"You matter even more\",\"slice\":[\"You\",\"matter\",\"even\",\"more\"],\"slice_u64\":[98712,12346,129389,102937]}\"#, \u0026s);\n        // the default impl is just calling underlying type defaults unless you have a custom impl Default\n        let config = InlineConfig::default();\n        let s = serde_json::to_string(\u0026config).unwrap();\n        assert_eq!(r#\"{\"inline_motto\":\"\",\"slice\":[],\"slice_u64\":[]}\"#, \u0026s);\n        }\n    }\n\n```\n\nLicense: MIT OR Apache-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folekspickle%2Fserde_default_utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folekspickle%2Fserde_default_utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folekspickle%2Fserde_default_utils/lists"}