Crate serde_flat_path

source ·
Expand description

flat_path can be applied to any named field within a struct or enum. The #[flat_path] attribute must be applied before the serialize/deserialize #[derive(...)] so that it can apply the necessary serde attributes before serde performs macro expansion for its derive macros. Similar to derive macros, the original type is not altered.

For cases where field names contain . or additional verbosity is desired, #[flat_path("a.b.c")] may also be written as #[flat_path(path = ["a", "b", "c"])]. These two forms are equivalent and no distinction is made between regarding macro expansion.

#[serde(...)] attributes are also moved from #[flat_path(...)] fields to the final field within the path. During this process, the order of attributes remains the same.

#[flat_path]
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)]
#[serde(default)]
pub struct Foo {
    foo: bool,
    #[flat_path(path=["a", "b", "c"])]
    #[serde(skip_serializing_if="Option::is_none")]
    x: Option<u64>,
    #[serde(rename="INDEX")]
    index_number: u32,
}

For more examples see the repository’s tests folder.

Attribute Macros