https://github.com/bytedream/serde-inline-default
Serde default values via inline attribute declaration
https://github.com/bytedream/serde-inline-default
rust rust-serde serde serde-json serde-serialization
Last synced: 5 months ago
JSON representation
Serde default values via inline attribute declaration
- Host: GitHub
- URL: https://github.com/bytedream/serde-inline-default
- Owner: bytedream
- License: apache-2.0
- Created: 2023-02-11T14:28:45.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-02T12:14:37.000Z (9 months ago)
- Last Synced: 2026-01-02T09:09:27.701Z (5 months ago)
- Topics: rust, rust-serde, serde, serde-json, serde-serialization
- Language: Rust
- Homepage:
- Size: 28.3 KB
- Stars: 36
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# serde-inline-default [](https://github.com/ByteDream/serde-inline-default/actions/workflows/ci.yml) [](https://crates.io/crates/serde-inline-default) [](https://crates.io/crates/serde-inline-default) [](https://docs.rs/serde-inline-default/latest/serde_inline_default/)
A tiny crate to set default values for serde struct fields via inline attribute declaration.
## Overview
This crate is an approach to do what [serde-rs/serde#368](https://github.com/serde-rs/serde/issues/368) purposes: Defining default values for struct fields via inline declaration instead of creating a separate function for it.
So instead of writing something like this, which can get very verbose quickly with many fields:
```rust
#[derive(Deserialize)]
struct Test {
#[serde(default = "value_default")]
value: u32
}
fn value_default() -> u32 { 42 }
```
you can just do this:
```rust
#[serde_inline_default]
#[derive(Deserialize)]
struct Test {
#[serde_inline_default(42)]
value: u32
}
```
> [!IMPORTANT]
> **`#[serde_inline_default]` must be set before `#[derive(Deserialize)]`/`#[derive(Serialize)]`!**
Internally, `#[serde_inline_default(...)]` gets expanded to a function which returns the set value and the attribute is replaced with `#[serde(default = "")]`.
So this macro is just some syntax sugar for you, but can get quite handy if you want to keep your code clean or write declarative macros / `macro_rules!`.
## Alternatives
This crate isn't perfect. Thus, you might be more satisfied with alternatives `serde` provides.
With `#[serde(default)]` + `impl Default` on a struct, `serde` uses the default implementation of the struct to get default values for each field ([docs](https://serde.rs/container-attrs.html#default)):
```rust
#[derive(Deserialize)]
#[serde(default)]
struct Test {
value: u32
}
impl Default for Test {
fn default() -> Self {
Self {
value: 42
}
}
}
```
If you still need/want `serde-inline-default` features, you also can combine them with `#[serde(default))` and `impl Default`:
```rust
#[serde_inline_default]
#[derive(Deserialize)]
#[serde(default)]
struct Test {
value: u32,
#[serde_inline_default(0)]
other_value: u32,
}
impl Default for Test {
fn default() -> Self {
Self {
value: 42,
other_value: 42
}
}
}
```
## License
This project is licensed under either of the following licenses, at your option:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or )
- MIT License ([LICENSE-MIT](LICENSE-MIT) or )