Adrian Kaczmarek
Adrian Kaczmarek

Reputation: 167

How can I control yaml indentation using SnakeYaml during dumping?

I am using snakeyaml library to parse yaml files, and dump it later from xml. I was wondering if there is any way to control final yaml indentation. For example, list in final file will look like this:

list:
- "first item"
- "second item"

I would like add some spaces before items of list. Final result should like like:

list:
   - "first item"
   - "second item"

I see there is possibility to add custom resolvers and representers. But neither let me to add extra spaces. I've seen that in ScalarNode class, there are marks which contain info about starting column and ending column, but those are used only for logging purpose. Does anyone know solution for such scenario?

Upvotes: 7

Views: 2307

Answers (3)

dariober
dariober

Reputation: 9062

Maybe obvious to others but not to me... The DumperOptions has to be passed to the Yaml constructor. For example:

DumperOptions options = new DumperOptions();
options.setIndent(2);
# More options...

Yaml yaml = new Yaml(options);
FileWriter writer = new FileWriter("out.yml");
yaml.dump(myData, writer);

Upvotes: 1

sercheo_87
sercheo_87

Reputation: 873

For apply the indentation required apply next configuration:

DumperOptions options = new DumperOptions();
options.setIndent(2);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setIndicatorIndent(2);
options.setIndentWithIndicator(true);

Where the properties IndicatorIndent and IndentWithIndicator apply this format output.

Upvotes: 11

Andrey
Andrey

Reputation: 356

DumperOptions.setIndicatorIndent() will do what you need.

Upvotes: 7

Related Questions