#serde #xml #serialization #roxml-tree

serde-roxmltree

Convert roxmltree documents into Serde-compatible types

13 unstable releases

0.7.0 Feb 10, 2024
0.6.1 Aug 15, 2023
0.6.0 Feb 4, 2023
0.4.0 Nov 21, 2022

#495 in Encoding

Download history 1/week @ 2024-02-11 18/week @ 2024-02-18 38/week @ 2024-02-25 28/week @ 2024-03-03 305/week @ 2024-03-10 29/week @ 2024-03-17 10/week @ 2024-03-24 41/week @ 2024-03-31 1/week @ 2024-04-07 7/week @ 2024-04-14

61 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

27KB
652 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 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");
#

Borrowing types 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");
#

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);
#

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"]);
#

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));
#

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);
#

Dependencies

~270–520KB
~12K SLoC