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

How to serializing Option<String> to self close element with #[serde(rename = "$unflatten={}")] #518

Closed
LokiSharp opened this issue Dec 2, 2022 · 1 comment
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@LokiSharp
Copy link

LokiSharp commented Dec 2, 2022

I want to serialize a Option<String> to unflatten self close element like <Foo><option-with-none-value/></Foo>. Is there any way to control it?

Code:

#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Foo {
    #[serde(rename = "$unflatten=option-with-none-value")]
    pub body: Option<String>,
}

to_string(&Foo { body: None }).unwrap()

Result:

<Foo><option-with-none-value></option-with-none-value></Foo>
@Mingun
Copy link
Collaborator

Mingun commented Dec 2, 2022

In current master this is how it works out-of-box since #490 is merged:

#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub struct Foo {
    #[serde(rename = "option-with-none-value")]
    pub body: Option<String>,
}

assert_eq!(
    to_string(&Foo { body: None }).unwrap(),
    "<Foo><option-with-none-value/></Foo>"
);

But deserialization of that value will return Some("") for now due to #497.

@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question serde Issues related to mapping from Rust types to XML
Projects
None yet
Development

No branches or pull requests

2 participants