In today's environment using at least version 2.12.x
of jackson-dataformat-yaml
thanks to the introduction of YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR
, you can simply just do:
public ObjectMapper yamlObjectMapper() { final YAMLFactory factory = new YAMLFactory() .enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR) .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); return new ObjectMapper(factory);}
Which will give you the output of:
employees: - name: John age: 26 - name: Bill age: 17