dtolnay/serde-yaml

Add a `to_string_pretty` method

Closed this issue · 1 comments

I would like to be able to print in a different way \n inside strings.
Example:

use serde::Serialize;

#[derive(Serialize, Debug)]
struct A {
    b: String,
    c: String,
}

fn main() {
    let a = A {
        b: "aaa\nbbb\n".to_string(),
        c: "ccc".to_string(),
    };
    let yaml = serde_yaml::to_string(&a).unwrap();
    println!("{}", yaml);
}

prints:

---
b: "aaa\nbbb\n"
c: ccc

Instead I would like to have:

---
b: |
  aaa
  bbb
c: cccc

Maybe this could be a good fit for a function similar to serde_json::to_string_pretty

As of 0.9 the formatting is prettier by default so a separate method shouldn't be needed.