#serde #xml #serialization #roxml-tree

serde-roxmltree

将 roxmltree 文档转换为 Serde 兼容类型

19 个版本

0.8.3 2024 年 7 月 20 日
0.8.1 2024 年 5 月 26 日
0.7.0 2024 年 2 月 10 日
0.6.1 2023 年 8 月 15 日
0.4.0 2022 年 11 月 21 日

编码 中排名 359

Download history 13/week @ 2024-04-29 263/week @ 2024-05-13 218/week @ 2024-05-20 104/week @ 2024-05-27 39/week @ 2024-06-03 47/week @ 2024-06-10 59/week @ 2024-06-17 155/week @ 2024-06-24 286/week @ 2024-07-01 227/week @ 2024-07-08 267/week @ 2024-07-15 81/week @ 2024-07-22 132/week @ 2024-07-29 97/week @ 2024-08-05 96/week @ 2024-08-12

每月下载量 427
用于 3crate(直接使用 2 个)

MIT/Apache

40KB
962 行(不包括注释)

serde-roxmltree

crates.io docs.rs github.com

roxmltree 文档转换为 Serde 兼容类型

许可

许可下

根据您的选择。

贡献

除非您明确声明,否则根据 Apache-2.0 许可证定义的您有意提交的任何贡献,将如上双许可,不附加任何额外条款或条件。


lib.rs:

roxmltree 文档转换为 serde 兼容类型

拥有类型可以直接使用 from_str 从 XML 文本反序列化

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

借用类型必须使用 from_docDocument 反序列化

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

结构体的字段映射到子元素和属性

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

序列收集重复的子元素

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

枚举变体描述替代项

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

保留名称 $text 用于直接引用元素内的文本

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

可选地,可以通过在属性名称前加上 @ 来区分它们与标签名称

use serde::Deserialize;
use serde_roxmltree::{defaults, from_str, Options};

#[derive(Deserialize)]
struct Record {
    child: String,
    #[serde(rename = "@attribute")]
    attribute: i32,
}

let record = defaults().prefix_attr().from_str::<Record>(r#"<record attribute="42"><child>foobar</child></record>"#)?;
assert_eq!(record.child, "foobar");
assert_eq!(record.attribute, 42);
#

可以通过 namespaces 选项启用对 命名空间 的支持

use serde::Deserialize;
use serde_roxmltree::{defaults, from_str, Options};

let text = r#"<record xmlns:foo="http://foo" xmlns:bar="http://bar">
    <foo:qux>23</foo:qux>
    <bar:qux>42</bar:qux>
</record>"#;

#[derive(Deserialize)]
struct SomeRecord {
    qux: Vec<i32>,
}

let record = from_str::<SomeRecord>(text)?;
assert_eq!(record.qux, [23, 42]);

#[derive(Deserialize)]
struct AnotherRecord {
    #[serde(rename = "{http://foo}qux")]
    some_qux: i32,
    #[serde(rename = "{http://bar}qux")]
    another_qux: i32,
}

let record = defaults().namespaces().from_str::<AnotherRecord>(text)?;
assert_eq!(record.some_qux, 23);
assert_eq!(record.another_qux, 42);
#

依赖

~425–660KB
~14K SLoC