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 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:
you can just do this:
[!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 = "<function name>")]
.
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):
If you still need/want serde-inline-default
features, you also can combine them with #[serde(default))
and impl Default
:
License
This project is licensed under either of the following licenses, at your option:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)