6

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?

  • From [the docs](https://bitbucket.org/asomov/snakeyaml/wiki/Documentation#markdown-header-block-sequences): '*A block sequence may be nested to a block mapping. Note that in this case it is not necessary to indent the sequence.*' But maybe `DumperOptions options = new DumperOptions(); options.setIndent(2);` can help. –  Aug 30 '19 at 14:09

2 Answers2

7

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.

sercheo_87
  • 803
  • 9
  • 13
6

DumperOptions.setIndicatorIndent() will do what you need.

nomadSK25
  • 2,350
  • 3
  • 25
  • 36
Andrey
  • 346
  • 1
  • 7
  • And depending on your prefere IndicatorIndent you may have to set Indent as well. `options.setIndent(4)` – KevinO Nov 05 '20 at 01:34