Skip to content

Help serializing empty attributes #252

@thebeekeeper

Description

@thebeekeeper

I have an externally defined XML standard I'm trying to write to which includes requiring empty attribute values. As an example, I need to write the following:

<TIM A="" D="4">

Where my A attribute needs to be written with an empty value, and D needs a constant value. Is it possible to achieve this with the crate using the serde serialize feature? Ideally, I'd like to use an Option like this:

#[derive(Serialize, Debug)]
struct Tim {
   a: Option<String>,
   b: u8,
}

But when I do that, and set a to None, the A attribute isn't serialized at all. I'm not sure that the crate would be able to decide whether or not to serialize the attribute, so I'm guessing I'll need serialize_with. Also tried that, but haven't been able to get it to write out an empty attribute value. The closest I've gotten is writing out <TIM A=" " D="4"/> but in my case, the space in the A attribute value is invalid.

Metadata

Metadata

Assignees

Labels

bugserdeIssues related to mapping from Rust types to XML

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions