More explicit XML Serialization and Deserialization
This crate uses quick-xml
as XML parser and writer.
Most code of this crate is based on quick-xml
and serde-xml-rs
. The main difference is that attributes are explicitly annotated (name starts with @
):
#[derive(Serialize, Deserialize)]
struct Struct {
#[serde(rename = "@attribute")]
attribute: String,
element: String,
}
<root attribute="attribute content">
<element>element content</element>
</root>
It is therefore not a drop-in replacement for quick-xml
or serde-xml-rs
.
Other differences:
$value
works for serialisation- Only
true
,false
,1
,0
accepted for boolean - Offset information for deserialization errors
Also look at the serde
examples for inspiration: https://serde.rs/examples.html
#[derive(Serialize, Deserialize)]
struct Struct {
#[serde(rename = "@string")]
string: String,
#[serde(rename = "@maybe_string")]
maybe_string: Option<String>, // will be omitted when None
}
<root string="attribute content"/>
#[derive(Serialize, Deserialize)]
struct Struct {
#[serde(rename = "string")]
strings: Vec<String>,
}
<root>
<string>one</string>
<string>two</string>
</root>
Use the special $value
as field name the model the inner value of an element:
#[derive(Serialize, Deserialize)]
struct Struct {
#[serde(rename = "@attr")]
attr: String,
#[serde(rename = "$value")]
value: String,
}
<root attr="">inner value</root>
To model a string with only a strict set of values use an enum with the inner value ($value
) as tag.
For the example for the values one
, two
, three
:
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "lowercase", tag = "$value")]
enum Enum {
One,
Two,
Three,
}
<root>one</root>
- Panic free