-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deserialize directly to vec #121
base: master
Are you sure you want to change the base?
Conversation
The travis CI build failed on my fork too, I had to clear cache in travis to get past this |
Travis error details:
|
@mward, I keep seeing this problem and another one that is similar. As you say, clearing the cache works around the problem. I'd love to find a permanent fix that can be automated. A few months back, I noticed that the option to clear the cache (https://docs.travis-ci.com/user/caching/#clearing-caches) is no longer available on travis-ci.org. |
@mward are you able to rebase/ merge master so we can see if the CI change I made fixes anything 😄 ? |
@efx I merged upstream master to my forked project. Thanks |
Do we have a schedule for merging this fix ? // ----------------------------------------------------
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all(deserialize = "lowercase"))]
pub struct LookupEntrySingle {
#[serde(rename = "state")]
lkup_state: String,
#[serde(rename = "value")]
lookup_value: u64,
#[serde(rename = "$value", default)]
lkup_label: String,
}
#[derive(Debug, Deserialize, PartialEq)]
#[serde(rename_all(deserialize = "lowercase"))]
pub enum LookupEntry{
#[serde(rename = "SingleInt")]
SingleInt(LookupEntrySingle),
#[serde(rename = "Boolean")]
Boolean(LookupEntrySingle),
#[serde(rename = "BitField")]
BitField(LookupEntrySingle),
}
#[derive(Debug, Deserialize, PartialEq)]
pub struct ValueLookup {
id: String,
#[serde(rename = "desiredValue", default)]
desiredvalue: u64,
#[serde(rename = "undefinedState", default)]
undefinedstate: Option<String>,
// #[serde(rename = "Lookups", default)]
#[serde(rename = "$value")]
lookups: Vec<LookupEntry>,
}
static LOOKUP_STD: &'static str = r##"<?xml version="1.0" encoding="UTF-8"?>
<ValueLookup id="int" desiredValue="1" >
<Lookups>
<SingleInt state="None" value="0" >Unknown</SingleInt>
<SingleInt state="Ok" value="1" >OK</SingleInt>
</Lookups>
</ValueLookup>
"##;
#[test]
fn test_lookupInt_JSON() {
/* This one is returns one element of the vec, only the last one
[
Object(
{
"Lookups": Object(
{
"SingleInt": Object(
{
"$value": String(
"OK",
),
"state": String(
"Ok",
),
"value": String(
"1",
),
},
),
},
),
"desiredValue": String(
"1",
),
"id": String(
"prtgc.std.lookups.status",
),
},
),
]
*/
let _ = simple_logger::init_with_level(log::Level::Debug);
let json_values: ValueLookup =
match serde_xml_rs::from_str(LOOKUP_STD) {
Ok(val) => val,
Err(err) => {
println!("{:#?}", err);
panic!(err);
},
};
println!("{:#?}", json_values);
}
#[test]
fn test_lookupInt(){
// This one works
let _ = simple_logger::init_with_level(Level::Debug);
let json_values: Vec<serde_json::Value> =
match serde_xml_rs::from_str(LOOKUP_STD) {
Ok(val) => val,
Err(err) => {
println!("{:#?}", err);
panic!(err);
},
};
println!("{:#?}", json_values);
}
#[test]
fn test_lookupdange() {
// This one does not work
let _ = simple_logger::init_with_level(log::Level::Debug);
let s = r##"<?xml version="1.0" encoding="UTF-8"?>
<ValueLookup id="range" desiredValue="1" >
<Lookups>
<Range From="100" To="199" state="Ok">Information</Range>
<Range From="200" To="299" state="Ok">Success</Range>
<Range From="300" To="399" state="Warning">Redirection</Range>
<Range From="400" To="499" state="Error">Client Error</Range>
<Range From="500" To="599" state="Error">Server Error</Range>
</Lookups>
</ValueLookup>
"##;
let json_values: ValueLookup =
match serde_xml_rs::from_str(s) {
Ok(val) => val,
Err(err) => {
println!("{:#?}", err);
panic!(err);
},
};
println!("{:#?}", json_values);
}
#[test]
fn test_lookupBit() {
// This one does not work
let _ = simple_logger::init_with_level(log::Level::Debug);
let s = r##"<?xml version="1.0" encoding="UTF-8"?>
<ValueLookup id="status" desiredValue="1" >
<Lookups>
<BitField state="OK" value="0" BitNumber="1">No Alarm</BitField>
<BitField state="Warning" value="1" BitNumber="1">
Alarm Active
</BitField>
</Lookups>
</ValueLookup>
"##;
let json_values: ValueLookup =
match serde_xml_rs::from_str(s) {
Ok(val) => val,
Err(err) => {
println!("{:#?}", err);
panic!(err);
},
};
println!("{:#?}", json_values);
} |
I've been experimenting with this and it seems to not behave properly when the considered The following test (built similarly to your new /** For reference */
#[derive(Debug, Deserialize, PartialEq)]
struct Shelter {
animals: Vec<Animal>,
}
#[derive(Debug, Deserialize, PartialEq)]
struct Animal {
name: String,
foo: String,
}
/********************/
#[test]
fn empty_vec_container() {
let _ = simple_logger::init();
let s = r##"
<shelter>
<animals>
</animals>
</shelter>
"##;
let shelter: Shelter = from_str(s).unwrap();
assert_eq!(shelter.animals, Vec::new());
}
Other than that it's very practical! |
@Ten0 I have a fix for the empty_vec_container on my branch. Before fixing that I realized that the test "test_doctype_fail" was failing for some unknown reason. That test also fails when I run directly from "RReverser/serde-xml-rs" master. I temporarily ignored that test on my branch |
I removed ignore from 'test_doctype_fail' test. Locking |
I don't beleive pinning these crates to old versions is the right approach, I think it's better to rather fix the reason why it's failing in either the dependencies or this test (depending on which one is the cause). Besides, as you pointed out it's unrelated to this PR, and I had already opened an issue to track this: #124 |
@@ -1,6 +1,6 @@ | |||
# serde-xml-rs | |||
|
|||
[![Build Status](https://travis-ci.org/RReverser/serde-xml-rs.svg?branch=master)](https://travis-ci.org/RReverser/serde-xml-rs) | |||
[![Build Status](https://travis-ci.org/mward/serde-xml-rs.svg?branch=master)](https://travis-ci.org/mward/serde-xml-rs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be included as part of this PR?
Is there anything I can do to help get this merged? This PR enables me to remove a lot of ugly container structs from my code base which has to support parsing very large and poorly designed XML document. |
…() from failing" This reverts commit 2014238.
@steven-joruk - I'd like to see this get merged too, but in the meantime I have my
BTW - I've merged master onto my fork and the CI build is still passing |
This might be my misunderstanding, but I'm seeing my "nested" example fail where I would expect it to succeed. This is pretty much the nested_collection unit test within a container deserialized in to a vec. use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Root {
#[serde(rename = "Groups")]
groups: Vec<Group>,
}
#[derive(Debug, Deserialize)]
struct Group {
#[serde(rename = "Item")]
items: Vec<Item>,
}
#[derive(Debug, Deserialize)]
struct Item {}
fn main() {
let not_nested = "
<Root>
<Groups>
<Group>
</Group>
</Groups>
</Root>
";
let root: Root = serde_xml_rs::from_str(not_nested).unwrap();
println!("{:#?}", root);
let nested = "
<Root>
<Groups>
<Group>
<Item/>
<Item/>
</Group>
</Groups>
</Root>
";
let root: Root = serde_xml_rs::from_str(nested).unwrap();
println!("{:#?}", root);
} Output:
|
Bump |
Been parsing though this PR to try and understand it and apply it to the latest version of this repo. i was able to cherry pick only commits 42b5c44 and 2e4d9f0 and have things work the same (as far as my limited testing) as if i had included this whole PR. In case I don't end up figuring out how this PR works and fixing the issue that @steven-joruk and I are seeing, I hope this information at least helps someone else. |
Updated to allow deserializing directly to a Vec, without need to define an intermediate struct. This does not break current functionality, so you can still use intermediate struct if you want.
This will resolve questions like this: https://stackoverflow.com/questions/58571896/rust-serde-deserialize-xml-directly-to-vect