commit | 4aff12b616d5a9a887981975449bed61ccfef50d | [log] [tgz] |
---|---|---|
author | Joel Galenson <jgalenson@google.com> | Mon Aug 30 21:30:07 2021 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Mon Aug 30 21:30:07 2021 +0000 |
tree | 488d9ed268b10a9998c2d55a17ab1e3e26e78cec | |
parent | de99e8ee4d7b2fa148d992f0c9dc799f7a8726ba [diff] | |
parent | 26f4ca693bd90b41469c88daf2a8f23281680ea8 [diff] |
Update TEST_MAPPING am: 9ca936cae1 am: 377bc4d28d am: 26f4ca693b Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/serde-xml-rs/+/1813978 Change-Id: I72a19a60dfa810ef189d1b5223ef11335264a919
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.