#serde #xml #serialization #roxmltree

serde-roxmltree

Convert roxmltree documents into Serde-compatible types

10 releases (4 breaking)

0.5.0 Jan 6, 2023
0.4.0 Nov 21, 2022
0.3.4 Oct 31, 2022
0.3.2 Aug 21, 2022
0.1.1 Aug 11, 2022

#652 in Encoding

Download history 34/week @ 2022-10-08 25/week @ 2022-10-15 109/week @ 2022-10-22 74/week @ 2022-10-29 42/week @ 2022-11-05 14/week @ 2022-11-12 47/week @ 2022-11-19 29/week @ 2022-11-26 11/week @ 2022-12-03 18/week @ 2022-12-10 17/week @ 2022-12-17 2/week @ 2022-12-24 15/week @ 2022-12-31 35/week @ 2023-01-07 22/week @ 2023-01-14 11/week @ 2023-01-21

83 downloads per month

MIT/Apache

26KB
646 lines

serde-roxmltree

crates.io docs.rs github.com

Convert roxmltree documents into Serde-compatible types

License

Licensed under

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Convert [roxmltree] documents into [serde]-compatible types

[Owned types][de::DeserializeOwned] can be deserialized directly from XML text using [from_str]:

use serde::Deserialize;
use serde_roxmltree::from_str;

#[derive(Deserialize)]
struct Record {
    field: String,
}

let record = from_str::<Record>("<record><field>foobar</field></record>")?;
assert_eq!(record.field, "foobar");
#
# Ok::<(), Box<dyn std::error::Error>>(())

[Borrowing types][de::Deserialize] must be deserialized from a [Document] using [from_doc]:

use roxmltree::Document;
use serde::Deserialize;
use serde_roxmltree::from_doc;

#[derive(Deserialize)]
struct Record<'a> {
    field: &'a str,
}

let document = Document::parse("<document><field>foobar</field></document>")?;

let record = from_doc::<Record>(&document)?;
assert_eq!(record.field, "foobar");
#
# Ok::<(), Box<dyn std::error::Error>>(())

Fields of structures map to child elements and attributes:

use serde::Deserialize;
use serde_roxmltree::from_str;

#[derive(Deserialize)]
struct Record {
    child: String,
    attribute: i32,
}

let record = from_str::<Record>(r#"<record attribute="42"><child>foobar</child></record>"#)?;
assert_eq!(record.child, "foobar");
assert_eq!(record.attribute, 42);
#
# Ok::<(), Box<dyn std::error::Error>>(())

Sequences collect repeated child elements:

use serde::Deserialize;
use serde_roxmltree::from_str;

#[derive(Deserialize)]
struct Record {
    field: Vec<String>,
}

let record = from_str::<Record>("<record><field>foo</field><field>bar</field></record>")?;
assert_eq!(record.field, ["foo", "bar"]);
#
# Ok::<(), Box<dyn std::error::Error>>(())

Enum variants describe alternatives:

use serde::Deserialize;
use serde_roxmltree::from_str;

#[derive(Debug, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
enum Record {
    Float(f32),
    Integer(i32),
}

let record = from_str::<Record>("<record><float>42.0</float></record>")?;
assert_eq!(record, Record::Float(42.0));

let record = from_str::<Record>("<record><integer>23</integer></record>")?;
assert_eq!(record, Record::Integer(23));
#
# Ok::<(), Box<dyn std::error::Error>>(())

The reserved name $text is used to directly refer to the text within an element:

use serde::Deserialize;
use serde_roxmltree::from_str;

#[derive(Deserialize)]
struct Record {
    child: Child,
}

#[derive(Deserialize)]
struct Child {
    #[serde(rename = "$text")]
    text: String,
    attribute: i32,
}

let record = from_str::<Record>(r#"<record><child attribute="42">foobar</child></record>"#)?;
assert_eq!(record.child.text, "foobar");
assert_eq!(record.child.attribute, 42);
#
# Ok::<(), Box<dyn std::error::Error>>(())

Dependencies

~295–420KB