serde-rs / serde

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

Differentiated Serialization Formats for Structs #2766

Open XuJiandong opened 3 days ago

XuJiandong commented 3 days ago

Problem Statement

I have the following struct definitions:

#[derive(Serialize, Deserialize)]
struct TypeA {
    // ...
}

#[derive(Serialize, Deserialize)]
#[special_compact]
struct TypeB {
    // ...
}

The goal is to serialize TypeA and TypeB into different formats. Specifically, TypeB should be serialized into a more compact format than TypeA.

Expected Behavior

Actual Behavior

Currently, both TypeA and TypeB are serialized using the same format, and there doesn't seem to be an out-of-the-box solution for differentiating their serialization formats.

Request for Solution

Is there a way to achieve this within the current framework of serde? If not, could you provide guidance or a potential feature enhancement to support this functionality?

Thank you!