serde_with_macros v2.1.0
github-actions
released this
16 Nov 22:19
·
459 commits
to master
since this release
Added
-
Add new
apply
attribute to simplify repetitive attributes over many fields.
Multiple rules and multiple attributes can be provided each.#[serde_with::apply( Option => #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")], Option<bool> => #[serde(rename = "bool")], )] #[derive(serde::Serialize)] struct Data { a: Option<String>, b: Option<u64>, c: Option<String>, d: Option<bool>, }
The
apply
attribute will expand into this, applying the attributs to the matching fields:#[derive(serde::Serialize)] struct Data { #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] a: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] b: Option<u64>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] c: Option<String>, #[serde(default)] #[serde(skip_serializing_if = "Option::is_none")] #[serde(rename = "bool")] d: Option<bool>, }
The attribute supports field matching using many rules, such as
_
to apply to all fields and partial generics likeOption
to match anyOption
be itOption<String>
,Option<bool>
, orOption<T>
.