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?
Indentation. The suggested syntax for YAML files is to use 2 spaces for indentation, but YAML will follow whatever indentation system that the individual file uses.
Indentation is meaningful in YAML. Make sure that you use spaces, rather than tab characters, to indent sections. In the default configuration files and in all the examples in the documentation, we use 2 spaces per indentation level. We recommend you do the same.
SnakeYAML allows you to read a YAML file into a simple Map object or parse the file and convert it into a custom Java object. Depending on your requirements you can decide in which format you want to read your YAML files.
In YAML block styles, structure is determined by indentation. In general, indentation is defined as a zero or more space characters at the start of a line. To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently.
DumperOptions.setIndicatorIndent() will do what you need.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With