serde-xml-rs: 在序列化时出现LastElementNameNotAvailable错误。

huangapple go评论138阅读模式
英文:

serde-xml-rs: LastElementNameNotAvailable when serializing

问题

在我的项目中,我遇到了一个错误,其中我使用 serde-xml-rs 解析和序列化 XML。

Cargo.toml

  1. [package]
  2. name = "serde_xml_rs_serialization_error"
  3. version = "0.1.0"
  4. edition = "2021"
  5. # 更多键及其定义,请参见 https://doc.rust-lang.org/cargo/reference/manifest.html
  6. [dependencies]
  7. serde = { version = "1.0.164", features = ["derive"] }
  8. serde-xml-rs = "0.6.0"

src/main.rs

  1. use serde::{Deserialize, Serialize};
  2. #[derive(Clone, Debug, Deserialize, Serialize)]
  3. struct Types {
  4. #[serde(rename = "type")]
  5. types: Vec<Type>,
  6. }
  7. #[derive(Clone, Debug, Deserialize, Serialize)]
  8. struct Type {
  9. name: String,
  10. }
  11. const XML: &str = r#"<types>
  12. <type name="first"/>
  13. <type name="second"/>
  14. </types>"#;
  15. fn main() {
  16. let types: Types = serde_xml_rs::from_str(XML).expect("无法解析 XML");
  17. dbg!(&types);
  18. match serde_xml_rs::to_string(&types) {
  19. Ok(xml) => {
  20. dbg!(xml);
  21. }
  22. Err(error) => {
  23. dbg!(error);
  24. }
  25. };
  26. }

我期望得到一个序列化的 XML 字符串,但实际上它产生了一个错误:

  1. cargo run
  2. Finished dev [unoptimized + debuginfo] target(s) in 0.05s
  3. Running `target\debug\serde_xml_rs_serialization_error.exe`
  4. [src\main.rs:21] &types = Types {
  5. types: [
  6. Type {
  7. name: "first",
  8. },
  9. Type {
  10. name: "second",
  11. },
  12. ],
  13. }
  14. [src\main.rs:27] error = Writer {
  15. source: LastElementNameNotAvailable,
  16. }

我在这里做错了什么?
1: https://docs.rs/serde-xml-rs/latest/serde_xml_rs/

英文:

So I encountered this error in one of my projects where I parse and serialize XML using serde-xml-rs.

Cargo.toml

  1. [package]
  2. name = "serde_xml_rs_serialization_error"
  3. version = "0.1.0"
  4. edition = "2021"
  5. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  6. [dependencies]
  7. serde = { version = "1.0.164", features = ["derive"] }
  8. serde-xml-rs = "0.6.0"

src/main.rs

  1. use serde::{Deserialize, Serialize};
  2. #[derive(Clone, Debug, Deserialize, Serialize)]
  3. struct Types {
  4. #[serde(rename = "type")]
  5. types: Vec<Type>,
  6. }
  7. #[derive(Clone, Debug, Deserialize, Serialize)]
  8. struct Type {
  9. name: String,
  10. }
  11. const XML: &str = r#"<types>
  12. <type name="first"/>
  13. <type name="second"/>
  14. </types>"#;
  15. fn main() {
  16. let types: Types = serde_xml_rs::from_str(XML).expect("Cannot parse XML.");
  17. dbg!(&types);
  18. match serde_xml_rs::to_string(&types) {
  19. Ok(xml) => {
  20. dbg!(xml);
  21. }
  22. Err(error) => {
  23. dbg!(error);
  24. }
  25. };
  26. }

I expected a serialized XML string, but instead it produces an error:

  1. cargo run
  2. Finished dev [unoptimized + debuginfo] target(s) in 0.05s
  3. Running `target\debug\serde_xml_rs_serialization_error.exe`
  4. [src\main.rs:21] &types = Types {
  5. types: [
  6. Type {
  7. name: "first",
  8. },
  9. Type {
  10. name: "second",
  11. },
  12. ],
  13. }
  14. [src\main.rs:27] error = Writer {
  15. source: LastElementNameNotAvailable,
  16. }

What am I doing wrong here?

答案1

得分: 0

这是一个关于serde-xml-rs问题,让我浪费了半天的时间。

使用 quick-xml 替代 serde-xml-rs 解决了这个问题:

Cargo.toml

  1. [package]
  2. name = "serde_xml_rs_serialization_error"
  3. version = "0.1.0"
  4. edition = "2021"
  5. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  6. [dependencies]
  7. serde = { version = "1.0.164", features = ["derive"] }
  8. quick-xml = { version = "0.28.2", features = ["serialize"] }

src/main.rs

  1. use serde::{Deserialize, Serialize};
  2. #[derive(Clone, Debug, Deserialize, Serialize)]
  3. struct Types {
  4. #[serde(rename = "type")]
  5. types: Vec<Type>,
  6. }
  7. #[derive(Clone, Debug, Deserialize, Serialize)]
  8. struct Type {
  9. #[serde(rename = "@name")]
  10. name: String,
  11. }
  12. const XML: &str = r#"<types>
  13. <type name="first"/>
  14. <type name="second"/>
  15. </types>"#;
  16. fn main() {
  17. let types: Types = quick_xml::de::from_str(XML).expect("Cannot parse XML.");
  18. dbg!(&types);
  19. match quick_xml::se::to_string(&types) {
  20. Ok(xml) => {
  21. dbg!(xml);
  22. }
  23. Err(error) => {
  24. dbg!(error);
  25. }
  26. };
  27. }
英文:

It's a bug in serde-xml-rs, that cost me half a day.

Using quick-xml instead of serde-xml-rs solved the issue:

Cargo.toml

  1. [package]
  2. name = &quot;serde_xml_rs_serialization_error&quot;
  3. version = &quot;0.1.0&quot;
  4. edition = &quot;2021&quot;
  5. # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
  6. [dependencies]
  7. serde = { version = &quot;1.0.164&quot;, features = [&quot;derive&quot;] }
  8. quick-xml = { version = &quot;0.28.2&quot;, features = [&quot;serialize&quot;] }

src/main.rs

  1. use serde::{Deserialize, Serialize};
  2. #[derive(Clone, Debug, Deserialize, Serialize)]
  3. struct Types {
  4. #[serde(rename = &quot;type&quot;)]
  5. types: Vec&lt;Type&gt;,
  6. }
  7. #[derive(Clone, Debug, Deserialize, Serialize)]
  8. struct Type {
  9. #[serde(rename = &quot;@name&quot;)]
  10. name: String,
  11. }
  12. const XML: &amp;str = r#&quot;&lt;types&gt;
  13. &lt;type name=&quot;first&quot;/&gt;
  14. &lt;type name=&quot;second&quot;/&gt;
  15. &lt;/types&gt;&quot;#;
  16. fn main() {
  17. let types: Types = quick_xml::de::from_str(XML).expect(&quot;Cannot parse XML.&quot;);
  18. dbg!(&amp;types);
  19. match quick_xml::se::to_string(&amp;types) {
  20. Ok(xml) =&gt; {
  21. dbg!(xml);
  22. }
  23. Err(error) =&gt; {
  24. dbg!(error);
  25. }
  26. };
  27. }

huangapple
  • 本文由 发表于 2023年6月22日 03:37:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76526592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定