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

Renaming unnamed field of internally tagged enum #2838

Open
Jobarion opened this issue Oct 21, 2024 · 0 comments
Open

Renaming unnamed field of internally tagged enum #2838

Jobarion opened this issue Oct 21, 2024 · 0 comments

Comments

@Jobarion
Copy link

The #[serde(rename)] attribute does not seem to work on unnamed fields of internally tagged enums.

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Test {
    A(#[serde(rename = "x")] u8),
    B(#[serde(rename = "x")] u8),
}

The struct above fails to serialize: "cannot serialize tagged newtype variant Test::A containing an integer", line: 0, column: 0, I would have expected it to serialize to {"type": "A", "x": 123}. Deserialization fails similarly.

Changing it to a struct with named fields works fine:

#[derive(Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Test {
    A{x: u8},
    B{x: u8},
}

Versions

serde: 1.0.210
serde_json: 1.0.132
rust: 1.81.0

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

1 participant