quick-xml icon indicating copy to clipboard operation
quick-xml copied to clipboard

Serialization of Hashmap duplicate outer tag in the inners elements. Deserialization has no such problem and fails on serialized xml.

Open julienfr112 opened this issue 3 years ago • 1 comments

quick-xml = {version = "0.23.0-alpha2", features = ["serialize"]}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use quick_xml::de::from_str;
use quick_xml::se::to_string;

#[derive(Debug, Serialize, Deserialize)]
struct Users {
    users: HashMap<String, User>,
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Max(u16);

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct User {
    max: Max,
}

fn main() {
    let xml = "<Users><users><roger><max>10</max></roger></users></Users";
    let users: Users = from_str(xml).unwrap();
    println!("des : {:?}", users);
    let xml2 = to_string(&users).unwrap();
    println!("res : {:?}", xml);
    println!("res : {:?}", xml2);
}

output :

des : Users { users: {"roger": User { max: Max(10) }} }
res : "<Users><users><roger><max>10</max></roger></users></Users"
res : "<Users><users><roger><users><max>10</max></users></roger></users></Users>"
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("missing field `max`")', src/main.rs:27:41
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

julienfr112 avatar Nov 19 '21 11:11 julienfr112

Enums have the same problem (#346)

Storyyeller avatar Dec 22 '21 19:12 Storyyeller