commit | 9ca936cae1e88464dfb3284b4eeba4c284c25ec4 | [log] [tgz] |
---|---|---|
author | Joel Galenson <jgalenson@google.com> | Mon Aug 30 10:33:00 2021 -0700 |
committer | Joel Galenson <jgalenson@google.com> | Mon Aug 30 10:33:00 2021 -0700 |
tree | 488d9ed268b10a9998c2d55a17ab1e3e26e78cec | |
parent | 98adb003bfd7720d69e119d0ae1a601b7b0f7561 [diff] |
Update TEST_MAPPING Test: None Change-Id: Iaa83e018cabf68aa65d657035d02c3c8640295da
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.