Search code examples
yamlsnakeyaml

Is there a SnakeYaml DumperOptions setting to avoid double-spacing output?


I seem to see double-spaced output when parsing/dumping a simple YAML file with a pipe-text field.

The test is:

public void yamlTest() 
{
    DumperOptions printOptions = new DumperOptions();
    printOptions.setLineBreak(DumperOptions.LineBreak.UNIX);
    Yaml y = new Yaml(printOptions);
    String input = "foo: |\n" +
            "      line 1\n" +
            "      line 2\n";
    Object parsedObject = y.load(new StringReader(input));
    String output = y.dump(parsedObject);
    System.out.println(output);
}

and the output is:

{foo: 'line 1

    line 2

    '}

Note the extra space between line 1 and line 2, and after line 2 before the end of the string.

This test was run on Mac OS X 10.6, java version "1.6.0_29".

Thanks!

Mark


Solution

  • In the original string you use literal style - it is indicating by the '|' character. When you dump your text, you use single-quoted style which ignores the '\n' characters at the end. That is why they are repeated with the empty lines. Try to set different styles in DumperOptions:

    // and others - FOLDED, DOUBLE_QUOTED
    DumperOptions.setDefaultScalarStyle(ScalarStyle.LITERAL)