Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: Add new field attribute "skip_serializing_when" #2762

Open
hxzhao527 opened this issue Jun 24, 2024 · 1 comment
Open

FR: Add new field attribute "skip_serializing_when" #2762

hxzhao527 opened this issue Jun 24, 2024 · 1 comment

Comments

@hxzhao527
Copy link

hxzhao527 commented Jun 24, 2024

similar as #818.
but this allows different values for the same type.
for example

#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct TCP {
    #[serde(skip_serializing_when = true)]
    no_delay: bool,
    #[serde(skip_serializing_when = true)]
    keep_alive: bool,
    #[serde(skip_serializing_when = false)]
    prefer_ipv4: bool,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Mux {
    enabled: bool,
    #[serde(skip_serializing_when = 8)]
    concurrency: u32,
    #[serde(skip_serializing_if = 60)]
    idle_timeout: u64,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Router {
    enabled: bool,
    bypass: Vec<String>,
    proxy: Vec<String>,
    block: Vec<String>,
    #[serde(skip_serializing_when = "proxy".to_string())]
    default_policy: String,
    #[serde(skip_serializing_when = "as_is".to_string())]
    domain_strategy: String,
    geoip: String,
    geosite: String,
}
@hxzhao527 hxzhao527 changed the title Add new field attribute "skip_serializing_when" FR: Add new field attribute "skip_serializing_when" Jun 24, 2024
@jberryman
Copy link

This is nice, because currently serde has no happy path for users who want to skip serializing certain values (to save bandwidth, say) and also want Serialize/Deserialize to be a correct-by-construction roundtrip (I don't want to write unit tests for this, though serde offers enough footguns that maybe its necessary anyway).

If there were an attribute based around Default that skipped serializing that would be okay too, but I would rather just annotate specific fields like in the example above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants