Skip to content

serde_with_macros v2.1.0

Compare
Choose a tag to compare
@github-actions github-actions released this 16 Nov 22:19
· 459 commits to master since this release
8aaacb6

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 like Option to match any Option be it Option<String>, Option<bool>, or Option<T>.

Fixed

  • The derive macros SerializeDisplay and DeserializeFromStr now take better care not to use conflicting names for generic values. (#526)
    All used generics now start with __ to make conflicts with manually written code unlikely.

    Thanks to @Elrendio for submitting a PR fixing the issue.