serde-yaml icon indicating copy to clipboard operation
serde-yaml copied to clipboard

Deserialize empty map and default value

Open ibigbug opened this issue 2 years ago • 0 comments

I have this code

use std::collections::HashMap;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(default)]
struct Request {
    m: HashMap<String, String>
}

impl Default for Request {
    fn default() -> Self {
        Self {
            m: Default::default(),
        }
    }
}



fn main() {
    let json = r#"
    m:
    "#;

    let requests: Request = serde_yaml::from_str(json).unwrap();
    println!("{:?}", requests);
}

I'd expect the Request.m to be an empty map.

but it actually throws:

 value: Message("invalid type: unit value, expected a map", Some(Pos { marker: Marker { index: 12, line: 4, col: 0 }, path: "m" }))

why it's showing as uint?

can I get an empty map in this case?

playgroud: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2b451c3a8e43672a1e8632afcab23db

thanks

ibigbug avatar Aug 08 '22 14:08 ibigbug

Fixed in 0.9.7.

dtolnay avatar Aug 13 '22 20:08 dtolnay