commit | 98adb003bfd7720d69e119d0ae1a601b7b0f7561 | [log] [tgz] |
---|---|---|
author | Bob Badour <bbadour@google.com> | Mon Aug 02 13:02:27 2021 -0700 |
committer | Bob Badour <bbadour@google.com> | Mon Aug 02 13:02:27 2021 -0700 |
tree | 765cec3e43132e50b6918fae9d51e2001174e69d | |
parent | d34029dbb9322ccd5d74cb6d7df86cacbf882f8f [diff] |
[LSC] Add LOCAL_LICENSE_KINDS to external/rust/crates/serde-xml-rs Added SPDX-license-identifier-MIT to: Android.bp Bug: 68860345 Bug: 151177513 Bug: 151953481 Test: m all Exempt-From-Owner-Approval: janitorial work Change-Id: I5ba730e16d6ac79580cf83aa1cde69fb60303c3c
xml-rs based deserializer for Serde (compatible with 0.9+)
Use serde_xml_rs::from_reader(...)
on any type that implements std::io::Read
as following:
#[macro_use] extern crate serde_derive; extern crate serde; extern crate serde_xml_rs; use serde_xml_rs::from_reader; #[derive(Debug, Deserialize)] struct Item { pub name: String, pub source: String } #[derive(Debug, Deserialize)] struct Project { pub name: String, #[serde(rename = "Item", default)] pub items: Vec<Item> } fn main() { let s = r##" <Project name="my_project"> <Item name="hello" source="world.rs" /> </Project> "##; let project: Project = from_reader(s.as_bytes()).unwrap(); println!("{:#?}", project); }
Alternatively, you can use serde_xml_rs::Deserializer
to create a deserializer from a preconfigured xml_rs::EventReader
.
If you have an input of the form <foo abc="xyz">bar</foo>
, and you want to get at thebar
, you can use the special name $value
:
struct Foo { pub abc: String, #[serde(rename = "$value")] pub body: String, }
Deserializer tries to be as intuitive as possible.
However, there are some edge cases where you might get unexpected errors, so it's best to check out tests
for expectations.