dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
964 stars 164 forks source link

How to serialize Mappings with empty values using the ? notation #238

Open benjamin-bergia opened 2 years ago

benjamin-bergia commented 2 years ago

Hi, first off, thank you for the great work! I have recently started to use this lib and I found that when serializing mappings, empty values are replaces by ~. For one of my projects I would like to generate files containing the ? notation instead (like in the unordered set example).

So for example, currently:

fn main() {
    let mut hm = HashMap::new();
    hm.insert("test", ());

    let mut set = HashMap::new();
    set.insert("mySet", hm); 

    println!("{}", serde_yaml::to_string(&set).unwrap());
}

gives:

---
mySet:
  test: ~

Is there any option or simple solution to get this instead?

---
mySet:
  ? test
puetzp commented 2 years ago

Just for reference, this seems to be a property of the underlying library, see this line. And there also exists an open issue that is at least somewhat related to this.