Comments (11)
Oookay, I used this use quick_xml::se::to_string;
instead of current crate and works like a charm
from serde-xml-rs.
I managed to reduce it to this test case which fails for me on master:
#[test]
fn serialize_nested_collection() {
#[derive(Debug, Serialize, PartialEq)]
struct OuterCollection {
a: Vec<A>,
}
#[derive(Debug, Serialize, PartialEq)]
struct A {
name: String,
}
let coll = OuterCollection {
a: vec![A {
name: "42".to_owned(),
}],
};
let str = to_string(&coll).unwrap();
println!("str={:?}", str);
}
running 1 test
thread 'serialize_nested_collection' panicked at 'called `Result::unwrap()` on an `Err` value: Writer { source: LastElementNameNotAvailable }', tests/test.rs:400:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test serialize_nested_collection ... FAILED
from serde-xml-rs.
Thanks. I stumbled over this just now. It appears, that serde-xml-rs is dead.
from serde-xml-rs.
Here is the debug output:
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Struct DeleteObjectsRequest
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field delete
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Struct Delete
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field objects
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Sequence
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Struct ObjectIdentifier
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field key
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field version_id
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Some
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Struct ObjectIdentifier
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field key
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field version_id
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] None
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Struct ObjectIdentifier
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field key
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] field version_id
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser] Some
2022-09-29T18:27:21.520Z DEBUG [serde_xml_rs::ser::map] end field
from serde-xml-rs.
+1
There is a bug when serializing Vec's of struct or enum. I'm working on a fix.
from serde-xml-rs.
I am having this issue now too, deserializing straight into a struct and converting it straight back gives this error. i do not get it when using serde_json::to_string
, so it must be on serde-xml's end.
from serde-xml-rs.
I am having tha same issue in this code:
use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_str, to_string};
#[derive(Debug, Default, Deserialize, Serialize)]
struct Video {
id: u32,
title: String,
description: String,
removed: bool,
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct Videos {
#[serde(rename = "Video")]
videos: Vec
fn main() {
//let file = File::open("videos.xml").expect("Unable to open file");
//let reader = BufReader::new(file);
//let videos: Videos = from_reader(reader).expect("Unable to parse XML");
//println!("Videos: {:?}", videos);
let xml_string = r#"
<Videos>
<Video>
<id>1</id>
<title>Video 1</title>
<description>This is the first video.</description>
<removed>false</removed>
</Video>
<Video>
<id>2</id>
<title>Video 2</title>
<description>This is the second video.</description>
<removed>true</removed>
</Video>
</Videos>
"#;
let videos: Videos = from_str(xml_string).unwrap();
println!("{:#?}", videos);
let xml_str_out = to_string(&videos).unwrap();
println!("{}", xml_str_out);
}
That's the error:
thread 'main' panicked at 'called Result::unwrap()
on an Err
value: Writer { source: LastElementNameNotAvailable }', src/main.rs:45:42
Any solution?
from serde-xml-rs.
I have exactly the same problem... Is there any workaround?
In general vector serializing works, but if the struct has rename-serialize then it's broken...
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename(serialize = "some-parameter"))] /// !!! This is a problem, commenting this line makes it work, but produces wrong XML
struct SomeParameter {
key: String,
value: String,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename(serialize = "some-session"))]
struct SomeSession {
#[serde(rename = "$value")]
some_parameters: Vec<SomeParameter>,
}
from serde-xml-rs.
I also have the same error
from serde-xml-rs.
@han1548772930, use the quick_xml library. Works like a charm.
Example:
Cargo.toml
[dependencies] serde = { version = "1.0", features = ["derive"] } quick-xml = {version = "0.31.0", features = ["serialize"]}
Code:
use serde::{Deserialize, Serialize};
use quick_xml::de::from_str as from_str;
use quick_xml::se::to_string as to_string;
#[derive(Debug, Default, Deserialize, Serialize)]
struct Video {
id: u32,
#[serde(rename = "Title")]
title: String,
description: String,
removed: bool,
}
#[derive(Debug, Default, Deserialize, Serialize)]
struct Videos {
#[serde(rename = "Video")]
videos: Vec<Video>,
}
fn main() {
let xml_string = r#"<Videos> <Video> <id>1</id> <Title>Video 1</Title> <description>This is the first video.</description> <removed>false</removed> </Video> <Video> <id>2</id> <Title>Video 2</Title> <description>This is the second video.</description> <removed>true</removed> </Video> </Videos>
"#;
let videos: Videos = from_str(xml_string).unwrap();
println!("{:?}", videos);
let xml_str_out = to_string(&videos).unwrap();
println!("{}", xml_str_out);
from serde-xml-rs.
@han1548772930, use the quick_xml library. Works like a charm. Example: Cargo.toml
[dependencies] serde = { version = "1.0", features = ["derive"] } quick-xml = {version = "0.31.0", features = ["serialize"]}
Code: use serde::{Deserialize, Serialize}; use quick_xml::de::from_str as from_str; use quick_xml::se::to_string as to_string;
#[derive(Debug, Default, Deserialize, Serialize)] struct Video { id: u32, #[serde(rename = "Title")] title: String, description: String, removed: bool, }
#[derive(Debug, Default, Deserialize, Serialize)] struct Videos { #[serde(rename = "Video")]
videos: Vec<Video>,
}fn main() { let xml_string = r#"
<Videos> <Video> <id>1</id> <Title>Video 1</Title> <description>This is the first video.</description> <removed>false</removed> </Video> <Video> <id>2</id> <Title>Video 2</Title> <description>This is the second video.</description> <removed>true</removed> </Video> </Videos>
"#;let videos: Videos = from_str(xml_string).unwrap(); println!("{:?}", videos); let xml_str_out = to_string(&videos).unwrap(); println!("{}", xml_str_out);
yes I have switched to quick_xml
from serde-xml-rs.
Related Issues (20)
- Decoding 0 elements in to a Vec gives "custom: missing field `$value`" error HOT 2
- Deserialization with custom deserializer does not work according to serde spec HOT 1
- Is there something like "from_slice"
- custom: missing field `id`
- Expose EmitterConfig/Make document declaration optional
- readme.md example broken out of the box
- readme.md example broken out of the box
- Can't serialize a struct with error 'LastElementNotAvailable'
- Error `DocumentStartAlreadyEmitted` while trying to serialize simple structure in 0.6.0
- Hoot serialize into attributes HOT 2
- Cannot add attribute id
- duplicate field `$value`'
- Does anyone have an improved fork or does any better alternative exist?
- You've found a bug in xml-rs, caused by calls to push_pos() in states that don't end up emitting events
- Regression : Fix xml-rs depencency version in Cargo.toml
- error order when serde a enum field
- Parse error whlist parsing hackernoon xml feed
- Enum/choice list does not work HOT 1
- Unable to Serialize vector struct HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serde-xml-rs.