serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.06k stars 767 forks source link

Support adding prefix/suffix to flattened fields #2738

Closed orclev closed 4 months ago

orclev commented 4 months ago

In Java the Jackson serialization library has a feature similar to flatten which is the @JsonUnwrapped annotation. Unlike flatten however it supports a pair of optional parameters prefix and suffix which are arbitrary String values that will be prepended or appended respectively to the flattened fields names. This is very useful when you have multiple instances of some struct that you want to flatten but you want to avoid name collisions. It would be great if serde could support a similar prefix and suffix argument to flatten.

dtolnay commented 4 months ago

Prefix is supported by using https://docs.rs/serde_with/3.8.1/serde_with/macro.with_prefix.html.

Suffix was previously requested in https://github.com/jonasbb/serde_with/issues/381 but there doesn't seem to be demand.

orclev commented 4 months ago

Ah nice, I had looked through the docs for flatten and didn't see any way to add a prefix, I didn't realize it was an entirely different attribute. Thanks.