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

Serialization of Option<String> = None, old behaviour still possible? #536

Closed
ghost opened this issue Jan 4, 2023 · 1 comment
Closed
Labels
question serde Issues related to mapping from Rust types to XML

Comments

@ghost
Copy link

ghost commented Jan 4, 2023

According to #252 (fixed by #490), None values for attributes that are Option<String> now serialize to Some(""), whereas the old behaviour was to not serialize at all. I have several XML specs where the attribute must have a value if present or not be present at all. Is this no longer possible?

In my case I get lots of optional_attr="" that no longer validate and create an excessive amount of (useless) data. Do I have to write my own serialization for this from now on?

I have ensured the attributes really do deserialize to None, before serializing back.

Sorry I guess there's always that someone who needs the opposite...

I'm using:

serde = { version = "1.0.152", features = [ "derive" ] }
quick-xml = { version = "0.27.1", features = [ "serialize" ] }
@Mingun Mingun added question serde Issues related to mapping from Rust types to XML labels Jan 4, 2023
@Mingun
Copy link
Collaborator

Mingun commented Jan 4, 2023

According to #252 (fixed by #490), None values for attributes that are Option<String> now serialize to Some("")

You mean, that Option<String>::None is serialized as some_attribute="". Yes, this is now the behaviour by default, which mathes the behaviour of serde_json and serde_yaml. To not write empty attributes annotate your fields with #[serde(skip_serializing_if = "Option::is_none")] as written in the serde examples.

@Mingun Mingun closed this as completed Jan 4, 2023
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

1 participant